How to convert ARGB value from string to color?
I'm trying to use a method MakeColor
in a GDIPAPI module, but converting from int
to byte
doesn't return the correct value for me.
Example
var
argbStr: string;
A, R, G, B: Byte;
begin
argbStr := 'ffffcc88';
A := StrToInt('$' + Copy(AValue, 1, 2));
R := StrToInt('$' + Copy(AValue, 3, 2));
G := StrToInt('$' + Copy(AValue, 5, 2));
B := StrToInt('$' + Copy(AValue, 7, 2));
Result := MakeColor(A, R, G, B);
end;
What am I doing wrong?
a source to share
The color components in your string are in the same order as in the value ARGB
. Therefore, you don't need to separate the components before combining them with MakeColor
. You can do the conversion directly:
function StringToARGB(const argbStr: string): GDIPAPI.ARGB;
begin
Result := ARGB(StrToInt('$' + argbStr));
end;
Type-casting is needed to suppress the range validation error that you get whenever the alpha component is greater than 127; StrToInt
returns a signed integer but ARGB
is an unsigned type.
a source to share