2

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?

Community
  • 1
  • 1
JustinHui
  • 729
  • 5
  • 18
  • I don't know much about *windows-store-app* . Isn't this available `var color = System.Drawing.ColorTranslator.FromHtml("red");` – I4V Sep 10 '13 at 22:39
  • No unfortunately it isn't ... or at least it is not in the same namespace. – JustinHui Sep 10 '13 at 23:07
  • If you're doing it frequently, you might just want to build and cache a dictionary like here: http://stackoverflow.com/questions/12751008/how-to-enumerate-through-colors-in-winrt – WiredPrairie Sep 11 '13 at 13:31

1 Answers1

5

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;
    }
}
Farhan Ghumra
  • 15,180
  • 6
  • 50
  • 115