How do you convert a TColor value to a decimal representation (clRed = $0000FF)?
Try
function FromTColorDelphiToHex(Color : TColor) : string;
begin
Result :='$'+
IntToHex(GetBValue(Color), 2) +
IntToHex(GetGValue(Color), 2) +
IntToHex(GetRValue(Color), 2) ;
end;
Try this:
uses
Graphics;
function ColorToHex(const color: TColor): string;
begin
result := '$' + IntToHex(ColorToRGB(color), 6);
end;
after several hours I came up with this.
function ColorToDecimal( Color: TColor ): string;
var
AColor: integer;
DecimalColor: integer;
begin
DecimalColor := ColorToRGB( Color );
FmtStr( Result, '%s%0.8x', [ HexDisplayPrefix, DecimalColor ] );
end;
I think it works but still testing...
I've always been a fan of "Format" for such uses:
function ColorToHex(color: TColor): String;
begin
Result := Format('$%.6x', [integer(aColor)]);
end;
Colour constants in Delphi (and the Windows API) are simply signed integers. They are normally represented in Hexadecimal format (with a leading $).
It is defined in Graphics.pas as TColor = -$7FFFFFFF-1..$7FFFFFFF;
Positive values ($00000000 -> $00FFFFFF) are in BGR format: $00FF0000 = blue, $0000FF00 = green, $000000FF = red.
Negative values refer to user defineable system colours, like the colour for window text (clWindowText).
To convert a TColor to it's display value, use
IntToHex(Colour, 8);
or use
Format('%.8x', [Colour]);
In older versions of Delphi, IntToHex calls Format(), in later versions it is directly implemented and is much faster.
To convert to HTML #RRBBGG format, you need to reverse the red and green values in RRUZ answer:
function FromTColorDelphiToHTML(Color : TColor) : string;
begin
Result :='#'+
IntToHex(GetRValue(Color), 2) +
IntToHex(GetGValue(Color), 2) +
IntToHex(GetBValue(Color), 2) ;
end;