How to get System.Windows.Media.Color From From HEX Windows 8 Application
Asked Answered
K

2

9

I want to set Border background color from web color value in my windows 8 mobile application .

I found one method that convert hex to Argb but its not working for me ..

  private System.Windows.Media.Color FromHex(string hex)
        {
            string colorcode = hex;
            int argb = Int32.Parse(colorcode.Replace("#", ""), System.Globalization.NumberStyles.HexNumber);
            return System.Windows.Media.Color.FromArgb((byte)((argb & -16777216) >> 0x18),
                                  (byte)((argb & 0xff0000) >> 0x10),
                                  (byte)((argb & 0xff00) >> 8),
                                  (byte)(argb & 0xff));


        }

I am using above method like..

     Border borderCell = new Border();
     var col = FromHex("#DD4AA3");
     var color =new System.Windows.Media.SolidColorBrush(col);
     borderCell.Background = color;

But if I pass color hex value like below

            var col = FromHex("#FFEEDDCC");

its works fine but it not work on my hex color value.

Before posting this question I go thru this stack answer. How to get Color from Hexadecimal color code using .NET?

Convert System.Drawing.Color to RGB and Hex Value

Keats answered 25/3, 2015 at 8:34 Comment(2)
I cannot understand your this line its works fine but it not work on my hex color value. what does it mean "It not work on my hex color value."? the second thing is that you are supplying alpha value in second example #FFEEDDCC. make sure that your method converts hex value if it has been supplied with alpha value (first two characters)Monadism
The System.Windows.Media.ColorConverter class is normally helpful to make the conversion. But it is not available on a phone so you basically need to duplicate this code.Six
K
12

finally I found one method that return color from hex string

 public System.Windows.Media.Color ConvertStringToColor(String hex)
    {
        //remove the # at the front
        hex = hex.Replace("#", "");

        byte a = 255;
        byte r = 255;
        byte g = 255;
        byte b = 255;

        int start = 0;

        //handle ARGB strings (8 characters long)
        if (hex.Length == 8)
        {
            a = byte.Parse(hex.Substring(0, 2), System.Globalization.NumberStyles.HexNumber);
            start = 2;
        }

        //convert RGB characters to bytes
        r = byte.Parse(hex.Substring(start, 2), System.Globalization.NumberStyles.HexNumber);
        g = byte.Parse(hex.Substring(start + 2, 2), System.Globalization.NumberStyles.HexNumber);
        b = byte.Parse(hex.Substring(start + 4, 2), System.Globalization.NumberStyles.HexNumber);

        return System.Windows.Media.Color.FromArgb(a, r, g, b);
    }
Keats answered 25/3, 2015 at 11:21 Comment(1)
Your effort totally worth it !!Sideway
O
22

Why not simply use System.Windows.Media.ColorConverter?

Color color = (Color)System.Windows.Media.ColorConverter.ConvertFromString("#EA1515");
Owades answered 24/3, 2016 at 17:28 Comment(3)
Because this question about Windows-phone not related to WPF.Keats
Bah humbug. I misread the article too and found exactly what i was looking for here. :)Algebraist
Perfect! Searching for this for sometime !!Famished
K
12

finally I found one method that return color from hex string

 public System.Windows.Media.Color ConvertStringToColor(String hex)
    {
        //remove the # at the front
        hex = hex.Replace("#", "");

        byte a = 255;
        byte r = 255;
        byte g = 255;
        byte b = 255;

        int start = 0;

        //handle ARGB strings (8 characters long)
        if (hex.Length == 8)
        {
            a = byte.Parse(hex.Substring(0, 2), System.Globalization.NumberStyles.HexNumber);
            start = 2;
        }

        //convert RGB characters to bytes
        r = byte.Parse(hex.Substring(start, 2), System.Globalization.NumberStyles.HexNumber);
        g = byte.Parse(hex.Substring(start + 2, 2), System.Globalization.NumberStyles.HexNumber);
        b = byte.Parse(hex.Substring(start + 4, 2), System.Globalization.NumberStyles.HexNumber);

        return System.Windows.Media.Color.FromArgb(a, r, g, b);
    }
Keats answered 25/3, 2015 at 11:21 Comment(1)
Your effort totally worth it !!Sideway

© 2022 - 2024 — McMap. All rights reserved.