Convert ARGB hex string to RGB

拥有回忆 提交于 2019-12-11 14:31:19

问题


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


回答1:


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}";



回答2:


Why not simply:

string argb = "#FFFF0000";
string rgb = argb.Remove(1, 2);

..since the format of an ARGB hex string is #AARRGGBB (source). This is the most straightforward solution (and much faster than solutions based on conversions).

To cover the case of 4 digits instead of 8, you may use something like:

int alphaDigits = (argb.Length == 9 ? 2 : 1);
string rgb = argb.Remove(1, alphaDigits);



回答3:


Note: This solution, although produces a valid HTML color value, that value doesn't necessarily have to be an RGB hex string as explained below. It's also significantly slower than my other solution.

A third solution would be to use the ColorTranslator class, which works because:

  • The ColorTranslator.FromHtml() method uses ColorConverter.ConvertFrom internally (which can parse ARGB hex values).

  • The ColorTranslator.ToHtml then just ignores the alpha value and only uses the RGB values to generate the string1.

Example:

Color c = ColorTranslator.FromHtml("#FFFF0000");
string rgb = ColorTranslator.ToHtml(c);

And here's some test code to show that this solution will work with all possible ARGB hex values:

try
{
    // Incrementing by 5 to save time. We can use `++` to cover all possible values
    // but it will take a long time to execute.
    for (int a = 0; a <= 255; a += 5)
        for (int r = 0; r <= 255; r += 5)
            for (int g = 0; g <= 255; g += 5)
                for (int b = 0; b <= 255; b += 5)
                    ColorTranslator.FromHtml($"#{a.ToString("X2")}{r.ToString("X2")}" +
                                             $"{g.ToString("X2")}{b.ToString("X2")}");
    Console.WriteLine("All is good!");
}
catch (FormatException ex)
{
    Console.WriteLine($"Failed. Message='{ex.Message}'");
}

1However, keep in mind that ColorTranslator.ToHtml() will not necessarily return an RGB hex string. It tries to return a color name if the color has a known color name (which works for HTML), otherwise, it resorts to RGB hex values in the format of #RRGGBB.



来源:https://stackoverflow.com/questions/58574084/convert-argb-hex-string-to-rgb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!