2 * A class to parse color values
4 * NOTE: modified by danvk. I removed the "getHelpXML" function to reduce the
5 * file size, added "use strict" and a few "var" declarations where needed.
7 * Modifications by adilh:
8 * Original "RGBColor" function name collides with:
9 * http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-RGBColor
10 * so renamed to "RGBColorParser"
12 * @author Stoyan Stefanov <sstoo@gmail.com>
13 * @link http://www.phpied.com/rgb-color-parser-in-javascript/
14 * @license Use it if you like it
18 function RGBColorParser(color_string)
22 // strip any leading #
23 if (color_string.charAt(0) == '#') { // remove # if any
24 color_string = color_string.substr(1,6);
27 color_string = color_string.replace(/ /g,'');
28 color_string = color_string.toLowerCase();
30 // before getting into regexps, try simple matches
31 // and overwrite the input
34 antiquewhite: 'faebd7',
41 blanchedalmond: 'ffebcd',
50 cornflowerblue: '6495ed',
56 darkgoldenrod: 'b8860b',
60 darkmagenta: '8b008b',
61 darkolivegreen: '556b2f',
66 darkseagreen: '8fbc8f',
67 darkslateblue: '483d8b',
68 darkslategray: '2f4f4f',
69 darkturquoise: '00ced1',
72 deepskyblue: '00bfff',
77 floralwhite: 'fffaf0',
78 forestgreen: '228b22',
86 greenyellow: 'adff2f',
94 lavenderblush: 'fff0f5',
96 lemonchiffon: 'fffacd',
100 lightgoldenrodyellow: 'fafad2',
102 lightgreen: '90ee90',
104 lightsalmon: 'ffa07a',
105 lightseagreen: '20b2aa',
106 lightskyblue: '87cefa',
107 lightslateblue: '8470ff',
108 lightslategray: '778899',
109 lightsteelblue: 'b0c4de',
110 lightyellow: 'ffffe0',
116 mediumaquamarine: '66cdaa',
117 mediumblue: '0000cd',
118 mediumorchid: 'ba55d3',
119 mediumpurple: '9370d8',
120 mediumseagreen: '3cb371',
121 mediumslateblue: '7b68ee',
122 mediumspringgreen: '00fa9a',
123 mediumturquoise: '48d1cc',
124 mediumvioletred: 'c71585',
125 midnightblue: '191970',
129 navajowhite: 'ffdead',
137 palegoldenrod: 'eee8aa',
139 paleturquoise: 'afeeee',
140 palevioletred: 'd87093',
141 papayawhip: 'ffefd5',
146 powderblue: 'b0e0e6',
151 saddlebrown: '8b4513',
153 sandybrown: 'f4a460',
162 springgreen: '00ff7f',
173 whitesmoke: 'f5f5f5',
175 yellowgreen: '9acd32'
177 for (var key in simple_colors) {
178 if (color_string == key) {
179 color_string = simple_colors[key];
182 // emd of simple type-in colors
184 // array of color definition objects
187 re: /^rgb\((\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3})\)$/,
188 example: ['rgb(123, 234, 45)', 'rgb(255,234,245)'],
189 process: function (bits){
198 re: /^(\w{2})(\w{2})(\w{2})$/,
199 example: ['#00ff00', '336699'],
200 process: function (bits){
202 parseInt(bits[1], 16),
203 parseInt(bits[2], 16),
204 parseInt(bits[3], 16)
209 re: /^(\w{1})(\w{1})(\w{1})$/,
210 example: ['#fb0', 'f0f'],
211 process: function (bits){
213 parseInt(bits[1] + bits[1], 16),
214 parseInt(bits[2] + bits[2], 16),
215 parseInt(bits[3] + bits[3], 16)
221 // search through the definitions to find a match
222 for (var i = 0; i < color_defs.length; i++) {
223 var re = color_defs[i].re;
224 var processor = color_defs[i].process;
225 var bits = re.exec(color_string);
227 var channels = processor(bits);
228 this.r = channels[0];
229 this.g = channels[1];
230 this.b = channels[2];
236 // validate/cleanup values
237 this.r = (this.r < 0 || isNaN(this.r)) ? 0 : ((this.r > 255) ? 255 : this.r);
238 this.g = (this.g < 0 || isNaN(this.g)) ? 0 : ((this.g > 255) ? 255 : this.g);
239 this.b = (this.b < 0 || isNaN(this.b)) ? 0 : ((this.b > 255) ? 255 : this.b);
242 this.toRGB = function () {
243 return 'rgb(' + this.r + ', ' + this.g + ', ' + this.b + ')';
245 this.toHex = function () {
246 var r = this.r.toString(16);
247 var g = this.g.toString(16);
248 var b = this.b.toString(16);
249 if (r.length == 1) r = '0' + r;
250 if (g.length == 1) g = '0' + g;
251 if (b.length == 1) b = '0' + b;
252 return '#' + r + g + b;