如何将 ARGB 值从字符串转换为颜色?

发布于 2024-08-30 01:30:48 字数 467 浏览 7 评论 0原文

我尝试在 GDIPAPI 单元中使用 MakeColor 方法,但从 intbyte 的转换没有返回正确的值。

示例

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

三人与歌 2024-09-06 01:30:48

字符串中颜色分量的顺序与 ARGB 值中的顺序相同。因此,在使用 MakeColor 组合组件之前,您不需要分离组件。您可以直接进行转换:

function StringToARGB(const argbStr: string): GDIPAPI.ARGB;
begin
  Result := ARGB(StrToInt('

类型转换对于抑制 alpha 分量大于 127 时出现的范围检查错误是必要的; StrToInt 返回有符号整数,但 ARGB 是无符号类型。

+ argbStr)); end;

类型转换对于抑制 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 with MakeColor. You can do the conversion directly:

function StringToARGB(const argbStr: string): GDIPAPI.ARGB;
begin
  Result := ARGB(StrToInt('

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, but ARGB is an unsigned type.

+ argbStr)); end;

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, but ARGB is an unsigned type.

烟织青萝梦 2024-09-06 01:30:48

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文