tags:

views:

566

answers:

5

How do you convert a TColor value to a decimal representation (clRed = $0000FF)?

+2  A: 

Try

function FromTColorDelphiToHex(Color : TColor) : string;
begin
   Result :='$'+
     IntToHex(GetBValue(Color), 2) +
     IntToHex(GetGValue(Color), 2) +
     IntToHex(GetRValue(Color), 2) ;
end;
RRUZ
+4  A: 

Try this:

uses
   Graphics;

function ColorToHex(const color: TColor): string;
begin
   result := '$' + IntToHex(ColorToRGB(color), 6);
end;
Mason Wheeler
I won't vote this down, but this won't work according to Bill Miller's specification. His example showed (clRed = $0000FF), so I'm guessing he wants the TColor BGR format. ColorToHex(clRed) returns $FF0000.
Wouter van Nifterick
Ah, leave it.. Bill Miller's own solution does it like you did it.. whatever guys. :) +1 after all then
Wouter van Nifterick
@Wouter: On **my** Delphi 4 this `ColorToHex(clRed)` above does return `$0000FF`, YMMV of course.
mghie
A: 

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...

Bill Miller
Nosredna mentioned it already. This is not decimal, but hexadecimal. ColorToDecimal would be `IntToStr(clRed)`. Your function doesn't match your example by the way... ColorToDecimal(clRed) is going to return $00FF0000 and not $0000FF.
Wouter van Nifterick
Stop calling it decimal! :-)
Nosredna
@Wouter: Same here, `ColorToDecimal(clRed)` is **not** going to return `$00FF0000` but `$000000FF`. It might be a good idea to try stuff next time before commenting.
mghie
+1  A: 

I've always been a fan of "Format" for such uses:

function ColorToHex(color: TColor): String;
begin
   Result := Format('$%.6x', [integer(aColor)]);
end;
Jim
A: 

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;
Gerry
Why would you concatenate three strings instead of calling `Format()` once with the `'%.2x%.2x%.2x'` format string and the three channel values?
mghie
Would need to profile it to see which is faster. Format isn't very fast as it has to parse the input. IntToHex of later delphi versions calls the same routine as IntToStr.
Gerry
Using Delphi 2009 the solution with three `IntToHex()` calls and string concatenation is about 25 % slower than the single `Format()` call. But for me it's not about performance: From the format string one can see at a glance what the end result will be.
mghie