13

I think that the title is clear !

What I have now is :

System.Drawing.Color uiui = System.Drawing.ColorTranslator.FromHtml(myString);
var intColor = (uint)((uiui.A << 24) | (uiui.R << 16) | (uiui.G << 8) | (uiui.B << 0));
var bytes = BitConverter.GetBytes(uint.Parse(value));
var brush = new SolidColorBrush();
brush.Color = Color.FromArgb(bytes[3], bytes[2], bytes[1], bytes[0]);

1- myString is like #FFFFFF like I said in the title

2- This fails on the BitConverter.GetBytes line which surprises me cause I got the int representation on my Color !

3- Anyway, I know that COlor conversion are not that intuitive but I feel like I'm not doing it right... Is that the good way ?

Guillaume Slashy
  • 3,554
  • 8
  • 43
  • 68
  • possible duplicate of [How to get Color from Hex color code using .NET?](http://stackoverflow.com/questions/2109756/how-to-get-color-from-hex-color-code-using-net) – Ray Feb 07 '12 at 15:24
  • I always assumed that 0xFFFFFF could easily be translated to RGB by taking 16 bytes for each. In other words 0XFF, 0XFF, and 0XFF. – Security Hound Feb 07 '12 at 15:30
  • the answer for that question was specific to WPF Media Color, I'd say it's not truly a duplicate – Anthony Shaw Feb 07 '12 at 15:32
  • @AnthonyShaw the step from Media.Color to Media.SolidColorBrush is trivial, but added an answer anyway. – Ray Feb 07 '12 at 15:42
  • The difference was more about the string representation difference – Guillaume Slashy Feb 07 '12 at 15:49

3 Answers3

36

You can use the System.Windows.Media.ColorConverter

var color = (Color)ColorConverter.ConvertFromString("#FF010203");
//OR
var color = (Color)ColorConverter.ConvertFromString("#010203");
//OR
var color = (Color)ColorConverter.ConvertFromString("Red");

//and then:
var brush = new SolidColorBrush(color);  

It's pretty flexible as to what it accepts. Have a look at the examples in XAML here. You can pass any of those string formats in.

Note: These are all in System.Windows.Media (for WPF) not to be confused with System.Drawing (for WinForms)

Ray
  • 45,695
  • 27
  • 126
  • 169
  • Worked fine with this code for my example : var color = (Color)ColorConverter.ConvertFromString("#FF" + myString.Substring(1)); – Guillaume Slashy Feb 07 '12 at 15:48
  • 1
    @GuillaumeSlashy you shouldn't have to do that, it's pretty smart. Just give it your string if it's #XXXXXX – Ray Feb 07 '12 at 15:56
2

This is the helper class that I've used in the past

    public static Color HexStringToColor(string hexColor)
    {
        string hc = ExtractHexDigits(hexColor);
        if (hc.Length != 6)
        {
            // you can choose whether to throw an exception
            //throw new ArgumentException("hexColor is not exactly 6 digits.");
            return Color.Empty;
        }
        string r = hc.Substring(0, 2);
        string g = hc.Substring(2, 2);
        string b = hc.Substring(4, 2);
        Color color = Color.Empty;
        try
        {
            int ri = Int32.Parse(r, NumberStyles.HexNumber);
            int gi = Int32.Parse(g, NumberStyles.HexNumber);
            int bi = Int32.Parse(b, NumberStyles.HexNumber);
            color = Color.FromArgb(ri, gi, bi);
        }
        catch
        {
            // you can choose whether to throw an exception
            //throw new ArgumentException("Conversion failed.");
            return Color.Empty;
        }
        return color;
    }

and an additional helper class

        public static string ExtractHexDigits(string input)
        {
            // remove any characters that are not digits (like #)
            var isHexDigit
                = new Regex("[abcdefABCDEF\\d]+", RegexOptions.Compiled);
            string newnum = "";
            foreach (char c in input)
            {
                if (isHexDigit.IsMatch(c.ToString()))
                {
                    newnum += c.ToString();
                }
            }
            return newnum;
        }
Anthony Shaw
  • 8,146
  • 4
  • 44
  • 62
0

Just use ColorTranslator method:

ColorTranslator.FromHtml("#010203");
DiSaSteR
  • 608
  • 6
  • 11