2004. március 25., csütörtök
How to convert a hex color to its HTML counterpart
Problem/Question/Abstract:
How to convert a hex color to its HTML counterpart
Answer:
Solve 1:
This may run faster using shl/ shr and the bitwise AND operator to separate R, G and B parts before converting to a string:
function ColorToHTML(C: TColor): string;
begin
Result := IntToHex(C, 6);
Result := '#' + Copy(Result, 5, 2) + Copy(Result, 3, 2) + Copy(Result, 1, 2);
end;
function HTMLToColor(C: string): TColor;
begin
if Copy(C, 1, 1) = '#' then
C := Copy(C, 2, Length(C));
Result := StrToInt('$' + Copy(C, 5, 2) + Copy(C, 3, 2) + Copy(C, 1, 2));
end;
Solve 2:
The HTML color code is the other way round as TColor :
TColor = $DDBB ggrr
HTML = $00 RRGGBB
So you just have to swap bytes:
{ ... }
var
HTMLColor, mycolor: Integer;
{ ... }
HTMLColor := (((MyColor and $FF) shl 16) or ((MyColor and $FF00)) or
((MyColor and $FF0000) shr 16));
YourHTMLColor := IntToHex(HTMLColor, 8);
Feliratkozás:
Megjegyzések küldése (Atom)
Nincsenek megjegyzések:
Megjegyzés küldése