Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Silverlight: Use KnownColor Through the Backdoor

0.00/5 (No votes)
12 Aug 2010 1  
Get around another irrational and arbitrary omission in Silverlight
Look at the 3rd edit for my final solution (I left the existing stuff here so you could see the different ways to approach the problem).

------------------------

I have been puzzling over an annoyance in Silverlight involving the use of known color values. In XAML, you can set a color property with one of dozens of colors - by name. However, that same list of colors isn't available (that I could find) from the code-behind (I hate the word "code-behind") unless you coded it up yourself.

I eventually came up with an idea about how to do it, and it might be ugly, but it's certainly less hassle than writing your own KnownColor enumeration. Using the XamlLoader, simply create a xaml string that creates a UIElement, set one of the element's color properties to the specified color, and then read the appropriate property. I know, if you specify a color that doesn't exist, an exception will be thrown, but hey, some forethought in the code will get around that issue. Here's the method:

C#
private Color GetColorByName(string nameOfColor)
{
    // Set a default color.
    Color color = Colors.Black;

    try
    {
        // I create a formatted string because I like formatted strings 
        // (and it saved some typing). We're going to create a 
        // rectange, set it's fill property to the specified color and 
        // see what happens.
	string xaml = string.Format("<Rectangle xmlns=\"{0}/presentation\" xmlns:x=\"{0}\" Fill=\"{1}\" />", 
	"http://schemas.microsoft.com/winfx/2006/xaml", nameOfColor);

        // Load the xaml - this line will throw an exception if an 
        // invalid color is specified, but that's okay - we're just 
        // going to eat the exception.  Note: the color doesn't have to 
        // be capitalized, but it DOES have to be spelled correctly.
        Rectangle rect = (Rectangle)XamlReader.Load(xaml);
        color = (Color)rect.Fill.GetValue(SolidColorBrush.ColorProperty);
    }
    catch
    {
        // Yummy!
    }

    // Return the resulting color (it will be the default color if an 
    // exception was thrown.
    return color;
}


The method above will return Colors.Black if you specified the color "blah", or Colors.Red if you specified "Red" (or "red").

EDIT #1 - I realize that using XamlReader is inefficient, but it's convenient, and as someone else already mentioned, you could easily build a cache of colors already "found" so you don't have to hammer the system with XamlReader. This technique is merely one approach to actually getting the values.

EDIT #2 - You could (and actually should) also use reflection, like so:

// put this code in the try block above
Type colorType = (typeof(System.Windows.Media.Colors));
if (colorType.GetProperty(rgbColor) != null)
{
    object obj = colorType.InvokeMember(rgbColor, BindingFlags.GetProperty, null, null, null);
    if (obj != null)
    {
        color = (Color)obj;
    }
 }


Using reflection is 500 times faster (no kidding) than using XamlReader. I tried 100,000 iterations using both, and XamlReader took 5.6 seconds to finish, while using reflection took just 0.7 seconds. I'd say that using reflection is the better approach with one really big exception - the method above only returns one of the colors we can already get to. In other words, it's pointless.

Edit #3 -------------------

I really wasn't happy with my solution, so I bit the bullet, and did the following. First, I defined an enumerator with the appropriate names/values. I want apologize ahead of time for the formatting of this code block, but listing each color on its own line would have made the tip WAY to long (as if it wasn't already).

C#
public enum KnownColors : uint {
AliceBlue=0xFFF0F8FF,AntiqueWhite=0xFFFAEBD7,Aqua=0xFF00FFFF,
Aquamarine=0xFF7FFFD4,Azure=0xFFF0FFFF,Beige=0xFFF5F5DC,
Bisque=0xFFFFE4C4,Black=0xFF000000,BlanchedAlmond=0xFFFFEBCD,
Blue=0xFF0000FF,BlueViolet=0xFF8A2BE2,Brown=0xFFA52A2A, 
BurlyWood=0xFFDEB887,CadetBlue=0xFF5F9EA0,Chartreuse=0xFF7FFF00,
Chocolate=0xFFD2691E,Coral=0xFFFF7F50,CornflowerBlue=0xFF6495ED,
Cornsilk=0xFFFFF8DC,Crimson=0xFFDC143C,Cyan=0xFF00FFFF,
DarkBlue=0xFF00008B,DarkCyan=0xFF008B8B,DarkGoldenrod=0xFFB8860B,
DarkGray=0xFFA9A9A9,DarkGreen=0xFF006400,DarkKhaki=0xFFBDB76B,
DarkMagenta=0xFF8B008B,DarkOliveGreen=0xFF556B2F,DarkOrange=0xFFFF8C00,
DarkOrchid=0xFF9932CC,DarkRed=0xFF8B0000,DarkSalmon=0xFFE9967A,
DarkSeaGreen=0xFF8FBC8F,DarkSlateBlue=0xFF483D8B,DarkSlateGray=0xFF2F4F4F,
LightSalmon=0xFFFFA07A,LightSeaGreen=0xFF20B2AA,LightSkyBlue=0xFF87CEFA,
LightSlateGray=0xFF778899,LightSteelBlue=0xFFB0C4DE,LightYellow=0xFFFFFFE0,
Lime=0xFF00FF00,LimeGreen=0xFF32CD32,Linen=0xFFFAF0E6,
Magenta=0xFFFF00FF,Maroon=0xFF800000,MediumAquamarine=0xFF66CDAA,
MediumBlue=0xFF0000CD,MediumOrchid=0xFFBA55D3,MediumPurple=0xFF9370DB,
MediumSeaGreen=0xFF3CB371,MediumSlateBlue=0xFF7B68EE,MediumSpringGreen=0xFF00FA9A,
MediumTurquoise=0xFF48D1CC,MediumVioletRed=0xFFC71585,MidnightBlue=0xFF191970,
MintCream=0xFFF5FFFA,MistyRose=0xFFFFE4E1,Moccasin=0xFFFFE4B5,
NavajoWhite=0xFFFFDEAD,Navy=0xFF000080,OldLace=0xFFFDF5E6,
Olive=0xFF808000,OliveDrab=0xFF6B8E23,Orange=0xFFFFA500,
OrangeRed=0xFFFF4500,Orchid=0xFFDA70D6,PaleGoldenrod=0xFFEEE8AA,
PaleGreen=0xFF98FB98,PaleTurquoise=0xFFAFEEEE,PaleVioletRed=0xFFDB7093,
PapayaWhip=0xFFFFEFD5,PeachPuff=0xFFFFDAB9,Peru=0xFFCD853F,
Pink=0xFFFFC0CB,Plum=0xFFDDA0DD,PowderBlue=0xFFB0E0E6,
Purple=0xFF800080,Red=0xFFFF0000,RosyBrown=0xFFBC8F8F,
RoyalBlue=0xFF4169E1,SaddleBrown=0xFF8B4513,Salmon=0xFFFA8072,
SandyBrown=0xFFF4A460,SeaGreen=0xFF2E8B57,SeaShell=0xFFFFF5EE,
Sienna=0xFFA0522D,Silver=0xFFC0C0C0,SkyBlue=0xFF87CEEB,
SlateBlue=0xFF6A5ACD,SlateGray=0xFF708090,Snow=0xFFFFFAFA,
SpringGreen=0xFF00FF7F,SteelBlue=0xFF4682B4,Tan=0xFFD2B48C,
Teal=0xFF008080,Thistle=0xFFD8BFD8,Tomato=0xFFFF6347,
Transparent=0x00FFFFFF,Turquoise=0xFF40E0D0,Violet=0xFFEE82EE,
Wheat=0xFFF5DEB3,White=0xFFFFFFFF,WhiteSmoke=0xFFF5F5F5,
Yellow=0xFFFFFF00,YellowGreen=0xFF9ACD32};
Next, I used some code from a previous tip I posted that allows nme to retrieve an enum value using a string:

C#
//--------------------------------------------------------------------------------
public static T StringToEnum<T>(string value, T defaultValue)
{
	T enumValue = (Enum.IsDefined(typeof(T), value)) ? (T)Enum.Parse(typeof(T), value, true) : defaultValue;
	return enumValue;
}
And finally, I wrote the method that combines the enum and the enum retrieval method to get an actual color.

C#
//--------------------------------------------------------------------------------
// call this method
public static Color FromColorName(string rgbColor)
{
	uint color = (uint)StringToEnum(rgbColor, KnownColors.Black);
	return Color.FromArgb((byte)(color >> 24),
                              (byte)(color >> 16),
                              (byte)(color >> 8),
                              (byte)color);
}
While a little "bulkier" in its setup, this really is ultimately the best way to perform the task at hand. You could also create an extension method for the enum that does all the stuff I did in the two separate methods if that blows up your skirt.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here