zoukankan      html  css  js  c++  java
  • delphi 颜色转换函数总结

    unit UColor;
    
    interface
    
    uses windows, sysutils, classes, graphics;
    
    function HexToInt(Hexa: String): LongWord;
    
    function ColorToString(color: TColor): String;
    
    
    function WebColorToDelphiTColor(webcolor: String): TColor;
    function HexToTColor(sHtmlColor: String): TColor;
    
    function HexIntColorToHtmlColor(c: Integer): String;
    function TColorToWebColor(DColor: TColor): String;
    function HexStrColorToHtmlColor(s: String): String;
    
    
    
    
    
    
    implementation
    
    function HexToInt(Hexa: String): LongWord;
    const
      ValoresHexa: array['A'..'F'] of Integer = (10, 11, 12, 13, 14, 15);
    var
      nDecimal: LongWord;
      nIndex: Byte;
    begin
      nDecimal := 0;
      Hexa := Uppercase(Hexa);
      for nIndex := Length(Hexa) downto 1 do
        if Hexa[nIndex] in ['0'..'9'] then
          nDecimal := nDecimal + StrToInt(Hexa[nIndex]) *
            Trunc(Exp((Length(Hexa) - nIndex) * ln(16)))
      else
        nDecimal := nDecimal + ValoresHexa[Hexa[nIndex]] *
          Trunc(Exp((Length(Hexa) - nIndex) * ln(16)));
      Result := nDecimal;
    end;
    
    function ColorToString(color: TColor): String;
    var
      r, g, b: Byte;
    begin
      r := GetRValue(color);
      g := GetgValue(color);
      b := GetbValue(color);
      Result := '$' + IntToHex(TColor(RGB(r, g, b)), 8);
    end;
    
    
    function WebColorToDelphiTColor(webcolor: String): TColor;
    var
      a: array [0..3] of Byte;
      b: array[0..3] of Byte;
    begin
    {
          rgb颜色,就是用6位16进制数去表示的颜色
          RGB的颜色是从低位向高位存储,而TCOLOR正好与之相反,
          例如
           RGB : F1F2FE
           Tcolor: $00FEF2F1
    }
      Integer(a) := HexToInt(webcolor);
      if a[3] = 0 then
      begin
        b[0] := a[2];
        b[1] := a[1];
        b[2] := a[0];
        b[3] := 0;
      end
      else
      begin
        b[0] := a[3];
        b[1] := a[2];
        b[2] := a[1];
        b[3] := a[0];
      end;
      Result := TColor(b);
    end;
    
    
    function HexToTColor(sHtmlColor: String): TColor;
    begin
      //与上面 WebColorToDelphiTColor 的功能相同
      if pos('#', sHtmlColor) = 1 then
        sHtmlColor := copy(sHtmlColor, 2, length(sHtmlColor));
    
      Result :=
        RGB(StrToInt(#36 + Copy(sHtmlColor, 1, 2)),
        StrToInt(#36 + Copy(sHtmlColor, 3, 2)), StrToInt(#36 + Copy(sHtmlColor, 5, 2)));
    end;
    
    
    function HexIntColorToHtmlColor(c: Integer): String;
    var
      R, G, B: Byte;
    begin
      R := c and $FF;
      G := (c shr 8) and $FF;
      B := (c shr 16) and $FF;
      Result := #35 + Format('%.2x%.2x%.2x', [R, G, B]);
    end;
    
    {从十六进制字符串转换到 Html 颜色}
    function HexStrColorToHtmlColor(s: String): String;
    var
      i: Integer;
      R, G, B: Byte;
    begin
      i := StrToInt(s);
      R := i and $FF;
      G := (i shr 8) and $FF;
      B := (i shr 16) and $FF;
      Result := #35 + Format('%.2x%.2x%.2x', [R, G, B]);
    end;
    
    function TColorToWebColor(DColor: TColor): String;
    var
      tmpRGB: TColorRef;
    begin
      tmpRGB := ColorToRGB(DColor);
      Result := Format('#%.2x%.2x%.2x', [GetRValue(tmpRGB),
        GetGValue(tmpRGB), GetBValue(tmpRGB)]);
    end;
    
    
    
    
    end.
  • 相关阅读:
    用一个测试类简化排序算法时间复杂度的研究
    用斗地主的实例学会使用java Collections工具类
    数据结构:用实例分析ArrayList与LinkedList的读写性能
    用一个通俗易懂的例子彻底说清楚单例模式
    用自定义链式栈解决力扣括号匹配问题
    数据结构之链式队列的代码实现及有趣应用
    用非常硬核的JAVA序列化手段实现对象流的持久化保存
    SpringBoot整合SpringSecurity实现JWT认证
    六百字搞懂lambda
    判空我推荐StringUtils.isBlank
  • 原文地址:https://www.cnblogs.com/yzryc/p/6322037.html
Copyright © 2011-2022 走看看