如何将RGBA css颜色格式转换为十六进制格式

时间:2016-07-21 00:53:01

标签: c# css selenium-webdriver colors

在我的硒代码中,我需要验证颜色代码是#192856的背景。但当我得到该元素的CSS属性时,它给我的颜色为rgba格式。现在我需要以十六进制值本身获取值。我怎么能这样做?

quickLinkstab.GetCssValue("background-color")

上面给出的是“rgba(25,40,86,1)”的值,即rgba值。有什么方法可以将它转换回Hex吗?或者我可以在Hex本身获得价值吗?

我也试过下面的代码

 string colorcode = menuColor;
        int argb = Int32.Parse(colorcode.Replace("#", ""), NumberStyles.HexNumber);
        Color clr = Color.FromArgb(argb);


        int r = Convert.ToInt16(clr.R);
        int g = Convert.ToInt16(clr.G);
        int b = Convert.ToInt16(clr.B);
        int a = Convert.ToInt16(clr.A);
        string x = string.Format("rgba({0}, {1}, {2}, {3});", r, g, b,a);

但这个给我的价值就像,“rgba(25,40,86,0);” 。 “a”值的差异。就像我的代码给我0代表“a”但cssvalue是1。

我更倾向于直接获取Hex值的解决方案,或者如果不可能,则将rgba转换为Hex。

6 个答案:

答案 0 :(得分:7)

以下代码涵盖十六进制格式, rgb 格式和 rgba 格式。

public static class ColorHelper
{
    public static Color ParseColor(string cssColor)
    {
        cssColor = cssColor.Trim();

        if (cssColor.StartsWith("#"))
        {
            return ColorTranslator.FromHtml(cssColor);
        }
        else if (cssColor.StartsWith("rgb")) //rgb or argb
        {
            int left = cssColor.IndexOf('(');
            int right = cssColor.IndexOf(')');

            if (left < 0 || right < 0)
                throw new FormatException("rgba format error");
            string noBrackets = cssColor.Substring(left + 1, right - left - 1);

            string[] parts = noBrackets.Split(',');

            int r = int.Parse(parts[0], CultureInfo.InvariantCulture);
            int g = int.Parse(parts[1], CultureInfo.InvariantCulture);
            int b = int.Parse(parts[2], CultureInfo.InvariantCulture);

            if (parts.Length == 3)
            {
                return Color.FromArgb(r, g, b);
            }
            else if (parts.Length == 4)
            {
                float a = float.Parse(parts[3], CultureInfo.InvariantCulture);
                return Color.FromArgb((int)(a * 255), r, g, b);
            }
        }
        throw new FormatException("Not rgb, rgba or hexa color string");
    }
}

预期结果:

[TestClass]
public class ColorParserTest
{
    [TestMethod]
    public void TestParseColorRGB()
    {
        Color c = ColorHelper.ParseColor("rgb(110,120,130)");

        Assert.AreEqual(110, c.R);
        Assert.AreEqual(120, c.G);
        Assert.AreEqual(130, c.B);
        Assert.AreEqual(255, c.A);
    }

    [TestMethod]
    public void TestParseColorRGBA()
    {
        Color c = ColorHelper.ParseColor("rgba(110,120,130,0.5)");

        Assert.AreEqual(110, c.R);
        Assert.AreEqual(120, c.G);
        Assert.AreEqual(130, c.B);
        Assert.AreEqual(127, c.A);
    }

    [TestMethod]
    public void TestParseColorHexa()
    {
        Color c = ColorHelper.ParseColor("#192856");

        Assert.AreEqual(25, c.R);
        Assert.AreEqual(40, c.G);
        Assert.AreEqual(86, c.B);
        Assert.AreEqual(255, c.A);
    }
}

答案 1 :(得分:1)

  1. 使用System.Drawing.ColorTranslator

    string htmlColor = ColorTranslator.ToHtml(myColor);

  2. 使用String.Format

    String.Format("#{0:X2}{1:X2}{2:X2}", colorValue.R,colorValue.G,colorValue.B);

答案 2 :(得分:1)

有一个可用的Selenium库用于颜色:

import org.openqa.selenium.support.Color;

String rgba = "rgba(1,1,1,1)";
Color color = Color.fromString(rgba);
System.out.println("hex value = " + color.asHex());

答案 3 :(得分:0)

这应该可以解决问题。

public static string ConvertRgbaToHex(string rgba)
{
    if (!Regex.IsMatch(rgba, @"rgba\((\d{1,3},\s*){3}(0(\.\d+)?|1)\)"))
            throw new FormatException("rgba string was in a wrong format");

    var matches = Regex.Matches(rgba, @"\d+");
    StringBuilder hexaString = new StringBuilder("#");

    for(int i = 0; i < matches.Count - 1; i++)
    {
        int value = Int32.Parse(matches[i].Value);

        hexaString.Append(value.ToString("X"));
    }

    return hexaString.ToString();
}

测试用例:

public class Program
{
    static void Main(string[] args)
    {
        // Output: #192856
        Console.WriteLine(ConvertRgbaToHex("rgba(25, 40, 86, 1)"));

        Console.Read();
    }

    public static string ConvertRgbaToHex(string rgba)
    {
        if (!Regex.IsMatch(rgba, @"rgba\((\d{1,3},\s*){3}(0(\.\d+)?|1)\)"))
                throw new FormatException("rgba string was in a wrong format");

        var matches = Regex.Matches(rgba, @"\d+");
        StringBuilder hexaString = new StringBuilder("#");

        for(int i = 0; i < matches.Count - 1; i++)
        {
            int value = Int32.Parse(matches[i].Value);

            hexaString.Append(value.ToString("X"));
        }

        return hexaString.ToString();
    }
}

答案 4 :(得分:0)

下面的代码从元素中选择style属性,并将rgb值转换为十六进制格式,然后比较该值。

string color = GetElement(LocateBy.XPath, "XpathOfTheElement").GetAttribute("style").ToString();
string[] colorvalue1 = color.Split('(');

string[] colorvalue2 = colorvalue1[1].Split(')');

string colorvalue = colorvalue2[0].ToString();
string[] ColorCodeRGBValue = colorvalue.Split(',');
Color myColor = Color.FromArgb(Convert.ToInt32(ColorCodeRGBValue[0]), Convert.ToInt32(ColorCodeRGBValue[1]), Convert.ToInt32(ColorCodeRGBValue[2]));

string hexValue = myColor.R.ToString("X2") + myColor.G.ToString("X2") + myColor.B.ToString("X2");

Assert.AreEqual(hexValue , CsvReader.ColorCode,"Color codes are not equal");

答案 5 :(得分:0)

我已经扩展了Thomas的答案,以涵盖#RGBA /#RRGGBBAA格式。

您可能希望以不同的方式处理错误,如果无法解析输入,我宁愿只返回Color.Empty。

(向Typescript添加单元测试导致研究了一些C#代码,这导致发现ColorTranslator完全不了解alpha值-不用做任何事情,就可以进行所有测试!)

public static ColorUtils
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "ColorTranslator throws System.Exception")]
    public static Color HtmlToColor(string htmlColor)
    {
        Guard.ArgumentIsNotNull(htmlColor, nameof(htmlColor));

        var htmlLowerCase = htmlColor.ToLower().Trim();

        try
        {
            if (htmlLowerCase.StartsWith("rgb"))
            {
                return ArgbToColor(htmlLowerCase);
            }
            else if (htmlLowerCase.StartsWith("#"))
            {
                return HexToColor(htmlLowerCase);
            }
            else
            {
                // Fallback to ColorTranslator for named colors, e.g. "Black", "White" etc.
                return ColorTranslator.FromHtml(htmlLowerCase);
            }
        }
        catch
        {
            // ColorTranslator throws System.Exception, don't really care what the actual error is.
        }

        return Color.Empty;
    }

    private static Color HexToColor(string htmlLowerCase)
    {
        var len = htmlLowerCase.Length;

        // #RGB
        if (len == 4)
        {
            var r = Convert.ToInt32(htmlLowerCase.Substring(1, 1), 16);
            var g = Convert.ToInt32(htmlLowerCase.Substring(2, 1), 16);
            var b = Convert.ToInt32(htmlLowerCase.Substring(3, 1), 16);

            return Color.FromArgb(r + (r * 16), g + (g * 16), b + (b * 16));
        }

        // #RGBA
        else if (len == 5)
        {
            var r = Convert.ToInt32(htmlLowerCase.Substring(1, 1), 16);
            var g = Convert.ToInt32(htmlLowerCase.Substring(2, 1), 16);
            var b = Convert.ToInt32(htmlLowerCase.Substring(3, 1), 16);
            var a = Convert.ToInt32(htmlLowerCase.Substring(4, 1), 16);

            return Color.FromArgb(a + (a * 16), r + (r * 16), g + (g * 16), b + (b * 16));
        }

        // #RRGGBB
        else if (len == 7)
        {
            return Color.FromArgb(
                Convert.ToInt32(htmlLowerCase.Substring(1, 2), 16),
                Convert.ToInt32(htmlLowerCase.Substring(3, 2), 16),
                Convert.ToInt32(htmlLowerCase.Substring(5, 2), 16));
        }

        // #RRGGBBAA
        else if (len == 9)
        {
            return Color.FromArgb(
                Convert.ToInt32(htmlLowerCase.Substring(7, 2), 16),
                Convert.ToInt32(htmlLowerCase.Substring(1, 2), 16),
                Convert.ToInt32(htmlLowerCase.Substring(3, 2), 16),
                Convert.ToInt32(htmlLowerCase.Substring(5, 2), 16));
        }

        return Color.Empty;
    }

    private static Color ArgbToColor(string htmlLowerCase)
    {
        int left = htmlLowerCase.IndexOf('(');
        int right = htmlLowerCase.IndexOf(')');

        if (left < 0 || right < 0)
        {
            return Color.Empty;
        }

        string noBrackets = htmlLowerCase.Substring(left + 1, right - left - 1);

        string[] parts = noBrackets.Split(',');

        int r = int.Parse(parts[0], CultureInfo.InvariantCulture);
        int g = int.Parse(parts[1], CultureInfo.InvariantCulture);
        int b = int.Parse(parts[2], CultureInfo.InvariantCulture);

        if (parts.Length == 3)
        {
            return Color.FromArgb(r, g, b);
        }
        else if (parts.Length == 4)
        {
            float a = float.Parse(parts[3], CultureInfo.InvariantCulture);

            return Color.FromArgb((int)(a * 255), r, g, b);
        }

        return Color.Empty;
    }
}

测试:

    [TestMethod]
    public void ColorUtils_HtmlToColor()
    {
        Assert.AreEqual(Color.FromArgb(0x11, 0x22, 0x33), ColorUtils.HtmlToColor("#123"), "#123 (without alpha channel)");
        Assert.AreEqual(Color.FromArgb(0x11, 0x22, 0x33), ColorUtils.HtmlToColor("#112233"), "#112233 (without alpha channel)");
        Assert.AreEqual(Color.FromArgb(0x44, 0x11, 0x22, 0x33), ColorUtils.HtmlToColor("#1234"), "#1234 (alpha channel)");
        Assert.AreEqual(Color.FromArgb(0x44, 0x11, 0x22, 0x33), ColorUtils.HtmlToColor("#11223344"), "#11223344 (alpha channel)");
        Assert.AreEqual(Color.FromArgb(127, 11, 22, 33), ColorUtils.HtmlToColor("rgba(11,22,33,0.5)"), "rgba(11,22,33,0.5) (alpha channel)");
        Assert.AreEqual(Color.FromArgb(11, 22, 33), ColorUtils.HtmlToColor("rgb(11,22,33)"), "rgb(11,22,33) (alpha channel)");
        Assert.AreEqual(Color.Red, ColorUtils.HtmlToColor("red"), "red (named color)");
        Assert.AreEqual(Color.White, ColorUtils.HtmlToColor("white"), "white (named color)");
        Assert.AreEqual(Color.Blue, ColorUtils.HtmlToColor("blue"), "blue (named color)");
        Assert.AreEqual(Color.Empty, ColorUtils.HtmlToColor("invalid"), "invalid");
        Assert.AreEqual(Color.Empty, ColorUtils.HtmlToColor("#invalid"), "#invalid");
        Assert.AreEqual(Color.Empty, ColorUtils.HtmlToColor("rgb(invalid)"), "rgb(invalid)");
        Assert.AreEqual(Color.Empty, ColorUtils.HtmlToColor("rgba(invalid)"), "rgba(invalid)");
        Assert.AreEqual(Color.Empty, ColorUtils.HtmlToColor("rgba(invalid,invalid)"), "rgba(invalid,invalid)");
        Assert.AreEqual(Color.Empty, ColorUtils.HtmlToColor("rgb(11,22,333)"), "rgb(11,22,333) (value out of range)");
        Assert.AreEqual(Color.Empty, ColorUtils.HtmlToColor("rgba(11,22,333,0.5)"), "rgb(11,22,333,0.5) (value out of range)");
    }