Pradeep devendran
Pradeep devendran

Reputation: 489

How to Convert Hexdecimal code to color in windows 8

I have code Hexadecimal code "FFB800" and I needed to convert to "Color" in WinRT.

Thanks in Advance.

Upvotes: 0

Views: 2809

Answers (4)

Filip Skakun
Filip Skakun

Reputation: 31724

The short way to do it in a tweet:

(Color)XamlReader.Load(string.Format("<Color xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation …\">{0}</Color>", c));

The recommended way is to get WinRT XAML Toolkit from NuGet and call

WinRTXamlToolkit.Imaging.ColorExtensions.FromString(c);

This runs way faster than using XamlReader, so it is recommended if you need to call it more than once. You can also clone it from GitHub or copy and paste from here:

#region FromString()
/// <summary>
/// Returns a Color based on XAML color string.
/// </summary>
/// <param name="c">The color string. Any format used in XAML should work.</param>
/// <returns></returns>
public static Color FromString(string c)
{
    if (string.IsNullOrEmpty(c))
        throw new ArgumentException("Invalid color string.", "c");

    if (c[0] == '#')
    {
        switch (c.Length)
        {
            case 9:
            {
                //var cuint = uint.Parse(c.Substring(1), NumberStyles.HexNumber);
                var cuint = Convert.ToUInt32(c.Substring(1), 16);
                var a = (byte)(cuint >> 24);
                var r = (byte)((cuint >> 16) & 0xff);
                var g = (byte)((cuint >> 8) & 0xff);
                var b = (byte)(cuint & 0xff);

                return Color.FromArgb(a, r, g, b);
            }
            case 7:
            {
                var cuint = Convert.ToUInt32(c.Substring(1), 16);
                var r = (byte)((cuint >> 16) & 0xff);
                var g = (byte)((cuint >> 8) & 0xff);
                var b = (byte)(cuint & 0xff);

                return Color.FromArgb(255, r, g, b);
            }
            case 5:
            {
                var cuint = Convert.ToUInt16(c.Substring(1), 16);
                var a = (byte)(cuint >> 12);
                var r = (byte)((cuint >> 8) & 0xf);
                var g = (byte)((cuint >> 4) & 0xf);
                var b = (byte)(cuint & 0xf);
                a = (byte)(a << 4 | a);
                r = (byte)(r << 4 | r);
                g = (byte)(g << 4 | g);
                b = (byte)(b << 4 | b);

                return Color.FromArgb(a, r, g, b);
            }
            case 4:
            {
                var cuint = Convert.ToUInt16(c.Substring(1), 16);
                var r = (byte)((cuint >> 8) & 0xf);
                var g = (byte)((cuint >> 4) & 0xf);
                var b = (byte)(cuint & 0xf);
                r = (byte)(r << 4 | r);
                g = (byte)(g << 4 | g);
                b = (byte)(b << 4 | b);

                return Color.FromArgb(255, r, g, b);
            }
            default:
                throw new FormatException(string.Format("The {0} string passed in the c argument is not a recognized Color format.", c));
        }
    }
    else if (
        c.Length > 3 &&
        c[0] == 's' &&
        c[1] == 'c' &&
        c[2] == '#')
    {
        var values = c.Split(',');

        if (values.Length == 4)
        {
            var scA = double.Parse(values[0].Substring(3));
            var scR = double.Parse(values[1]);
            var scG = double.Parse(values[2]);
            var scB = double.Parse(values[3]);

            return Color.FromArgb(
                (byte)(scA * 255),
                (byte)(scR * 255),
                (byte)(scG * 255),
                (byte)(scB * 255));
        }
        else if (values.Length == 3)
        {
            var scR = double.Parse(values[0].Substring(3));
            var scG = double.Parse(values[1]);
            var scB = double.Parse(values[2]);

            return Color.FromArgb(
                255,
                (byte)(scR * 255),
                (byte)(scG * 255),
                (byte)(scB * 255));
        }
        else
        {
            throw new FormatException(string.Format("The {0} string passed in the c argument is not a recognized Color format (sc#[scA,]scR,scG,scB).", c));
        }
    }
    else
    {
        var prop = typeof(Colors).GetTypeInfo().GetDeclaredProperty(c);
        return (Color)prop.GetValue(null);
    }
}
#endregion

Upvotes: 3

Ankush Madankar
Ankush Madankar

Reputation: 3834

Try this:

public struct MyColor : Windows.UI.Color
{
    /// <summary>
    /// Convert hexdecimal value into color.
    /// </summary>
    /// <param name="hexCode">hexdecimal of color.</param>
    /// <returns></returns>
    public Windows.UI.Xaml.Media.Brush ColorToBrush(string hexCode)
    {
        hexCode = hexCode.Replace("#", "");

        if (hexCode.Length == 6)
            return new Windows.UI.Xaml.Media.SolidColorBrush(Windows.UI.ColorHelper.FromArgb(255,
                byte.Parse(hexCode.Substring(0, 2), System.Globalization.NumberStyles.HexNumber),
                byte.Parse(hexCode.Substring(2, 2), System.Globalization.NumberStyles.HexNumber),
                byte.Parse(hexCode.Substring(4, 2), System.Globalization.NumberStyles.HexNumber)));
        else if (hexCode.Length == 8)
        {
            var color = new Windows.UI.Color();
            color.A = byte.Parse(hexCode.Substring(0, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
            color.R = byte.Parse(hexCode.Substring(2, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
            color.G = byte.Parse(hexCode.Substring(4, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
            color.B = byte.Parse(hexCode.Substring(6, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
        }

        return null;
    }
}

Upvotes: 0

Tim
Tim

Reputation: 11

        var hexCode = "#FFFFB800";
        var color = new Color();
        color.A = byte.Parse(hexCode.Substring(7, 2), NumberStyles.AllowHexSpecifier);
        color.R = byte.Parse(hexCode.Substring(1, 2), NumberStyles.AllowHexSpecifier);
        color.G = byte.Parse(hexCode.Substring(3, 2), NumberStyles.AllowHexSpecifier);
        color.B = byte.Parse(hexCode.Substring(5, 2), NumberStyles.AllowHexSpecifier);

how to set as fill for a xaml rectangle object

rect.Fill = new SolidColorBrush(color);

the other solution like this one works but returns the Parameters out of order if you have only a 6 digit hex rather than the full 8 simply set the a to 255

Upvotes: 1

Hutjepower
Hutjepower

Reputation: 1261

What is the purpose of the question? Is it an option to do this in plain XAML? XAML does take Hexadecimal codes.

    <Grid Background="#FFB800">

Otherwise in code-behind I've used more or less the following in a Windows 8 App:

    var hexCode = "#FFFFB800";
    var color = new Color();
    color.A = byte.Parse(hexCode.Substring(1, 2), NumberStyles.AllowHexSpecifier);
    color.R = byte.Parse(hexCode.Substring(3, 2), NumberStyles.AllowHexSpecifier);
    color.G = byte.Parse(hexCode.Substring(5, 2), NumberStyles.AllowHexSpecifier);
    color.B = byte.Parse(hexCode.Substring(7, 2), NumberStyles.AllowHexSpecifier);

Upvotes: 5

Related Questions