Search code examples
inno-setuprgbtcolor

Converting Inno Setup WizardForm.Color to RGB


If I try this:

[Setup]
AppName=MyApp
AppVerName=MyApp
DefaultDirName={pf}\MyApp
DefaultGroupName=MyApp
OutputDir=.

[Code]
function ColorToRGBstring(Color: TColor): string;
var
  R,G,B : Integer; 
begin 
  R := Color and $ff; 
  G := (Color and $ff00) shr 8; 
  B := (Color and $ff0000) shr 16; 
  result := 'red:' + inttostr(r) + ' green:' + inttostr(g) + ' blue:' + inttostr(b); 
end;

procedure InitializeWizard();
begin
  MsgBox(ColorToRGBstring(WizardForm.Color),mbConfirmation, MB_OK);
end;

I get: red:15 green:0 blue:0
But the result should be: 240 240 240 (grey)

What is wrong?

I need to get the proper TColor and convert it to RGB color code.


Solution

  • When the first byte is $FF, the last byte is an index in a system color palette.

    You can get RGB of the system color using GetSysColor function.

    function GetSysColor(nIndex: Integer): DWORD;
      external '[email protected] stdcall';
    
    function ColorToRGB(Color: TColor): Cardinal;
    begin
      if Color < 0 then
        Result := GetSysColor(Color and $000000FF) else
        Result := Color;
    end;
    

    The ColorToRGB code is copied from Delphi VCL (Vcl.Graphics unit).


    The above code returns Win32 API color, what is $BBGGRR. It you need $RRGGBB, you need to swap the bits:

    Result :=
      ((Result and $FF) shl 16) +
      (Result and $FF00) +
      ((Result and $FF0000) shr 16);