locked
Converting a String into a Color

    Question

  • I am trying to find a way to convert a string (such as "Red", "Blue", "Green", etc.) into a Color in my Windows Store app. I have seen methods such as Color.FromName, but everything I have found seems to be in the System.Drawing namespace, and I have been unable to find the System.Drawing namespace. Is this namespace now gone? Am I somehow missing it? Or, is there another way to convert a string into a Color? Thanks.

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Thursday, May 8, 2014 5:17 PM

Answers

  • I also just got this from Rob Caplan: is reflection to go over the colors in the Windows.UI.Colors structure:

    foreach (var prop in typeof(Colors).GetRuntimeProperties())
                {
                    brushes.Add(new SolidColorBrush((Color)prop.GetValue(null)));
                }


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Thursday, May 8, 2014 5:54 PM
    Moderator
  • Hi, you can try this:

           

    using System.Reflection;

    private Color ColorFromName(string name) { var type = typeof(Colors).GetTypeInfo(); var prop = type.GetDeclaredProperty(name); if (null == prop) { return Colors.Transparent; } else { return (Color)prop.GetValue(null); } }

    use ColorFromName("Green") you will get green color


    Friday, May 9, 2014 9:55 AM

All replies

  • You're right that System.Drawing is not in Windows Store apps. I don't see how to do this natively, but here's a crazy idea... find a javscript library that does this, run the code inside a hidden webview, and get the value.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Thursday, May 8, 2014 5:47 PM
    Moderator
  • I also just got this from Rob Caplan: is reflection to go over the colors in the Windows.UI.Colors structure:

    foreach (var prop in typeof(Colors).GetRuntimeProperties())
                {
                    brushes.Add(new SolidColorBrush((Color)prop.GetValue(null)));
                }


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Thursday, May 8, 2014 5:54 PM
    Moderator
  • Hi, you can try this:

           

    using System.Reflection;

    private Color ColorFromName(string name) { var type = typeof(Colors).GetTypeInfo(); var prop = type.GetDeclaredProperty(name); if (null == prop) { return Colors.Transparent; } else { return (Color)prop.GetValue(null); } }

    use ColorFromName("Green") you will get green color


    Friday, May 9, 2014 9:55 AM