AS3 HTML Color Keywords (ColorName Class)


/ Published in: ActionScript 3
Save to your folder(s)

All credit for this class goes to Mark Walters (www.yourpalmark.com)

This class needs to be put in the package structure "com/yourpalmark/utils"


Copy this code and paste it in your HTML
  1. /*
  2. Copyright (c) 2009 Mark Walters
  3.  
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10.  
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13.  
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. */
  22.  
  23. package com.yourpalmark.utils
  24. {
  25. import flash.utils.Dictionary;
  26.  
  27. /**
  28. * The ColorName class defines the names and hexadecimal values of the colors defined in SVG 1.0.
  29. * The colors include the X11 colors supported by popular browsers with the addition of gray/grey variants from SVG 1.0.
  30. * The resulting list is precisely the same as the SVG 1.0 color keyword names.
  31. * The SVG 1.0 colors include the HTML4 colors, based on the VGA colors.
  32. * @see http://www.w3.org/TR/css3-color/#html4
  33. * @see http://www.w3.org/TR/css3-color/#svg-color
  34. *
  35. * @author Mark Walters
  36. */
  37. public class ColorName
  38. {
  39. //HTML4 COLOR KEYWORDS - VGA
  40. //@see http://www.w3.org/TR/css3-color/#html4
  41. public static const AQUA:ColorName = new ColorName( "aqua", 0x00FFFF );
  42. public static const BLACK:ColorName = new ColorName( "black", 0x000000 );
  43. public static const BLUE:ColorName = new ColorName( "blue", 0x0000FF );
  44. public static const FUCHSIA:ColorName = new ColorName( "fuchsia", 0xFF00FF );
  45. public static const GRAY:ColorName = new ColorName( "gray", 0x808080 );
  46. public static const GREEN:ColorName = new ColorName( "green", 0x008000 );
  47. public static const LIME:ColorName = new ColorName( "lime", 0x00FF00 );
  48. public static const MAROON:ColorName = new ColorName( "maroon", 0x800000 );
  49. public static const NAVY:ColorName = new ColorName( "navy", 0x000080 );
  50. public static const OLIVE:ColorName = new ColorName( "olive", 0x808000 );
  51. public static const PURPLE:ColorName = new ColorName( "purple", 0x800080 );
  52. public static const RED:ColorName = new ColorName( "red", 0xFF0000 );
  53. public static const SILVER:ColorName = new ColorName( "silver", 0xC0C0C0 );
  54. public static const TEAL:ColorName = new ColorName( "teal", 0x008080 );
  55. public static const WHITE:ColorName = new ColorName( "white", 0xFFFFFF );
  56. public static const YELLOW:ColorName = new ColorName( "yellow", 0xFFFF00 );
  57.  
  58. //SVG COLOR KEYWORDS - X11 ( INCLUDES THE 16 HTML4 - VGA COLORS )
  59. //@see http://www.w3.org/TR/css3-color/#svg-color
  60. public static const ALICE_BLUE:ColorName = new ColorName( "aliceBlue", 0xF0F8FF );
  61. public static const ANTIQUE_WHITE:ColorName = new ColorName( "antiqueWhite", 0xFAEBD7 );
  62. public static const AQUAMARINE:ColorName = new ColorName( "aquamarine", 0x7FFFD4 );
  63. public static const AZURE:ColorName = new ColorName( "azure", 0xF0FFFF );
  64. public static const BEIGE:ColorName = new ColorName( "beige", 0xF5F5DC );
  65. public static const BISQUE:ColorName = new ColorName( "bisque", 0xFFE4C4 );
  66. public static const BLANCHED_ALMOND:ColorName = new ColorName( "blanchedAlmond", 0xFFEBCD );
  67. public static const BLUE_VIOLET:ColorName = new ColorName( "blueViolet", 0x8A2BE2 );
  68. public static const BROWN:ColorName = new ColorName( "brown", 0xA52A2A );
  69. public static const BURLY_WOOD:ColorName = new ColorName( "burlyWood", 0xDEB887 );
  70. public static const CADET_BLUE:ColorName = new ColorName( "cadetBlue", 0x5F9EA0 );
  71. public static const CHARTREUSE:ColorName = new ColorName( "chartreuse", 0x7FFF00 );
  72. public static const CHOCOLATE:ColorName = new ColorName( "chocolate", 0xD2691E );
  73. public static const CORAL:ColorName = new ColorName( "coral", 0xFF7F50 );
  74. public static const CORNFLOWER_BLUE:ColorName = new ColorName( "cornflowerBlue", 0x6495ED );
  75. public static const CORNSILK:ColorName = new ColorName( "cornsilk", 0xFFF8DC );
  76. public static const CRIMSON:ColorName = new ColorName( "crimson", 0xDC143C );
  77. public static const CYAN:ColorName = new ColorName( "cyan", 0x00FFFF );
  78. public static const DARK_BLUE:ColorName = new ColorName( "darkBlue", 0x00008B );
  79. public static const DARK_CYAN:ColorName = new ColorName( "darkCyan", 0x008B8B );
  80. public static const DARK_GOLDEN_ROD:ColorName = new ColorName( "darkGoldenRod", 0xB8860B );
  81. public static const DARK_GRAY:ColorName = new ColorName( "darkGray", 0xA9A9A9 );
  82. public static const DARK_GREY:ColorName = new ColorName( "darkGrey", 0xA9A9A9 );
  83. public static const DARK_GREEN:ColorName = new ColorName( "darkGreen", 0x006400 );
  84. public static const DARK_KHAKI:ColorName = new ColorName( "darkKhaki", 0xBDB76B );
  85. public static const DARK_MAGENTA:ColorName = new ColorName( "darkMagenta", 0x8B008B );
  86. public static const DARK_OLIVE_GREEN:ColorName = new ColorName( "darkOliveGreen", 0x556B2F );
  87. public static const DARK_ORANGE:ColorName = new ColorName( "darkOrange", 0xFF8C00 );
  88. public static const DARK_ORCHID:ColorName = new ColorName( "darkOrchid", 0x9932CC );
  89. public static const DARK_RED:ColorName = new ColorName( "darkRed", 0x8B0000 );
  90. public static const DARK_SALMON:ColorName = new ColorName( "darkSalmon", 0xE9967A );
  91. public static const DARK_SEA_GREEN:ColorName = new ColorName( "darkSeaGreen", 0x8FBC8F );
  92. public static const DARK_SLATE_BLUE:ColorName = new ColorName( "darkSlateBlue", 0x483D8B );
  93. public static const DARK_SLATE_GRAY:ColorName = new ColorName( "darkSlateGray", 0x2F4F4F );
  94. public static const DARK_SLATE_GREY:ColorName = new ColorName( "darkSlateGrey", 0x2F4F4F );
  95. public static const DARK_TURQUOISE:ColorName = new ColorName( "darkTurquoise", 0x00CED1 );
  96. public static const DARK_VIOLET:ColorName = new ColorName( "darkViolet", 0x9400D3 );
  97. public static const DEEP_PINK:ColorName = new ColorName( "deepPink", 0xFF1493 );
  98. public static const DEEP_SKY_BLUE:ColorName = new ColorName( "deepSkyBlue", 0x00BFFF );
  99. public static const DIM_GRAY:ColorName = new ColorName( "dimGray", 0x696969 );
  100. public static const DIM_GREY:ColorName = new ColorName( "dimGrey", 0x696969 );
  101. public static const DODGER_BLUE:ColorName = new ColorName( "dodgerBlue", 0x1E90FF );
  102. public static const FIRE_BRICK:ColorName = new ColorName( "fireBrick", 0xB22222 );
  103. public static const FLORAL_WHITE:ColorName = new ColorName( "floralWhite", 0xFFFAF0 );
  104. public static const FOREST_GREEN:ColorName = new ColorName( "forestGreen", 0x228B22 );
  105. public static const GAINSBORO:ColorName = new ColorName( "gainsboro", 0xDCDCDC );
  106. public static const GHOST_WHITE:ColorName = new ColorName( "ghostWhite", 0xF8F8FF );
  107. public static const GOLD:ColorName = new ColorName( "gold", 0xFFD700 );
  108. public static const GOLDEN_ROD:ColorName = new ColorName( "goldenRod", 0xDAA520 );
  109. public static const GREY:ColorName = new ColorName( "grey", 0x808080 );
  110. public static const GREEN_YELLOW:ColorName = new ColorName( "greenYellow", 0xADFF2F );
  111. public static const HONEY_DEW:ColorName = new ColorName( "honeyDew", 0xF0FFF0 );
  112. public static const HOT_PINK:ColorName = new ColorName( "hotPink", 0xFF69B4 );
  113. public static const INDIAN_RED:ColorName = new ColorName( "indianRed", 0xCD5C5C );
  114. public static const INDIGO:ColorName = new ColorName( "indigo", 0x4B0082 );
  115. public static const IVORY:ColorName = new ColorName( "ivory", 0xFFFFF0 );
  116. public static const KHAKI:ColorName = new ColorName( "khaki", 0xF0E68C );
  117. public static const LAVENDER:ColorName = new ColorName( "lavender", 0xE6E6FA );
  118. public static const LAVENDER_BLUSH:ColorName = new ColorName( "lavenderBlush", 0xFFF0F5 );
  119. public static const LAWN_GREEN:ColorName = new ColorName( "lawnGreen", 0x7CFC00 );
  120. public static const LEMON_CHIFFON:ColorName = new ColorName( "lemonChiffon", 0xFFFACD );
  121. public static const LIGHT_BLUE:ColorName = new ColorName( "lightBlue", 0xADD8E6 );
  122. public static const LIGHT_CORAL:ColorName = new ColorName( "lightCoral", 0xF08080 );
  123. public static const LIGHT_CYAN:ColorName = new ColorName( "lightCyan", 0xE0FFFF );
  124. public static const LIGHT_GOLDEN_ROD_YELLOW:ColorName = new ColorName( "lightGoldenRodYellow", 0xFAFAD2 );
  125. public static const LIGHT_GRAY:ColorName = new ColorName( "lightGray", 0xD3D3D3 );
  126. public static const LIGHT_GREY:ColorName = new ColorName( "lightGrey", 0xD3D3D3 );
  127. public static const LIGHT_GREEN:ColorName = new ColorName( "lightGreen", 0x90EE90 );
  128. public static const LIGHT_PINK:ColorName = new ColorName( "lightPink", 0xFFB6C1 );
  129. public static const LIGHT_SALMON:ColorName = new ColorName( "lightSalmon", 0xFFA07A );
  130. public static const LIGHT_SEA_GREEN:ColorName = new ColorName( "lightSeaGreen", 0x20B2AA );
  131. public static const LIGHT_SKY_BLUE:ColorName = new ColorName( "lightSkyBlue", 0x87CEFA );
  132. public static const LIGHT_SLATE_GRAY:ColorName = new ColorName( "lightSlateGray", 0x778899 );
  133. public static const LIGHT_SLATE_GREY:ColorName = new ColorName( "lightSlateGrey", 0x778899 );
  134. public static const LIGHT_STEEL_BLUE:ColorName = new ColorName( "lightSteelBlue", 0xB0C4DE );
  135. public static const LIGHT_YELLOW:ColorName = new ColorName( "lightYellow", 0xFFFFE0 );
  136. public static const LIME_GREEN:ColorName = new ColorName( "limeGreen", 0x32CD32 );
  137. public static const LINEN:ColorName = new ColorName( "linen", 0xFAF0E6 );
  138. public static const MAGENTA:ColorName = new ColorName( "magenta", 0xFF00FF );
  139. public static const MEDIUM_AQUA_MARINE:ColorName = new ColorName( "mediumAquaMarine", 0x66CDAA );
  140. public static const MEDIUM_BLUE:ColorName = new ColorName( "mediumBlue", 0x0000CD );
  141. public static const MEDIUM_ORCHID:ColorName = new ColorName( "mediumOrchid", 0xBA55D3 );
  142. public static const MEDIUM_PURPLE:ColorName = new ColorName( "mediumPurple", 0x9370D8 );
  143. public static const MEDIUM_SEA_GREEN:ColorName = new ColorName( "mediumSeaGreen", 0x3CB371 );
  144. public static const MEDIUM_SLATE_BLUE:ColorName = new ColorName( "mediumSlateBlue", 0x7B68EE );
  145. public static const MEDIUM_SPRING_GREEN:ColorName = new ColorName( "mediumSpringGreen", 0x00FA9A );
  146. public static const MEDIUM_TURQUOISE:ColorName = new ColorName( "mediumTurquoise", 0x48D1CC );
  147. public static const MEDIUM_VIOLET_RED:ColorName = new ColorName( "mediumVioletRed", 0xC71585 );
  148. public static const MIDNIGHT_BLUE:ColorName = new ColorName( "midnightBlue", 0x191970 );
  149. public static const MINT_CREAM:ColorName = new ColorName( "mintCream", 0xF5FFFA );
  150. public static const MISTY_ROSE:ColorName = new ColorName( "mistyRose", 0xFFE4E1 );
  151. public static const MOCCASIN:ColorName = new ColorName( "moccasin", 0xFFE4B5 );
  152. public static const NAVAJO_WHITE:ColorName = new ColorName( "navajoWhite", 0xFFDEAD );
  153. public static const OLD_LACE:ColorName = new ColorName( "oldLace", 0xFDF5E6 );
  154. public static const OLIVE_DRAB:ColorName = new ColorName( "oliveDrab", 0x6B8E23 );
  155. public static const ORANGE:ColorName = new ColorName( "orange", 0xFFA500 );
  156. public static const ORANGE_RED:ColorName = new ColorName( "orangeRed", 0xFF4500 );
  157. public static const ORCHID:ColorName = new ColorName( "orchid", 0xDA70D6 );
  158. public static const PALE_GOLDEN_ROD:ColorName = new ColorName( "paleGoldenRod", 0xEEE8AA );
  159. public static const PALE_GREEN:ColorName = new ColorName( "paleGreen", 0x98FB98 );
  160. public static const PALE_TURQUOISE:ColorName = new ColorName( "paleTurquoise", 0xAFEEEE );
  161. public static const PALE_VIOLET_RED:ColorName = new ColorName( "paleVioletRed", 0xD87093 );
  162. public static const PAPAYA_WHIP:ColorName = new ColorName( "papayaWhip", 0xFFEFD5 );
  163. public static const PEACH_PUFF:ColorName = new ColorName( "peachPuff", 0xFFDAB9 );
  164. public static const PERU:ColorName = new ColorName( "peru", 0xCD853F );
  165. public static const PINK:ColorName = new ColorName( "pink", 0xFFC0CB );
  166. public static const PLUM:ColorName = new ColorName( "plum", 0xDDA0DD );
  167. public static const POWDER_BLUE:ColorName = new ColorName( "powderBlue", 0xB0E0E6 );
  168. public static const ROSY_BROWN:ColorName = new ColorName( "rosyBrown", 0xBC8F8F );
  169. public static const ROYAL_BLUE:ColorName = new ColorName( "royalBlue", 0x4169E1 );
  170. public static const SADDLE_BROWN:ColorName = new ColorName( "saddleBrown", 0x8B4513 );
  171. public static const SALMON:ColorName = new ColorName( "salmon", 0xFA8072 );
  172. public static const SANDY_BROWN:ColorName = new ColorName( "sandyBrown", 0xF4A460 );
  173. public static const SEA_GREEN:ColorName = new ColorName( "seaGreen", 0x2E8B57 );
  174. public static const SEA_SHELL:ColorName = new ColorName( "seaShell", 0xFFF5EE );
  175. public static const SIENNA:ColorName = new ColorName( "sienna", 0xA0522D );
  176. public static const SKY_BLUE:ColorName = new ColorName( "skyBlue", 0x87CEEB );
  177. public static const SLATE_BLUE:ColorName = new ColorName( "slateBlue", 0x6A5ACD );
  178. public static const SLATE_GRAY:ColorName = new ColorName( "slateGray", 0x708090 );
  179. public static const SLATE_GREY:ColorName = new ColorName( "slateGrey", 0x708090 );
  180. public static const SNOW:ColorName = new ColorName( "snow", 0xFFFAFA );
  181. public static const SPRING_GREEN:ColorName = new ColorName( "springGreen", 0x00FF7F );
  182. public static const STEEL_BLUE:ColorName = new ColorName( "steelBlue", 0x4682B4 );
  183. public static const TAN:ColorName = new ColorName( "tan", 0xD2B48C );
  184. public static const THISTLE:ColorName = new ColorName( "thistle", 0xD8BFD8 );
  185. public static const TOMATO:ColorName = new ColorName( "tomato", 0xFF6347 );
  186. public static const TURQUOISE:ColorName = new ColorName( "turquoise", 0x40E0D0 );
  187. public static const VIOLET:ColorName = new ColorName( "violet", 0xEE82EE );
  188. public static const WHEAT:ColorName = new ColorName( "wheat", 0xF5DEB3 );
  189. public static const WHITE_SMOKE:ColorName = new ColorName( "whiteSmoke", 0xF5F5F5 );
  190. public static const YELLOW_GREEN:ColorName = new ColorName( "yellowGreen", 0x9ACD32 );
  191.  
  192. /**
  193. * @private
  194. */
  195. protected static var nameDict:Dictionary;
  196.  
  197. /**
  198. * @private
  199. */
  200. protected static var colorDict:Dictionary;
  201.  
  202. /**
  203. * @private
  204. */
  205. private var _name:String;
  206.  
  207. /**
  208. * @private
  209. */
  210. private var _color:uint;
  211.  
  212. /**
  213. * Constructor.
  214. *
  215. * @param name The string name of a color (case-insensitive).
  216. * @param color The hexadecimal value of a color.
  217. */
  218. public function ColorName( name:String, color:uint )
  219. {
  220. if( !nameDict ) nameDict = new Dictionary();
  221. if( !colorDict ) colorDict = new Dictionary();
  222.  
  223. this.name = name;
  224. this.color = color;
  225. }
  226.  
  227. /**
  228. * The string name of a color (case-insensitive).
  229. */
  230. public function get name():String
  231. {
  232. return _name;
  233. }
  234.  
  235. /**
  236. * @private
  237. */
  238. public function set name( value:String ):void
  239. {
  240. _name = value;
  241. nameDict[ value.toLowerCase() ] = this;
  242. }
  243.  
  244. /**
  245. * The hexadecimal value of a color.
  246. */
  247. public function get color():uint
  248. {
  249. return _color;
  250. }
  251.  
  252. /**
  253. * @private
  254. */
  255. public function set color( value:uint ):void
  256. {
  257. _color = value;
  258. colorDict[ value ] = this;
  259. }
  260.  
  261. /**
  262. * Returns the corresponding ColorName object from the string name.
  263. *
  264. * @param name The string name of a color (case-insensitive).
  265. * @return A ColorName object.
  266. */
  267. public static function getColorNameByName( name:String ):ColorName
  268. {
  269. return nameDict[ name.toLowerCase() ];
  270. }
  271.  
  272. /**
  273. * Returns the corresponding ColorName object from the hexadecimal value.
  274. *
  275. * @param color The hexadecimal value of a color.
  276. * @return A ColorName object.
  277. */
  278. public static function getColorNameByColor( color:uint ):ColorName
  279. {
  280. return colorDict[ color ];
  281. }
  282.  
  283. /**
  284. * Returns a string containing all the properties of the ColorName object.
  285. */
  286. public function toString():String
  287. {
  288. var colorString:String = color.toString( 16 ).toUpperCase();
  289. while( 6 > colorString.length ) colorString = "0" + colorString;
  290. return "[ColorName name=\"" + name + "\" color=0x" + colorString + "]";
  291. }
  292.  
  293. }
  294. }
  295.  
  296.  
  297.  
  298. // Usage example ...
  299. //
  300. // package
  301. // {
  302. // import flash.display.Sprite;
  303. // import com.yourpalmark.utils.ColorName;
  304. //
  305. // public class Main extends Sprite
  306. // {
  307. // private var canvas:Sprite;
  308. //
  309. // public function Main():void
  310. // {
  311. // canvas = new Sprite();
  312. // canvas.graphics.beginFill(ColorName.LAVENDER_BLUSH.color, 1);
  313. // canvas.graphics.drawRect(0, 0, 500, 250);
  314. // canvas.graphics.endFill();
  315. // addChild(canvas);
  316. // }
  317. //
  318. // }
  319. //
  320. // }

URL: http://yourpalmark.com/2008/06/20/color-name-class-svg-10-x11-and-vga/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.