在Inno Setup Pascal Script中将HTML十六进制颜色转换为TColor

时间:2016-08-19 01:13:31

标签: html inno-setup pascalscript tcolor

我想在Inno Setup Pascal Script中将HTML Hex颜色转换为TColor

我尝试从Convert Inno Setup Pascal Script TColor to HTML hex colour撤消功能ColorToWebColorStr,但我可能需要像RGBToColor这样的函数来获得TColor的结果。

示例

#497AC2 HTML十六进制颜色的转换应返回为TColor $C27A49 输入应为HTML颜色字符串表示形式,输出应为TColor

更新

当我在Inno Setup中使用VCL Windows单元中的以下功能时,TForm.Color显示为红色。

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);

我在TForm.Color中预期的颜色是:

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

此外,我也想知道为什么红色在这里返回(形式显示红色)而不是预期的半浅蓝.........

我想将转化用作:

#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})

提前致谢。

1 个答案:

答案 0 :(得分:1)

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;

您的RGB功能不起作用,因为似乎Pascal脚本(与Delphi相反)不会隐式地将Byte转换/扩展为Integer shl操作。所以你必须明确地做。