UnitUcolor;Interfaceuseswindows, Sysutils, classes, graphics;functionHexToInt (hexa:string): Longword;functioncolortostring (Color:tcolor): String;functionWebcolortodelphitcolor (webcolor:string): TColor;functionHextotcolor (shtmlcolor:string): TColor;functionHexintcolortohtmlcolor (C:integer): String;functionTcolortowebcolor (Dcolor:tcolor): String;functionHexstrcolortohtmlcolor (s:string): String;ImplementationfunctionHexToInt (hexa:string): Longword;ConstValoreshexa:Array['A'..'F'] ofInteger = (Ten, One, A, -, -, the);varNdecimal:longword; Nindex:byte;beginNdecimal:=0; Hexa:=uppercase (HEXA); forNIndex: = Length (Hexa)Downto 1 Do ifHexa[nindex]inch['0'..'9'] ThenNdecimal:= Ndecimal + strtoint (Hexa[nindex]) *Trunc (Exp (Length (Hexa)-nIndex) * LN ( -))) ElseNdecimal:= Ndecimal + Valoreshexa[hexa[nindex]] *Trunc (Exp (Length (Hexa)-nIndex) * LN ( -))); Result:=Ndecimal;End;functioncolortostring (Color:tcolor): String;varR, G, B:byte;beginr:=getrvalue (color); G:=getgvalue (color); B:=getbvalue (color); Result:='$'+ Inttohex (TColor (RGB (R, G, b)),8);End;functionWebcolortodelphitcolor (webcolor:string): TColor;varA:Array[0..3] ofByte; B:Array[0..3] ofByte;begin{The RGB color, which is the color of RGB, which is represented by a 6-bit 16 binary number, is stored from low to high, and TColor is the opposite, for example Rgb:f1f2fe TColor: $00fef2f1}Integer (a):=HexToInt (Webcolor); ifa[3] =0 Then beginb[0]: = a[2]; b[1]: = a[1]; b[2]: = a[0]; b[3] :=0; End Else beginb[0]: = a[3]; b[1]: = a[2]; b[2]: = a[1]; b[3]: = a[0]; End; Result:=TColor (b);End;functionHextotcolor (shtmlcolor:string): TColor;begin //same function as above Webcolortodelphitcolor ifPos'#', shtmlcolor) =1 ThenShtmlcolor:= Copy (Shtmlcolor,2, Length (Shtmlcolor)); Result:=RGB (Strtoint (# $+ Copy (Shtmlcolor,1,2)), Strtoint (# $+ Copy (Shtmlcolor,3,2)), Strtoint (# $+ Copy (Shtmlcolor,5,2)));End;functionHexintcolortohtmlcolor (C:integer): String;varR, G, B:byte;beginR:= C and$FF; G:= (cSHR 8) and$FF; B:= (cSHR -) and$FF; Result:= # *+ Format ('%.2x%.2x%.2x', [R, G, B]);End;{convert from hexadecimal string to Html color}functionHexstrcolortohtmlcolor (s:string): String;varI:integer; R, G, B:byte;beginI:=Strtoint (s); R:= I and$FF; G:= (iSHR 8) and$FF; B:= (iSHR -) and$FF; Result:= # *+ Format ('%.2x%.2x%.2x', [R, G, B]);End;functionTcolortowebcolor (Dcolor:tcolor): String;varTmprgb:tcolorref;beginTmprgb:=Colortorgb (Dcolor); Result:= Format ('#%.2x%.2x%.2x', [Getrvalue (Tmprgb), Getgvalue (Tmprgb), Getbvalue (TMPRGB)]);End;End.
Delphi Color Conversion Function Summary