8

I am working on a windows phone game, and I got stuck when I wanted to convert a HEX string into Color. On windows phone 8 silverlight it is not a problem but I cannot find a solution in runtime because it doesn't include Color.FromArgb, or Color.FromName functions.

Does somebody have a function that converts string HEX to Color?

Thanks.

Edvin
  • 117
  • 2
  • 10

8 Answers8

19

Color.FromArgb is in the Windows.UI namespace. There isn't a Color.FromName method, but you can use the Colors.< name > properties or you can use reflection to look up the name from a string.

using System.Reflection;     // For GetRuntimeProperty
using System.Globalization;  // For NumberStyles
using Windows.UI;            // for Color and Colors
using Windows.UI.Xaml.Media; // for SystemColorBrush

// from #AARRGGBB string
byte a = byte.Parse(hexColor.Substring(1, 2),NumberStyles.HexNumber);
byte r = byte.Parse(hexColor.Substring(3, 2),NumberStyles.HexNumber);
byte g = byte.Parse(hexColor.Substring(5, 2),NumberStyles.HexNumber);
byte b = byte.Parse(hexColor.Substring(7, 2),NumberStyles.HexNumber);

Windows.UI.Color color = Color.FromArgb(a, r, g, b);
Windows.UI.Xaml.Media.SolidColorBrush br = new SolidColorBrush(color);

// From Name
var prop = typeof(Windows.UI.Colors).GetRuntimeProperty("Aqua");
if (prop != null)
{
    Color c = (Color) prop.GetValue(null);
    br = new SolidColorBrush(c);
}

// From Property
br = new SolidColorBrush(Colors.Aqua);
Rob Caplan - MSFT
  • 21,714
  • 3
  • 32
  • 54
15

Just in case someone is looking for a better alternative. In Universal Windows Platform (Windows 10), there is XamlBindingHelper.ConvertValue, which is much better than nothing.

// Get a Color instance representing #FFFF0000.
var color = XamlBindingHelper.ConvertValue(typeof(Windows.UI.Color), "red");

It can convert enums from Windows.UI.Xaml namespace, booleans, brushes, colors and other simple stuff XAML parser is able to do.

Yarik
  • 1,423
  • 1
  • 14
  • 16
  • Thanks this works perfectly in UWP - I haven't checked performance compared to the other GetColorFromHex methods posted but the difference is probably so negligible I'll use the above by default – Dan Harris Jan 23 '17 at 15:30
8

Converting Hex to Color in C# for Universal Windows Platform (UWP)

Create a method to Convert Hex string to SolidColorBrush:

  public SolidColorBrush GetSolidColorBrush(string hex)
    {
        hex = hex.Replace("#", string.Empty);
        byte a = (byte)(Convert.ToUInt32(hex.Substring(0, 2), 16));
        byte r = (byte)(Convert.ToUInt32(hex.Substring(2, 2), 16));
        byte g = (byte)(Convert.ToUInt32(hex.Substring(4, 2), 16));
        byte b = (byte)(Convert.ToUInt32(hex.Substring(6, 2), 16));
        SolidColorBrush myBrush = new SolidColorBrush(Windows.UI.Color.FromArgb(a, r, g, b));
        return myBrush;
    }

Now all that left is to get the color by Calling the method and pass the hex string to it as parameter:

  var color = GetSolidColorBrush("#FFCD3927").Color;  

Reference: http://www.joeljoseph.net/converting-hex-to-color-in-universal-windows-platform-uwp/

Mohammad Fazeli
  • 648
  • 10
  • 13
3

I made a method from the top answer here:

private Windows.UI.Color GetColorFromHex(string hexString)
{
    hexString = hexString.Replace("#", string.Empty);
    byte r = byte.Parse(hexString.Substring(0, 2), NumberStyles.HexNumber);
    byte g = byte.Parse(hexString.Substring(2, 2), NumberStyles.HexNumber);
    byte b = byte.Parse(hexString.Substring(4, 2), NumberStyles.HexNumber);

    return Windows.UI.Color.FromArgb(byte.Parse("1"), r, g, b);
}
JKennedy
  • 18,150
  • 17
  • 114
  • 198
  • found this question again, tried to use the first answer (not seeing my own answer) and found it was breaking because i wasnt using alpha in my hex code. Then saw my answer as second. If you are not using alpha in your hex code use this answer – JKennedy Jul 21 '16 at 13:44
2

Here is an easy to use code snippet

public Color HexColor(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 Color.FromArgb(a, r, g, b);
}

and you can simply call like the below one

Color c = HexColor("#99ccff");

I tested this in winphone 8.1 and it works

Reference

Durai Amuthan.H
  • 31,670
  • 10
  • 160
  • 241
1

generete a file namely: ColorUtils.cs

using Windows.UI.Xaml.Markup;
....
 static class ColorUtils
    {
        public static Color GetColorFromHex(string hexString)
        {
            Color x =(Color)XamlBindingHelper.ConvertValue(typeof(Color), hexString);
            return x;
        }
    }

and use it something like

var acolor=(new SolidColorBrush(ColorUtils.GetColorFromHex("#F24C27")):

or with alpha

var acolor=(new SolidColorBrush(ColorUtils.GetColorFromHex("#4CF24C27")):
Zen Of Kursat
  • 2,672
  • 1
  • 31
  • 47
0

Final solution, that works as well on UWP:

 public static Color GetColorFromHex(string hexString) {
            //add default transparency to ignore exception
            if ( !string.IsNullOrEmpty(hexString) && hexString.Length > 6 ) {
                if ( hexString.Length == 7 ) {
                    hexString = "FF" + hexString;
                }

                hexString = hexString.Replace("#", string.Empty);
                byte a = (byte) ( Convert.ToUInt32(hexString.Substring(0, 2), 16) );
                byte r = (byte) ( Convert.ToUInt32(hexString.Substring(2, 2), 16) );
                byte g = (byte) ( Convert.ToUInt32(hexString.Substring(4, 2), 16) );
                byte b = (byte) ( Convert.ToUInt32(hexString.Substring(6, 2), 16) );
                Color color = Color.FromArgb(a, r, g, b);
                return color;
            }

            //return black if hex is null or invalid
            return Color.FromArgb(255, 0, 0, 0);
        }
Choletski
  • 7,074
  • 6
  • 43
  • 64
  • Does this work? Shouldn't the # be removed first an then `hexString = "FF" + hexString` instead of `+=`? – Thaoden Mar 12 '17 at 12:08
-1

You can use

var color = new SolidColorBrush(Color.FromArgb(0xFF, 0xFF, 0, 0));
Med.Amine.Touil
  • 1,225
  • 2
  • 11
  • 15