首页 > 代码库 > delphi 颜色转换函数总结
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.
delphi 颜色转换函数总结
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。