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?

+2


a source to share


2 answers


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.

+3


a source


Changing the type A, R, G, B

for Integer seems to have fixed the problem. It should be related to casting between Integer -> Byte.



0


a source







All Articles