Click here to Skip to main content
Click here to Skip to main content

Tagged as

Silverlight: Use KnownColor Through the Backdoor

, 12 Aug 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
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:
 
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).
 
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:
 
//--------------------------------------------------------------------------------
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.
 
//--------------------------------------------------------------------------------
// 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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

John Simmons / outlaw programmer
Software Developer (Senior)
United States United States
I've been paid as a programmer since 1982 with experience in Pascal, and C++ (both self-taught), and began writing Windows programs in 1991 using Visual C++ and MFC. In the 2nd half of 2007, I started writing C# Windows Forms and ASP.Net applications, and have since done WPF, Silverlight, WCF, web services, and Windows services.
 
My weakest point is that my moments of clarity are too brief to hold a meaningful conversation that requires more than 30 seconds to complete. Thankfully, grunts of agreement are all that is required to conduct most discussions without committing to any particular belief system.

Comments and Discussions

 
QuestionToo much code & work Pinmembersensboston30-Sep-14 17:54 
AnswerRe: Too much code & work PinmemberJohn Simmons / outlaw programmer1-Oct-14 8:12 
GeneralRe: Too much code & work Pinmembersensboston2-Oct-14 6:47 
GeneralRe 3rd edit. I suspect that that would be quicker than the p... PinmemberHenry Minute12-Aug-10 5:52 
GeneralReason for my vote of 1 Not a proper way to get the color fr... PinmvpKunalChowdhury8-Aug-10 8:25 
GeneralPost 2nd edit. Nifty! PinmemberHenry Minute6-Aug-10 1:22 
GeneralReason for my vote of 5 Thank you for sharing this tip. Pinmemberlinuxjr5-Aug-10 10:13 
GeneralYou have a typo in your code. Your method takes "nameOfColor... PinmemberYusuf5-Aug-10 8:28 
GeneralI'm guessing that XamlReader.Load() is bad for performance, ... PinmemberJörgen Sigvardsson5-Aug-10 8:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141220.1 | Last Updated 12 Aug 2010
Article Copyright 2010 by John Simmons / outlaw programmer
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid