JustinHui
JustinHui

Reputation: 729

How to parse an HTML color name to a SolidColorBrush in a Windows Store App

In .NET there is a class to cast a text HTML colour name to a Color (in this case "Red"):

Color col=(Color)ColorConverter.ConvertFromString("Red"); 
Brush brush=new SolidColorBrush(col);

(Which I took from here: Cast Color Name to SolidColorBrush)

This works for pretty much all the colours that can be found on wikipedia

Is there an equivalent class/library for Windows Store Apps that can do the same thing?

Upvotes: 2

Views: 904

Answers (1)

Farhan Ghumra
Farhan Ghumra

Reputation: 15296

Try this

using System.Reflection;

public SolidColorBrush ColorStringToBrush(string name)
{
    var property = typeof(Colors).GetRuntimeProperty(name);
    if (property != null)
    {
        return new SolidColorBrush((Color)property.GetValue(null));
    }
    else
    {
        return null;
    }
}

Upvotes: 5

Related Questions