Search code examples
hexinno-setuppascalscript

Convert Inno Setup Pascal Script TColor to HTML hex colour


Inno Setup Pascal Script has colour variables like clBtnFace, clYellow in Delphi.

I like to know how can I convert any of those TColor to HTML hex colour.

For example, if I convert clBtnFace to a HTML hex colour, the result should be #497AC2. And if I convert clYellow to a HTML hex colour, the result should be #FFFF00.

I found many examples for above, but they're for RGB colours. I want to convert TColor to HTML hex colour to use as hex colour in the command line parameters for ImageMagick in my Pascal Script like ...xc:#497AC2....

Thanks in advance.


Solution

  • function ColorToWebColorStr(Color: TColor): string;
    var
      RGB: Integer;
    begin
      RGB := ColorToRGB(Color);
      Result :=
        Format('#%.2x%.2x%.2x', [Byte(RGB), Byte(RGB shr 8), Byte(RGB shr 16)]);
      Result := UpperCase(Result);
    end;
    

    For the ColorToRGB, see Converting Inno Setup WizardForm.Color to RGB.


    Though note that using this with system colors (clXXX constants) has little sense on modern themed versions of Windows. For example clBtnFace will always return a system color. Which has nothing to do with an actual color of the form. Each form can have a different color/skin. The clBtnFace is always the same. Actually even with the system skin/theme, the actual form color is a different shade of gray than clBtnFace on modern versions of Windows. The actual form color is clBtnFace only, if you turn off themes in Windows (i.e. with the classical Windows 2000 look).