I would like to assign some color to label (TNewStaticText - property Color: TColor; read write;) on my form.
I have my color stored as '$RRGGBB' (String) and I would like to convert it to TColor using Delphi function StringToColor() but Inno Setup shows me an error if I use this function in script.
How to tell Inno Setup to use this function or how to convert String to TColor in Inno to use it with this property?
As Andreas mentioned in his comment to the question, there's no built-in function. You can craft your own, example:
function StringToRGB(ColorStr: string): Integer;
var
r, g, b: Integer;
begin
r := StrToInt(Copy(ColorStr, 1, 3));
g := StrToInt('$' + Copy(ColorStr, 4, 2));
b := StrToInt('$' + Copy(ColorStr, 6, 2));
Result := (r or (g shl 8) or (b shl 16));
end;
Should you have been storing your colors like $BBGGRR
, you could simply convert it with
StrToInt(ColorStr);
Sertac Akyuz's approach is fine, but I solved this few minutes after writing this question, sorry :)
This is my function - there is a little more handling:
function StringToColor(Color: String): TColor;
var
RR, GG, BB: String;
Dec: Integer;
begin
{ Change string Color from $RRGGBB to $BBGGRR and then convert to TColor }
if((Length(Color) <> 7) or (Color[1] <> '$')) then
Result := $000000
else
begin
RR := Color[2] + Color[3];
GG := Color[4] + Color[5];
BB := Color[6] + Color[7];
Dec := StrToInt('$' + BB + GG + RR);
Result := TColor(Dec);
end;
end;
The whole magic does function StrToInt() which converts String to Integer - but if you add '$' before string it treats the string as Hex number and it will be converted automatically to Decimal number. Result is Integer TColor created from that number.
@Andreas Rejbrand:
When I downloaded Inno's sources I noticed function StringToColor() in sources. Here is fragment of Compiler.pas:
type
TColor = $7FFFFFFF-1..$7FFFFFFF;
const
clScrollBar = TColor(COLOR_SCROLLBAR or $80000000);
clBackground = TColor(COLOR_BACKGROUND or $80000000);
clActiveCaption = TColor(COLOR_ACTIVECAPTION or $80000000);
clInactiveCaption = TColor(COLOR_INACTIVECAPTION or $80000000);
clMenu = TColor(COLOR_MENU or $80000000);
clWindow = TColor(COLOR_WINDOW or $80000000);
clWindowFrame = TColor(COLOR_WINDOWFRAME or $80000000);
clMenuText = TColor(COLOR_MENUTEXT or $80000000);
clWindowText = TColor(COLOR_WINDOWTEXT or $80000000);
clCaptionText = TColor(COLOR_CAPTIONTEXT or $80000000);
clActiveBorder = TColor(COLOR_ACTIVEBORDER or $80000000);
clInactiveBorder = TColor(COLOR_INACTIVEBORDER or $80000000);
clAppWorkSpace = TColor(COLOR_APPWORKSPACE or $80000000);
clHighlight = TColor(COLOR_HIGHLIGHT or $80000000);
clHighlightText = TColor(COLOR_HIGHLIGHTTEXT or $80000000);
clBtnFace = TColor(COLOR_BTNFACE or $80000000);
clBtnShadow = TColor(COLOR_BTNSHADOW or $80000000);
clGrayText = TColor(COLOR_GRAYTEXT or $80000000);
clBtnText = TColor(COLOR_BTNTEXT or $80000000);
clInactiveCaptionText = TColor(COLOR_INACTIVECAPTIONTEXT or $80000000);
clBtnHighlight = TColor(COLOR_BTNHIGHLIGHT or $80000000);
cl3DDkShadow = TColor(COLOR_3DDKSHADOW or $80000000);
cl3DLight = TColor(COLOR_3DLIGHT or $80000000);
clInfoText = TColor(COLOR_INFOTEXT or $80000000);
clInfoBk = TColor(COLOR_INFOBK or $80000000);
clBlack = TColor($000000);
clMaroon = TColor($000080);
clGreen = TColor($008000);
clOlive = TColor($008080);
clNavy = TColor($800000);
clPurple = TColor($800080);
clTeal = TColor($808000);
clGray = TColor($808080);
clSilver = TColor($C0C0C0);
clRed = TColor($0000FF);
clLime = TColor($00FF00);
clYellow = TColor($00FFFF);
clBlue = TColor($FF0000);
clFuchsia = TColor($FF00FF);
clAqua = TColor($FFFF00);
clLtGray = TColor($C0C0C0);
clDkGray = TColor($808080);
clWhite = TColor($FFFFFF);
clNone = TColor($1FFFFFFF);
clDefault = TColor($20000000);
type
TColorEntry = record
Value: TColor;
Name: string;
end;
const
Colors: array[0..41] of TColorEntry = (
(Value: clBlack; Name: 'clBlack'),
(Value: clMaroon; Name: 'clMaroon'),
(Value: clGreen; Name: 'clGreen'),
(Value: clOlive; Name: 'clOlive'),
(Value: clNavy; Name: 'clNavy'),
(Value: clPurple; Name: 'clPurple'),
(Value: clTeal; Name: 'clTeal'),
(Value: clGray; Name: 'clGray'),
(Value: clSilver; Name: 'clSilver'),
(Value: clRed; Name: 'clRed'),
(Value: clLime; Name: 'clLime'),
(Value: clYellow; Name: 'clYellow'),
(Value: clBlue; Name: 'clBlue'),
(Value: clFuchsia; Name: 'clFuchsia'),
(Value: clAqua; Name: 'clAqua'),
(Value: clWhite; Name: 'clWhite'),
(Value: clScrollBar; Name: 'clScrollBar'),
(Value: clBackground; Name: 'clBackground'),
(Value: clActiveCaption; Name: 'clActiveCaption'),
(Value: clInactiveCaption; Name: 'clInactiveCaption'),
(Value: clMenu; Name: 'clMenu'),
(Value: clWindow; Name: 'clWindow'),
(Value: clWindowFrame; Name: 'clWindowFrame'),
(Value: clMenuText; Name: 'clMenuText'),
(Value: clWindowText; Name: 'clWindowText'),
(Value: clCaptionText; Name: 'clCaptionText'),
(Value: clActiveBorder; Name: 'clActiveBorder'),
(Value: clInactiveBorder; Name: 'clInactiveBorder'),
(Value: clAppWorkSpace; Name: 'clAppWorkSpace'),
(Value: clHighlight; Name: 'clHighlight'),
(Value: clHighlightText; Name: 'clHighlightText'),
(Value: clBtnFace; Name: 'clBtnFace'),
(Value: clBtnShadow; Name: 'clBtnShadow'),
(Value: clGrayText; Name: 'clGrayText'),
(Value: clBtnText; Name: 'clBtnText'),
(Value: clInactiveCaptionText; Name: 'clInactiveCaptionText'),
(Value: clBtnHighlight; Name: 'clBtnHighlight'),
(Value: cl3DDkShadow; Name: 'cl3DDkShadow'),
(Value: cl3DLight; Name: 'cl3DLight'),
(Value: clInfoText; Name: 'clInfoText'),
(Value: clInfoBk; Name: 'clInfoBk'),
(Value: clNone; Name: 'clNone'));
function IdentToColor(const Ident: string; var Color: Longint): Boolean;
var
I: Integer;
begin
for I := Low(Colors) to High(Colors) do
if CompareText(Colors[I].Name, Ident) = 0 then
begin
Result := True;
Color := Longint(Colors[I].Value);
Exit;
end;
Result := False;
end;
function StringToColor(const S: string): TColor;
begin
if not IdentToColor(S, Longint(Result)) then
Result := TColor(StrToInt(S));
end;
As you can see Inno has it's own implementation of StringToColor() which can also parse colors by names [like IdentToColor('clBlack')].
I was hoping for some quick hack how to use this function from script but it seems that this function is not pulled to public interface (ScriptFunc_R.pas or ScriptFunc_C.pas).