Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a string HEX to color in Windows Phone Runtime c#

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.

like image 828
Edvin Avatar asked Feb 28 '15 13:02

Edvin


3 Answers

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);
like image 51
Rob Caplan - MSFT Avatar answered Nov 15 '22 21:11

Rob Caplan - MSFT


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.

like image 29
Yarik Avatar answered Nov 15 '22 19:11

Yarik


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/

like image 23
Mohammad Fazeli Avatar answered Nov 15 '22 19:11

Mohammad Fazeli