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 = (, , , , , );
var
nDecimal: LongWord;
nIndex: Byte;
begin
nDecimal := ;
Hexa := Uppercase(Hexa);
for nIndex := Length(Hexa) downto do
if Hexa[nIndex] in [''..''] then
nDecimal := nDecimal + StrToInt(Hexa[nIndex]) *
Trunc(Exp((Length(Hexa) - nIndex) * ln()))
else
nDecimal := nDecimal + ValoresHexa[Hexa[nIndex]] *
Trunc(Exp((Length(Hexa) - nIndex) * ln()));
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)), );
end; function WebColorToDelphiTColor(webcolor: String): TColor;
var
a: array [..] of Byte;
b: array[..] of Byte;
begin
{
rgb颜色,就是用6位16进制数去表示的颜色
RGB的颜色是从低位向高位存储,而TCOLOR正好与之相反,
例如
RGB : F1F2FE
Tcolor: $00FEF2F1
}
Integer(a) := HexToInt(webcolor);
if a[] = then
begin
b[] := a[];
b[] := a[];
b[] := a[];
b[] := ;
end
else
begin
b[] := a[];
b[] := a[];
b[] := a[];
b[] := a[];
end;
Result := TColor(b);
end; function HexToTColor(sHtmlColor: String): TColor;
begin
//与上面 WebColorToDelphiTColor 的功能相同
if pos('#', sHtmlColor) = then
sHtmlColor := copy(sHtmlColor, , length(sHtmlColor)); Result :=
RGB(StrToInt(# + Copy(sHtmlColor, , )),
StrToInt(# + Copy(sHtmlColor, , )), StrToInt(# + Copy(sHtmlColor, , )));
end; function HexIntColorToHtmlColor(c: Integer): String;
var
R, G, B: Byte;
begin
R := c and $FF;
G := (c shr ) and $FF;
B := (c shr ) and $FF;
Result := # + 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 ) and $FF;
B := (i shr ) and $FF;
Result := # + 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.