将ARGB十六进制字符串转换为RGB [英] Convert ARGB hex string to RGB

查看:243
本文介绍了将ARGB十六进制字符串转换为RGB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题非常类似于将RGBA颜色转换为RGB ,只有我有一个十六进制字符串而不是字节颜色值。



我正在使用 EPPlus ,我希望将单元格背景色转换为HTML颜色。

单元格背景色来自此属性 ColorType.Rgb

注意: 该属性表示为Rgb,但正如文档所说,它实际上是Argb。



在测试期间,我有一个红格,其Argb十六进制为#FFFF0000 ,但据我了解,在HTML中不支持Alpha,红色的Rgb十六进制为#FF0000



我需要先将十六进制字符串转换为字节值,然后执行计算



谢谢

解决方案

您可以使用以16为底的十六进制将十六进制字符串转换为整数值

  int argb =转换。 ToInt32( 0xFFFF0000,16); 

然后使用



<$ p创建您的颜色$ p> Color c = Color.FromArgb(argb);

最后,您可以获得一个只有RGB值的新十六进制字符串,

  string rgb = $#{cR:X2} {cG:X2} {cB:X2}; 


My question is very simular to Convert RGBA color to RGB only I have a Hex string not byte colour values.

I am working with EPPlus and I wish to convery cell background colours to HTML colours.
The cell backgound colours come from this property ColorType.Rgb.
Note: The property says Rgb but as the doc says it is actually Argb.

During testing I have one Red cell which has the Argb hex of #FFFF0000 but as I understand it, in HTML the Alpha is not supported and the Rgb hex for red is #FF0000.

I need to first convert the Hex strings to byte values, perform the calculation from the previous answer, convert the Rgb bytes into a hex string.

Thanks

解决方案

You can convert the Hex string to an integer value using the base 16 as

int argb = Convert.ToInt32("0xFFFF0000", 16);

and then create your color with

Color c = Color.FromArgb(argb);

finally you can get a new hex string with only RGB values with

string rgb = $"#{c.R:X2}{c.G:X2}{c.B:X2}";

这篇关于将ARGB十六进制字符串转换为RGB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆