2016-07-21 75 views
3

在我的硒代碼中,我需要驗證顏色代碼是#192856的背景。但是當我得到那個元素的CSS屬性時,它給了我rgba格式的顏色。現在我需要獲取十六進制值本身的值。我怎樣才能做到這一點?如何將RGBA css顏色格式轉換爲十六進制格式

quickLinkstab.GetCssValue("background-color") 

上面給出了rgba值「rgba(25,40,86,1)」的值。有什麼辦法可以將它轉換回十六進制?或者我可以在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爲「一」,但cssvalue爲1

我更期待對越來越十六進制數值直接或如果沒有可能再轉換RGBA爲十六進制的解決方案。

+0

我想'了'是不透明,它應該是'0'和'1'之間的值,所以它應該是類型'double' –

+0

「一「是爲了透明。 0 =不可見,255 =可見。如果你只有RGB,則A始終爲255 https://msdn.microsoft.com/en-us/library/at1k42eh(v=vs.110).aspx –

+0

[轉換RGBA以HEX]可能的複製(HTTP ://stackoverflow.com/questions/21576092/convert-rgba-to-hex) – JeffC

回答

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(); 
    } 
} 
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);

5

下面的代碼覆蓋十六進制格式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); 
    } 
} 
+0

工程太棒了!很好,單元測試! – Julian

相關問題