rgbcolor.src.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /** @preserve
  2. * A class to parse color values
  3. * @author Stoyan Stefanov <sstoo@gmail.com>
  4. * @link http://www.phpied.com/rgb-color-parser-in-javascript/
  5. * @license Use it if you like it
  6. */
  7. (function (global) {
  8. function RGBColor(color_string)
  9. {
  10. this.ok = false;
  11. // strip any leading #
  12. if (color_string.charAt(0) == '#') { // remove # if any
  13. color_string = color_string.substr(1,6);
  14. }
  15. color_string = color_string.replace(/ /g,'');
  16. color_string = color_string.toLowerCase();
  17. // before getting into regexps, try simple matches
  18. // and overwrite the input
  19. var simple_colors = {
  20. aliceblue: 'f0f8ff',
  21. antiquewhite: 'faebd7',
  22. aqua: '00ffff',
  23. aquamarine: '7fffd4',
  24. azure: 'f0ffff',
  25. beige: 'f5f5dc',
  26. bisque: 'ffe4c4',
  27. black: '000000',
  28. blanchedalmond: 'ffebcd',
  29. blue: '0000ff',
  30. blueviolet: '8a2be2',
  31. brown: 'a52a2a',
  32. burlywood: 'deb887',
  33. cadetblue: '5f9ea0',
  34. chartreuse: '7fff00',
  35. chocolate: 'd2691e',
  36. coral: 'ff7f50',
  37. cornflowerblue: '6495ed',
  38. cornsilk: 'fff8dc',
  39. crimson: 'dc143c',
  40. cyan: '00ffff',
  41. darkblue: '00008b',
  42. darkcyan: '008b8b',
  43. darkgoldenrod: 'b8860b',
  44. darkgray: 'a9a9a9',
  45. darkgreen: '006400',
  46. darkkhaki: 'bdb76b',
  47. darkmagenta: '8b008b',
  48. darkolivegreen: '556b2f',
  49. darkorange: 'ff8c00',
  50. darkorchid: '9932cc',
  51. darkred: '8b0000',
  52. darksalmon: 'e9967a',
  53. darkseagreen: '8fbc8f',
  54. darkslateblue: '483d8b',
  55. darkslategray: '2f4f4f',
  56. darkturquoise: '00ced1',
  57. darkviolet: '9400d3',
  58. deeppink: 'ff1493',
  59. deepskyblue: '00bfff',
  60. dimgray: '696969',
  61. dodgerblue: '1e90ff',
  62. feldspar: 'd19275',
  63. firebrick: 'b22222',
  64. floralwhite: 'fffaf0',
  65. forestgreen: '228b22',
  66. fuchsia: 'ff00ff',
  67. gainsboro: 'dcdcdc',
  68. ghostwhite: 'f8f8ff',
  69. gold: 'ffd700',
  70. goldenrod: 'daa520',
  71. gray: '808080',
  72. green: '008000',
  73. greenyellow: 'adff2f',
  74. honeydew: 'f0fff0',
  75. hotpink: 'ff69b4',
  76. indianred : 'cd5c5c',
  77. indigo : '4b0082',
  78. ivory: 'fffff0',
  79. khaki: 'f0e68c',
  80. lavender: 'e6e6fa',
  81. lavenderblush: 'fff0f5',
  82. lawngreen: '7cfc00',
  83. lemonchiffon: 'fffacd',
  84. lightblue: 'add8e6',
  85. lightcoral: 'f08080',
  86. lightcyan: 'e0ffff',
  87. lightgoldenrodyellow: 'fafad2',
  88. lightgrey: 'd3d3d3',
  89. lightgreen: '90ee90',
  90. lightpink: 'ffb6c1',
  91. lightsalmon: 'ffa07a',
  92. lightseagreen: '20b2aa',
  93. lightskyblue: '87cefa',
  94. lightslateblue: '8470ff',
  95. lightslategray: '778899',
  96. lightsteelblue: 'b0c4de',
  97. lightyellow: 'ffffe0',
  98. lime: '00ff00',
  99. limegreen: '32cd32',
  100. linen: 'faf0e6',
  101. magenta: 'ff00ff',
  102. maroon: '800000',
  103. mediumaquamarine: '66cdaa',
  104. mediumblue: '0000cd',
  105. mediumorchid: 'ba55d3',
  106. mediumpurple: '9370d8',
  107. mediumseagreen: '3cb371',
  108. mediumslateblue: '7b68ee',
  109. mediumspringgreen: '00fa9a',
  110. mediumturquoise: '48d1cc',
  111. mediumvioletred: 'c71585',
  112. midnightblue: '191970',
  113. mintcream: 'f5fffa',
  114. mistyrose: 'ffe4e1',
  115. moccasin: 'ffe4b5',
  116. navajowhite: 'ffdead',
  117. navy: '000080',
  118. oldlace: 'fdf5e6',
  119. olive: '808000',
  120. olivedrab: '6b8e23',
  121. orange: 'ffa500',
  122. orangered: 'ff4500',
  123. orchid: 'da70d6',
  124. palegoldenrod: 'eee8aa',
  125. palegreen: '98fb98',
  126. paleturquoise: 'afeeee',
  127. palevioletred: 'd87093',
  128. papayawhip: 'ffefd5',
  129. peachpuff: 'ffdab9',
  130. peru: 'cd853f',
  131. pink: 'ffc0cb',
  132. plum: 'dda0dd',
  133. powderblue: 'b0e0e6',
  134. purple: '800080',
  135. red: 'ff0000',
  136. rosybrown: 'bc8f8f',
  137. royalblue: '4169e1',
  138. saddlebrown: '8b4513',
  139. salmon: 'fa8072',
  140. sandybrown: 'f4a460',
  141. seagreen: '2e8b57',
  142. seashell: 'fff5ee',
  143. sienna: 'a0522d',
  144. silver: 'c0c0c0',
  145. skyblue: '87ceeb',
  146. slateblue: '6a5acd',
  147. slategray: '708090',
  148. snow: 'fffafa',
  149. springgreen: '00ff7f',
  150. steelblue: '4682b4',
  151. tan: 'd2b48c',
  152. teal: '008080',
  153. thistle: 'd8bfd8',
  154. tomato: 'ff6347',
  155. turquoise: '40e0d0',
  156. violet: 'ee82ee',
  157. violetred: 'd02090',
  158. wheat: 'f5deb3',
  159. white: 'ffffff',
  160. whitesmoke: 'f5f5f5',
  161. yellow: 'ffff00',
  162. yellowgreen: '9acd32'
  163. };
  164. for (var key in simple_colors) {
  165. if (color_string == key) {
  166. color_string = simple_colors[key];
  167. }
  168. }
  169. // emd of simple type-in colors
  170. // array of color definition objects
  171. var color_defs = [
  172. {
  173. re: /^rgb\((\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3})\)$/,
  174. example: ['rgb(123, 234, 45)', 'rgb(255,234,245)'],
  175. process: function (bits){
  176. return [
  177. parseInt(bits[1]),
  178. parseInt(bits[2]),
  179. parseInt(bits[3])
  180. ];
  181. }
  182. },
  183. {
  184. re: /^(\w{2})(\w{2})(\w{2})$/,
  185. example: ['#00ff00', '336699'],
  186. process: function (bits){
  187. return [
  188. parseInt(bits[1], 16),
  189. parseInt(bits[2], 16),
  190. parseInt(bits[3], 16)
  191. ];
  192. }
  193. },
  194. {
  195. re: /^(\w{1})(\w{1})(\w{1})$/,
  196. example: ['#fb0', 'f0f'],
  197. process: function (bits){
  198. return [
  199. parseInt(bits[1] + bits[1], 16),
  200. parseInt(bits[2] + bits[2], 16),
  201. parseInt(bits[3] + bits[3], 16)
  202. ];
  203. }
  204. }
  205. ];
  206. // search through the definitions to find a match
  207. for (var i = 0; i < color_defs.length; i++) {
  208. var re = color_defs[i].re;
  209. var processor = color_defs[i].process;
  210. var bits = re.exec(color_string);
  211. if (bits) {
  212. channels = processor(bits);
  213. this.r = channels[0];
  214. this.g = channels[1];
  215. this.b = channels[2];
  216. this.ok = true;
  217. }
  218. }
  219. // validate/cleanup values
  220. this.r = (this.r < 0 || isNaN(this.r)) ? 0 : ((this.r > 255) ? 255 : this.r);
  221. this.g = (this.g < 0 || isNaN(this.g)) ? 0 : ((this.g > 255) ? 255 : this.g);
  222. this.b = (this.b < 0 || isNaN(this.b)) ? 0 : ((this.b > 255) ? 255 : this.b);
  223. // some getters
  224. this.toRGB = function () {
  225. return 'rgb(' + this.r + ', ' + this.g + ', ' + this.b + ')';
  226. }
  227. this.toHex = function () {
  228. var r = this.r.toString(16);
  229. var g = this.g.toString(16);
  230. var b = this.b.toString(16);
  231. if (r.length == 1) r = '0' + r;
  232. if (g.length == 1) g = '0' + g;
  233. if (b.length == 1) b = '0' + b;
  234. return '#' + r + g + b;
  235. }
  236. // help
  237. this.getHelpXML = function () {
  238. var examples = new Array();
  239. // add regexps
  240. for (var i = 0; i < color_defs.length; i++) {
  241. var example = color_defs[i].example;
  242. for (var j = 0; j < example.length; j++) {
  243. examples[examples.length] = example[j];
  244. }
  245. }
  246. // add type-in colors
  247. for (var sc in simple_colors) {
  248. examples[examples.length] = sc;
  249. }
  250. var xml = document.createElement('ul');
  251. xml.setAttribute('id', 'rgbcolor-examples');
  252. for (var i = 0; i < examples.length; i++) {
  253. try {
  254. var list_item = document.createElement('li');
  255. var list_color = new RGBColor(examples[i]);
  256. var example_div = document.createElement('div');
  257. example_div.style.cssText =
  258. 'margin: 3px; '
  259. + 'border: 1px solid black; '
  260. + 'background:' + list_color.toHex() + '; '
  261. + 'color:' + list_color.toHex()
  262. ;
  263. example_div.appendChild(document.createTextNode('test'));
  264. var list_item_value = document.createTextNode(
  265. ' ' + examples[i] + ' -> ' + list_color.toRGB() + ' -> ' + list_color.toHex()
  266. );
  267. list_item.appendChild(example_div);
  268. list_item.appendChild(list_item_value);
  269. xml.appendChild(list_item);
  270. } catch(e){}
  271. }
  272. return xml;
  273. }
  274. }
  275. if (typeof define === "function" && define.amd) {
  276. define(function () {
  277. return RGBColor;
  278. });
  279. } else if (typeof module !== "undefined" && module.exports) {
  280. module.exports = RGBColor;
  281. } else {
  282. global.RGBColor = RGBColor;
  283. }
  284. return RGBColor;
  285. })(typeof self !== "undefined" && self || typeof window !== "undefined" && window || this);