Search code examples
htmlinno-setuppascalscripttcolor

Convert HTML hex colour to TColor in Inno Setup Pascal Script


I want to convert a HTML Hex colour to a TColor in Inno Setup Pascal Script.

I tried reversing the function ColorToWebColorStr from Convert Inno Setup Pascal Script TColor to HTML hex colour, but I might need a function like RGBToColor to get result as the TColor.

Example: Conversion of #497AC2 HTML Hex Colour should be returned as TColor $C27A49. Input should be a HTML colour string representation and output should be a TColor.

When I use the following function from VCL Windows unit in Inno Setup, TForm.Color shows as red.

const
  COLORREF: TColor;

function RGB( R, G, B: Byte): COLORREF;
begin
  Result := (R or (G shl 8) or (B shl 16));
end;

DataChecker.Color := RGB( 73, 122, 194);

The colour I expected in TForm.Color is:

<html>
<body bgcolor="#497AC2">
<h2>This Background Colour is the Colour I expected instead of Red.</h2>
</body>
</html> 

Additionally, I also like to know why red colour is returning here (form showing red) instead of expected semi light blue.........


I want to use the conversion as:

#define BackgroundColour "#497AC2"

procedure InitializeDataChecker;
...
begin
...
  repeat
    ShellExec('Open', ExpandConstant('{pf64}\ImageMagick-7.0.2-Q16\Convert.exe'),
      ExpandConstant('-size ' + ScreenResolution + ' xc:' '{#BackgroundColour}' + ' -quality 100% "{tmp}\'+IntToStr(ImageNumber)+'-X.jpg"'), '', SW_HIDEX, ewWaitUntilTerminated, ErrorCode); 
...    
  until FileExists(ExpandConstant('{tmp}\'+IntToStr(ImageNumber)+'.jpg')) = False; 
...
end;

...
DataChecker := TForm.Create(nil);
{ ---HERE IT SHOULD BE RETURNED AS `$C27A49`--- }
DataChecker.Color := NewFunction({#BackgroundColour})

Thanks in advance.


Solution

  • function RGB(r, g, b: Byte): TColor;
    begin
      Result := (Integer(r) or (Integer(g) shl 8) or (Integer(b) shl 16));
    end;
    
    function WebColorStrToColor(WebColor: string): TColor;
    begin
      if (Length(WebColor) <> 7) or (WebColor[1] <> '#') then
        RaiseException('Invalid web color string');
    
      Result :=
        RGB(
          StrToInt('$' + Copy(WebColor, 2, 2)),
          StrToInt('$' + Copy(WebColor, 4, 2)),
          StrToInt('$' + Copy(WebColor, 6, 2)));
    end;
    

    Your RGB function does not work because it seems that Pascal Script (contrary to Delphi) does not implicitly convert/expand the Byte to the Integer for the shl operation. So you have to do it explicitly.