I am encountering a problem which is how do I convert input strings like \"RED\" to the actual Color type Color.Red in C#. Is there a good way to do this?
The following can generate a color from name, hex, or known name.
Color beige = StringToColor("Beige");
Color purple = StringToColor("#800080");
Color window = StringToColor("Window");
public static Color StringToColor(string colorStr)
{
TypeConverter cc = TypeDescriptor.GetConverter(typeof(Color));
var result = (Color)cc.ConvertFromString(colorStr);
return result;
}
The snippet was taken from Jo Albahari's C# in a Nutshell.