So, Silverlight does not have a Brushes class. I know that the reason given for this is to reduce the size of the SL plugin download, but the XAML reader recognizes 141 colors by name, so I'm not sure why these can't be exposed without significantly increasing
the code size.
In the meantime, those of us who have need of a bigger crayon box in code can use the following method. For the size conscious, the full list of colors will add an additional 3.5KB to your assembly, but you can trim as needed (I suggest commenting out colors
rather than removing them so that they'll still be there if you need them later).
First, you'll need the following method. Stick it somewhere useful [;)]
static public Color FromName(ColorNames color)
{
uint value = (uint)color;
Second, you'll need the ColorNames enumeration listed below. I believe these are all the colors recognized by the Silverlight XAML parser (I might have missed one or two, but I don't think so). Comment the ones you don't need or remove them entirely (but
you might want to keep a backup). Then, to get the color you want just use FromName, as in
SolidColorBrush brush =
newSolidColorBrush(FromName(ColorNames.Firebrick));
Thanks. I was very surprised that DarkSlateGray wasn't available to me in code. You've helped me a lot. By the way, I find that a convenient place to stick the FromName method is as an extension to the ColorName enum, like this..
public static class ColorNamesExtensions
{
// thanks to Jabb...
// http://silverlight.net/forums/t/13225.aspx
public static Color FromName(this ColorNames color)
{
uint value = (uint)color;
return Color.FromArgb(
(byte)(value >> 24),
(byte)(value >> 16),
(byte)(value >> 8),
(byte)value
);
}
}
I just put that definition right beside the definition of the enum and then I can create colors like this...
private Brush _Stroke = new SolidColorBrush(ColorNames.DarkSlateGray.FromName());
Yeah, it would have been nice if C# were more orthogonal and enum's had allowed implicit conversion operators to be defined, but at least they allow extensions. That really bothered me, but I was finally able to restructure the code so that it looks as though
the color names were an enum with an implicit conversion. That is, I can write...
private Brush _Stroke = new SolidColorBrush(ColorName.DarkGray);
Or I can even write...
private Brush _Stroke = ColorName.DarkGray;
The way I did it was to turn the enum into a struct and define all the colors as static readonly values of the struct. Like this (pardon my old-style preferences for braces).
public struct ColorName {
// thanks to Jabb's blog
// http://silverlight.net/forums/t/13225.aspx
// for the code from which this evolved and
// especially for all the color definitions!
public static implicit operator Color(ColorName color) {
uint value = color;
return Color.FromArgb(
(byte)(value >> 24),
(byte)(value >> 16),
(byte)(value >> 8),
(byte)value
);
}
public static implicit operator Brush(ColorName color) {
return new SolidColorBrush(color);
}
public static implicit operator uint(ColorName color) {
return color.ColorValue;
}
public static implicit operator ColorName(uint color) {
return new ColorName(color);
}
private uint ColorValue;
public ColorName(uint color) {
ColorValue = color;
}
public static readonly ColorName AliceBlue = 0xFFF0F8FF;
public static readonly ColorName AntiqueWhite = 0xFFFAEBD7;
public static readonly ColorName Aqua = 0xFF00FFFF;
public static readonly ColorName Aquamarine = 0xFF7FFFD4;
// ... and so on ...
}
Jabb
Member
474 Points
82 Posts
Colors in Silverlight: I need a bigger box of crayons!
Apr 01, 2008 04:47 PM | LINK
So, Silverlight does not have a Brushes class. I know that the reason given for this is to reduce the size of the SL plugin download, but the XAML reader recognizes 141 colors by name, so I'm not sure why these can't be exposed without significantly increasing the code size.
In the meantime, those of us who have need of a bigger crayon box in code can use the following method. For the size conscious, the full list of colors will add an additional 3.5KB to your assembly, but you can trim as needed (I suggest commenting out colors rather than removing them so that they'll still be there if you need them later).
First, you'll need the following method. Stick it somewhere useful [;)]
static public Color FromName(ColorNames color)
{
uint value = (uint)color;
return Color.FromArgb(
(byte)(value >> 24),
(byte)(value >> 16),
(byte)(value >> 8),
(byte)value
);
}
Second, you'll need the ColorNames enumeration listed below. I believe these are all the colors recognized by the Silverlight XAML parser (I might have missed one or two, but I don't think so). Comment the ones you don't need or remove them entirely (but you might want to keep a backup). Then, to get the color you want just use FromName, as in SolidColorBrush brush = new SolidColorBrush(FromName(ColorNames.Firebrick));
public enum ColorNames : 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,
DarkTurquoise = 0xFF00CED1,
DarkViolet = 0xFF9400D3,
DeepPink = 0xFFFF1493,
DeepSkyBlue = 0xFF00BFFF,
DimGray = 0xFF696969,
DodgerBlue = 0xFF1E90FF,
Firebrick = 0xFFB22222,
FloralWhite = 0xFFFFFAF0,
ForestGreen = 0xFF228B22,
Fuchsia = 0xFFFF00FF,
Gainsboro = 0xFFDCDCDC,
GhostWhite = 0xFFF8F8FF,
Gold = 0xFFFFD700,
Goldenrod = 0xFFDAA520,
Gray = 0xFF808080,
Green = 0xFF008000,
GreenYellow = 0xFFADFF2F,
Honeydew = 0xFFF0FFF0,
HotPink = 0xFFFF69B4,
IndianRed = 0xFFCD5C5C,
Indigo = 0xFF4B0082,
Ivory = 0xFFFFFFF0,
Khaki = 0xFFF0E68C,
Lavender = 0xFFE6E6FA,
LavenderBlush = 0xFFFFF0F5,
LawnGreen = 0xFF7CFC00,
LemonChiffon = 0xFFFFFACD,
LightBlue = 0xFFADD8E6,
LightCoral = 0xFFF08080,
LightCyan = 0xFFE0FFFF,
LightGoldenrodYellow = 0xFFFAFAD2,
LightGray = 0xFFD3D3D3,
LightGreen = 0xFF90EE90,
LightPink = 0xFFFFB6C1,
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
}
shacktoms
Member
185 Points
155 Posts
Re: Colors in Silverlight: I need a bigger box of crayons!
Apr 03, 2008 06:05 PM | LINK
Thanks. I was very surprised that DarkSlateGray wasn't available to me in code. You've helped me a lot. By the way, I find that a convenient place to stick the FromName method is as an extension to the ColorName enum, like this..
public static class ColorNamesExtensions { // thanks to Jabb... // http://silverlight.net/forums/t/13225.aspx public static Color FromName(this ColorNames color) { uint value = (uint)color; return Color.FromArgb( (byte)(value >> 24), (byte)(value >> 16), (byte)(value >> 8), (byte)value ); } }I just put that definition right beside the definition of the enum and then I can create colors like this...
Jabb
Member
474 Points
82 Posts
Re: Re: Colors in Silverlight: I need a bigger box of crayons!
Apr 04, 2008 08:30 PM | LINK
ooo that's nifty - never thought of extending an *enum*
that's pretty cool [;)]
shacktoms
Member
185 Points
155 Posts
Re: Colors in Silverlight: I need a bigger box of crayons!
Apr 24, 2008 03:47 PM | LINK
Or I can even write...
The way I did it was to turn the enum into a struct and define all the colors as static readonly values of the struct. Like this (pardon my old-style preferences for braces).
public struct ColorName { // thanks to Jabb's blog // http://silverlight.net/forums/t/13225.aspx // for the code from which this evolved and // especially for all the color definitions! public static implicit operator Color(ColorName color) { uint value = color; return Color.FromArgb( (byte)(value >> 24), (byte)(value >> 16), (byte)(value >> 8), (byte)value ); } public static implicit operator Brush(ColorName color) { return new SolidColorBrush(color); } public static implicit operator uint(ColorName color) { return color.ColorValue; } public static implicit operator ColorName(uint color) { return new ColorName(color); } private uint ColorValue; public ColorName(uint color) { ColorValue = color; } public static readonly ColorName AliceBlue = 0xFFF0F8FF; public static readonly ColorName AntiqueWhite = 0xFFFAEBD7; public static readonly ColorName Aqua = 0xFF00FFFF; public static readonly ColorName Aquamarine = 0xFF7FFFD4; // ... and so on ... }jink
Member
2 Points
6 Posts
Re: Colors in Silverlight: I need a bigger box of crayons!
May 12, 2011 08:28 PM | LINK
Nice code shacktoms. I'll have to look closer at this "implicit operator" stuff - powerful!