如何将 ARGB 值从字符串转换为颜色?
我尝试在 GDIPAPI 单元中使用 MakeColor
方法,但从 int
到 byte
的转换没有返回正确的值。
示例
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;
我做错了什么?
I am trying to use the MakeColor
method in the GDIPAPI unit but the conversion from int
to byte
is not returning me the correct value.
Example
var
argbStr: string;
A, R, G, B: Byte;
begin
argbStr := 'ffffcc88';
A := StrToInt('
What am I doing wrong?
+ Copy(AValue, 1, 2));
R := StrToInt('
What am I doing wrong?
+ Copy(AValue, 3, 2));
G := StrToInt('
What am I doing wrong?
+ Copy(AValue, 5, 2));
B := StrToInt('
What am I doing wrong?
+ Copy(AValue, 7, 2));
Result := MakeColor(A, R, G, B);
end;
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
字符串中颜色分量的顺序与
ARGB
值中的顺序相同。因此,在使用MakeColor
组合组件之前,您不需要分离组件。您可以直接进行转换:类型转换对于抑制 alpha 分量大于 127 时出现的范围检查错误是必要的;
StrToInt
返回有符号整数,但ARGB
是无符号类型。The color components in your string are in the same order they would have in a
ARGB
value. Therefore, you don't need to separate the components before combining them withMakeColor
. You can do the conversion directly:The type-cast is necessary to suppress a range-checking error you'd get whenever the alpha component was greater than 127;
StrToInt
returns a signed integer, butARGB
is an unsigned type.将
A, R, G, B 的类型更改为 Integer 似乎可以解决该问题。这一定与 Integer ->; 之间的转换有关。字节。
Changing the type of
A, R, G, B
to be Integer seemed to fix the issue. It must be something to do with the casting between Integer -> Byte.