Programming with .NET - Generalhttp://forums.silverlight.net//17.aspx/1?Programming+with+NET+GeneralGeneral discussions around authoring Silverlight .NET applications.Mon, 01 Jan 0001 00:00:00 -05001743099http://forums.silverlight.net//p/13225/43099.aspx/1?Colors+in+Silverlight+I+need+a+bigger+box+of+crayons+Colors in Silverlight: I need a bigger box of crayons! <p>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.</p> <p>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).</p> <p>First, you'll need the following method. Stick it somewhere useful [;)]</p> <p>static public Color FromName(ColorNames color)<br> {<br> &nbsp;&nbsp;&nbsp; uint value = (uint)color;</p> <p>&nbsp;&nbsp;&nbsp; return Color.FromArgb(<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (byte)(value &gt;&gt; 24),<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (byte)(value &gt;&gt; 16),<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (byte)(value &gt;&gt; 8),<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (byte)value<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; );<br> }<br> &nbsp;</p> <p>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 <span style="font-size:8pt; color:#2b91af; line-height:115%; font-family:'Courier New'"> SolidColorBrush</span><span style="font-size:8pt; line-height:115%; font-family:'Courier New'"> brush = <span style="color:blue">new</span> <span style="color:#2b91af">SolidColorBrush</span>(FromName(<span style="color:#2b91af">ColorNames</span>.Firebrick));</span> </p> <p><span style="font-size:8pt; line-height:115%; font-family:'Courier New'">public enum ColorNames : uint<br> {<br> &nbsp;&nbsp;&nbsp; AliceBlue = 0xFFF0F8FF,<br> &nbsp;&nbsp;&nbsp; AntiqueWhite = 0xFFFAEBD7,<br> &nbsp;&nbsp;&nbsp; Aqua = 0xFF00FFFF,<br> &nbsp;&nbsp;&nbsp; Aquamarine = 0xFF7FFFD4,<br> &nbsp;&nbsp;&nbsp; Azure = 0xFFF0FFFF,<br> &nbsp;&nbsp;&nbsp; Beige = 0xFFF5F5DC,<br> &nbsp;&nbsp;&nbsp; Bisque = 0xFFFFE4C4,<br> &nbsp;&nbsp;&nbsp; Black = 0xFF000000,<br> &nbsp;&nbsp;&nbsp; BlanchedAlmond = 0xFFFFEBCD,<br> &nbsp;&nbsp;&nbsp; Blue = 0xFF0000FF,<br> &nbsp;&nbsp;&nbsp; BlueViolet = 0xFF8A2BE2,<br> &nbsp;&nbsp;&nbsp; Brown = 0xFFA52A2A,<br> &nbsp;&nbsp;&nbsp; BurlyWood = 0xFFDEB887,<br> &nbsp;&nbsp;&nbsp; CadetBlue = 0xFF5F9EA0,<br> &nbsp;&nbsp;&nbsp; Chartreuse = 0xFF7FFF00,<br> &nbsp;&nbsp;&nbsp; Chocolate = 0xFFD2691E,<br> &nbsp;&nbsp;&nbsp; Coral = 0xFFFF7F50,<br> &nbsp;&nbsp;&nbsp; CornflowerBlue = 0xFF6495ED,<br> &nbsp;&nbsp;&nbsp; Cornsilk = 0xFFFFF8DC,<br> &nbsp;&nbsp;&nbsp; Crimson = 0xFFDC143C,<br> &nbsp;&nbsp;&nbsp; Cyan = 0xFF00FFFF,<br> &nbsp;&nbsp;&nbsp; DarkBlue = 0xFF00008B,<br> &nbsp;&nbsp;&nbsp; DarkCyan = 0xFF008B8B,<br> &nbsp;&nbsp;&nbsp; DarkGoldenrod = 0xFFB8860B,<br> &nbsp;&nbsp;&nbsp; DarkGray = 0xFFA9A9A9,<br> &nbsp;&nbsp;&nbsp; DarkGreen = 0xFF006400,<br> &nbsp;&nbsp;&nbsp; DarkKhaki = 0xFFBDB76B,<br> &nbsp;&nbsp;&nbsp; DarkMagenta = 0xFF8B008B,<br> &nbsp;&nbsp;&nbsp; DarkOliveGreen = 0xFF556B2F,<br> &nbsp;&nbsp;&nbsp; DarkOrange = 0xFFFF8C00,<br> &nbsp;&nbsp;&nbsp; DarkOrchid = 0xFF9932CC,<br> &nbsp;&nbsp;&nbsp; DarkRed = 0xFF8B0000,<br> &nbsp;&nbsp;&nbsp; DarkSalmon = 0xFFE9967A,<br> &nbsp;&nbsp;&nbsp; DarkSeaGreen = 0xFF8FBC8F,<br> &nbsp;&nbsp;&nbsp; DarkSlateBlue = 0xFF483D8B,<br> &nbsp;&nbsp;&nbsp; DarkSlateGray = 0xFF2F4F4F,<br> &nbsp;&nbsp;&nbsp; DarkTurquoise = 0xFF00CED1,<br> &nbsp;&nbsp;&nbsp; DarkViolet = 0xFF9400D3,<br> &nbsp;&nbsp;&nbsp; DeepPink = 0xFFFF1493,<br> &nbsp;&nbsp;&nbsp; DeepSkyBlue = 0xFF00BFFF,<br> &nbsp;&nbsp;&nbsp; DimGray = 0xFF696969,<br> &nbsp;&nbsp;&nbsp; DodgerBlue = 0xFF1E90FF,<br> &nbsp;&nbsp;&nbsp; Firebrick = 0xFFB22222,<br> &nbsp;&nbsp;&nbsp; FloralWhite = 0xFFFFFAF0,<br> &nbsp;&nbsp;&nbsp; ForestGreen = 0xFF228B22,<br> &nbsp;&nbsp;&nbsp; Fuchsia = 0xFFFF00FF,<br> &nbsp;&nbsp;&nbsp; Gainsboro = 0xFFDCDCDC,<br> &nbsp;&nbsp;&nbsp; GhostWhite = 0xFFF8F8FF,<br> &nbsp;&nbsp;&nbsp; Gold = 0xFFFFD700,<br> &nbsp;&nbsp;&nbsp; Goldenrod = 0xFFDAA520,<br> &nbsp;&nbsp;&nbsp; Gray = 0xFF808080,<br> &nbsp;&nbsp;&nbsp; Green = 0xFF008000,<br> &nbsp;&nbsp;&nbsp; GreenYellow = 0xFFADFF2F,<br> &nbsp;&nbsp;&nbsp; Honeydew = 0xFFF0FFF0,<br> &nbsp;&nbsp;&nbsp; HotPink = 0xFFFF69B4,<br> &nbsp;&nbsp;&nbsp; IndianRed = 0xFFCD5C5C,<br> &nbsp;&nbsp;&nbsp; Indigo = 0xFF4B0082,<br> &nbsp;&nbsp;&nbsp; Ivory = 0xFFFFFFF0,<br> &nbsp;&nbsp;&nbsp; Khaki = 0xFFF0E68C,<br> &nbsp;&nbsp;&nbsp; Lavender = 0xFFE6E6FA,<br> &nbsp;&nbsp;&nbsp; LavenderBlush = 0xFFFFF0F5,<br> &nbsp;&nbsp;&nbsp; LawnGreen = 0xFF7CFC00,<br> &nbsp;&nbsp;&nbsp; LemonChiffon = 0xFFFFFACD,<br> &nbsp;&nbsp;&nbsp; LightBlue = 0xFFADD8E6,<br> &nbsp;&nbsp;&nbsp; LightCoral = 0xFFF08080,<br> &nbsp;&nbsp;&nbsp; LightCyan = 0xFFE0FFFF,<br> &nbsp;&nbsp;&nbsp; LightGoldenrodYellow = 0xFFFAFAD2,<br> &nbsp;&nbsp;&nbsp; LightGray = 0xFFD3D3D3,<br> &nbsp;&nbsp;&nbsp; LightGreen = 0xFF90EE90,<br> &nbsp;&nbsp;&nbsp; LightPink = 0xFFFFB6C1,<br> &nbsp;&nbsp;&nbsp; LightSalmon = 0xFFFFA07A,<br> &nbsp;&nbsp;&nbsp; LightSeaGreen = 0xFF20B2AA,<br> &nbsp;&nbsp;&nbsp; LightSkyBlue = 0xFF87CEFA,<br> &nbsp;&nbsp;&nbsp; LightSlateGray = 0xFF778899,<br> &nbsp;&nbsp;&nbsp; LightSteelBlue = 0xFFB0C4DE,<br> &nbsp;&nbsp;&nbsp; LightYellow = 0xFFFFFFE0,<br> &nbsp;&nbsp;&nbsp; Lime = 0xFF00FF00,<br> &nbsp;&nbsp;&nbsp; LimeGreen = 0xFF32CD32,<br> &nbsp;&nbsp;&nbsp; Linen = 0xFFFAF0E6,<br> &nbsp;&nbsp;&nbsp; Magenta = 0xFFFF00FF,<br> &nbsp;&nbsp;&nbsp; Maroon = 0xFF800000,<br> &nbsp;&nbsp;&nbsp; MediumAquamarine = 0xFF66CDAA,<br> &nbsp;&nbsp;&nbsp; MediumBlue = 0xFF0000CD,<br> &nbsp;&nbsp;&nbsp; MediumOrchid = 0xFFBA55D3,<br> &nbsp;&nbsp;&nbsp; MediumPurple = 0xFF9370DB,<br> &nbsp;&nbsp;&nbsp; MediumSeaGreen = 0xFF3CB371,<br> &nbsp;&nbsp;&nbsp; MediumSlateBlue = 0xFF7B68EE,<br> &nbsp;&nbsp;&nbsp; MediumSpringGreen = 0xFF00FA9A,<br> &nbsp;&nbsp;&nbsp; MediumTurquoise = 0xFF48D1CC,<br> &nbsp;&nbsp;&nbsp; MediumVioletRed = 0xFFC71585,<br> &nbsp;&nbsp;&nbsp; MidnightBlue = 0xFF191970,<br> &nbsp;&nbsp;&nbsp; MintCream = 0xFFF5FFFA,<br> &nbsp;&nbsp;&nbsp; MistyRose = 0xFFFFE4E1,<br> &nbsp;&nbsp;&nbsp; Moccasin = 0xFFFFE4B5,<br> &nbsp;&nbsp;&nbsp; NavajoWhite = 0xFFFFDEAD,<br> &nbsp;&nbsp;&nbsp; Navy = 0xFF000080,<br> &nbsp;&nbsp;&nbsp; OldLace = 0xFFFDF5E6,<br> &nbsp;&nbsp;&nbsp; Olive = 0xFF808000,<br> &nbsp;&nbsp;&nbsp; OliveDrab = 0xFF6B8E23,<br> &nbsp;&nbsp;&nbsp; Orange = 0xFFFFA500,<br> &nbsp;&nbsp;&nbsp; OrangeRed = 0xFFFF4500,<br> &nbsp;&nbsp;&nbsp; Orchid = 0xFFDA70D6,<br> &nbsp;&nbsp;&nbsp; PaleGoldenrod = 0xFFEEE8AA,<br> &nbsp;&nbsp;&nbsp; PaleGreen = 0xFF98FB98,<br> &nbsp;&nbsp;&nbsp; PaleTurquoise = 0xFFAFEEEE,<br> &nbsp;&nbsp;&nbsp; PaleVioletRed = 0xFFDB7093,<br> &nbsp;&nbsp;&nbsp; PapayaWhip = 0xFFFFEFD5,<br> &nbsp;&nbsp;&nbsp; PeachPuff = 0xFFFFDAB9,<br> &nbsp;&nbsp;&nbsp; Peru = 0xFFCD853F,<br> &nbsp;&nbsp;&nbsp; Pink = 0xFFFFC0CB,<br> &nbsp;&nbsp;&nbsp; Plum = 0xFFDDA0DD,<br> &nbsp;&nbsp;&nbsp; PowderBlue = 0xFFB0E0E6,<br> &nbsp;&nbsp;&nbsp; Purple = 0xFF800080,<br> &nbsp;&nbsp;&nbsp; Red = 0xFFFF0000,<br> &nbsp;&nbsp;&nbsp; RosyBrown = 0xFFBC8F8F,<br> &nbsp;&nbsp;&nbsp; RoyalBlue = 0xFF4169E1,<br> &nbsp;&nbsp;&nbsp; SaddleBrown = 0xFF8B4513,<br> &nbsp;&nbsp;&nbsp; Salmon = 0xFFFA8072,<br> &nbsp;&nbsp;&nbsp; SandyBrown = 0xFFF4A460,<br> &nbsp;&nbsp;&nbsp; SeaGreen = 0xFF2E8B57,<br> &nbsp;&nbsp;&nbsp; SeaShell = 0xFFFFF5EE,<br> &nbsp;&nbsp;&nbsp; Sienna = 0xFFA0522D,<br> &nbsp;&nbsp;&nbsp; Silver = 0xFFC0C0C0,<br> &nbsp;&nbsp;&nbsp; SkyBlue = 0xFF87CEEB,<br> &nbsp;&nbsp;&nbsp; SlateBlue = 0xFF6A5ACD,<br> &nbsp;&nbsp;&nbsp; SlateGray = 0xFF708090,<br> &nbsp;&nbsp;&nbsp; Snow = 0xFFFFFAFA,<br> &nbsp;&nbsp;&nbsp; SpringGreen = 0xFF00FF7F,<br> &nbsp;&nbsp;&nbsp; SteelBlue = 0xFF4682B4,<br> &nbsp;&nbsp;&nbsp; Tan = 0xFFD2B48C,<br> &nbsp;&nbsp;&nbsp; Teal = 0xFF008080,<br> &nbsp;&nbsp;&nbsp; Thistle = 0xFFD8BFD8,<br> &nbsp;&nbsp;&nbsp; Tomato = 0xFFFF6347,<br> &nbsp;&nbsp;&nbsp; Transparent = 0x00FFFFFF,<br> &nbsp;&nbsp;&nbsp; Turquoise = 0xFF40E0D0,<br> &nbsp;&nbsp;&nbsp; Violet = 0xFFEE82EE,<br> &nbsp;&nbsp;&nbsp; Wheat = 0xFFF5DEB3,<br> &nbsp;&nbsp;&nbsp; White = 0xFFFFFFFF,<br> &nbsp;&nbsp;&nbsp; WhiteSmoke = 0xFFF5F5F5,<br> &nbsp;&nbsp;&nbsp; Yellow = 0xFFFFFF00,<br> &nbsp;&nbsp;&nbsp; YellowGreen = 0xFF9ACD32<br> }<br> </span></p> 2008-04-01T16:47:17-04:0043867http://forums.silverlight.net//p/13225/43867.aspx/1?Re+Colors+in+Silverlight+I+need+a+bigger+box+of+crayons+Re: Colors in Silverlight: I need a bigger box of crayons! <p>Thanks.&nbsp; I was very surprised that DarkSlateGray wasn't available to me in code.&nbsp; You've helped me a lot.&nbsp;&nbsp; By the way, I find that a convenient place to stick the FromName method is as an extension to the ColorName enum, like this..</p> <pre class="code">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 &gt;&gt; 24), (byte)(value &gt;&gt; 16), (byte)(value &gt;&gt; 8), (byte)value ); } } </pre> <p>I just put that definition right beside the definition of the enum and then I can create colors like this...</p> <pre class="code">private Brush _Stroke = new SolidColorBrush(ColorNames.DarkSlateGray.FromName()); </pre> 2008-04-03T18:05:36-04:0044193http://forums.silverlight.net//p/13225/44193.aspx/1?Re+Re+Colors+in+Silverlight+I+need+a+bigger+box+of+crayons+Re: Re: Colors in Silverlight: I need a bigger box of crayons! <p>&nbsp;ooo that's nifty - never thought of extending an *enum*</p> <p>&nbsp;that's pretty cool [;)]<br> &nbsp;</p> 2008-04-04T20:30:54-04:0049034http://forums.silverlight.net//p/13225/49034.aspx/1?Re+Colors+in+Silverlight+I+need+a+bigger+box+of+crayons+Re: Colors in Silverlight: I need a bigger box of crayons! 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.&nbsp; 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.&nbsp;&nbsp; That is, I can write... <pre class="code">private Brush _Stroke = new SolidColorBrush(ColorName.DarkGray);</pre> <p>Or I can even write...</p> <pre class="code">private Brush _Stroke = ColorName.DarkGray;<br></pre> <p>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.&nbsp;&nbsp; Like this (pardon my old-style preferences for braces).</p> <pre class="code"> 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 &gt;&gt; 24), (byte)(value &gt;&gt; 16), (byte)(value &gt;&gt; 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 ... } </pre> 2008-04-24T15:47:19-04:00555804http://forums.silverlight.net//p/13225/555804.aspx/1?Re+Colors+in+Silverlight+I+need+a+bigger+box+of+crayons+Re: Colors in Silverlight: I need a bigger box of crayons! <p>Nice code shacktoms. &nbsp;I'll have to look closer at this &quot;<span class="Apple-style-span" style="font-family:Consolas,'Courier New',Courier,monospace; white-space:pre">implicit operator&quot; stuff - powerful!</span></p> 2011-05-12T20:28:02-04:00