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
7 function RGBColor(color_string)
11 // strip any leading #
12 if (color_string.charAt(0) == '#') { // remove # if any
13 color_string = color_string.substr(1,6);
16 color_string = color_string.replace(/ /g,'');
17 color_string = color_string.toLowerCase();
19 // before getting into regexps, try simple matches
20 // and overwrite the input
23 antiquewhite: 'faebd7',
30 blanchedalmond: 'ffebcd',
39 cornflowerblue: '6495ed',
45 darkgoldenrod: 'b8860b',
49 darkmagenta: '8b008b',
50 darkolivegreen: '556b2f',
55 darkseagreen: '8fbc8f',
56 darkslateblue: '483d8b',
57 darkslategray: '2f4f4f',
58 darkturquoise: '00ced1',
61 deepskyblue: '00bfff',
66 floralwhite: 'fffaf0',
67 forestgreen: '228b22',
75 greenyellow: 'adff2f',
83 lavenderblush: 'fff0f5',
85 lemonchiffon: 'fffacd',
89 lightgoldenrodyellow: 'fafad2',
93 lightsalmon: 'ffa07a',
94 lightseagreen: '20b2aa',
95 lightskyblue: '87cefa',
96 lightslateblue: '8470ff',
97 lightslategray: '778899',
98 lightsteelblue: 'b0c4de',
99 lightyellow: 'ffffe0',
105 mediumaquamarine: '66cdaa',
106 mediumblue: '0000cd',
107 mediumorchid: 'ba55d3',
108 mediumpurple: '9370d8',
109 mediumseagreen: '3cb371',
110 mediumslateblue: '7b68ee',
111 mediumspringgreen: '00fa9a',
112 mediumturquoise: '48d1cc',
113 mediumvioletred: 'c71585',
114 midnightblue: '191970',
118 navajowhite: 'ffdead',
126 palegoldenrod: 'eee8aa',
128 paleturquoise: 'afeeee',
129 palevioletred: 'd87093',
130 papayawhip: 'ffefd5',
135 powderblue: 'b0e0e6',
140 saddlebrown: '8b4513',
142 sandybrown: 'f4a460',
151 springgreen: '00ff7f',
162 whitesmoke: 'f5f5f5',
164 yellowgreen: '9acd32'
166 for (var key in simple_colors) {
167 if (color_string == key) {
168 color_string = simple_colors[key];
171 // emd of simple type-in colors
173 // array of color definition objects
176 re: /^rgb\((\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3})\)$/,
177 example: ['rgb(123, 234, 45)', 'rgb(255,234,245)'],
178 process: function (bits){
187 re: /^(\w{2})(\w{2})(\w{2})$/,
188 example: ['#00ff00', '336699'],
189 process: function (bits){
191 parseInt(bits[1], 16),
192 parseInt(bits[2], 16),
193 parseInt(bits[3], 16)
198 re: /^(\w{1})(\w{1})(\w{1})$/,
199 example: ['#fb0', 'f0f'],
200 process: function (bits){
202 parseInt(bits[1] + bits[1], 16),
203 parseInt(bits[2] + bits[2], 16),
204 parseInt(bits[3] + bits[3], 16)
210 // search through the definitions to find a match
211 for (var i = 0; i < color_defs.length; i++) {
212 var re = color_defs[i].re;
213 var processor = color_defs[i].process;
214 var bits = re.exec(color_string);
216 channels = processor(bits);
217 this.r = channels[0];
218 this.g = channels[1];
219 this.b = channels[2];
225 // validate/cleanup values
226 this.r = (this.r < 0 || isNaN(this.r)) ? 0 : ((this.r > 255) ? 255 : this.r);
227 this.g = (this.g < 0 || isNaN(this.g)) ? 0 : ((this.g > 255) ? 255 : this.g);
228 this.b = (this.b < 0 || isNaN(this.b)) ? 0 : ((this.b > 255) ? 255 : this.b);
231 this.toRGB = function () {
232 return 'rgb(' + this.r + ', ' + this.g + ', ' + this.b + ')';
234 this.toHex = function () {
235 var r = this.r.toString(16);
236 var g = this.g.toString(16);
237 var b = this.b.toString(16);
238 if (r.length == 1) r = '0' + r;
239 if (g.length == 1) g = '0' + g;
240 if (b.length == 1) b = '0' + b;
241 return '#' + r + g + b;
245 this.getHelpXML = function () {
247 var examples = new Array();
249 for (var i = 0; i < color_defs.length; i++) {
250 var example = color_defs[i].example;
251 for (var j = 0; j < example.length; j++) {
252 examples[examples.length] = example[j];
255 // add type-in colors
256 for (var sc in simple_colors) {
257 examples[examples.length] = sc;
260 var xml = document.createElement('ul');
261 xml.setAttribute('id', 'rgbcolor-examples');
262 for (var i = 0; i < examples.length; i++) {
264 var list_item = document.createElement('li');
265 var list_color = new RGBColor(examples[i]);
266 var example_div = document.createElement('div');
267 example_div.style.cssText =
269 + 'border: 1px solid black; '
270 + 'background:' + list_color.toHex() + '; '
271 + 'color:' + list_color.toHex()
273 example_div.appendChild(document.createTextNode('test'));
274 var list_item_value = document.createTextNode(
275 ' ' + examples[i] + ' -> ' + list_color.toRGB() + ' -> ' + list_color.toHex()
277 list_item.appendChild(example_div);
278 list_item.appendChild(list_item_value);
279 xml.appendChild(list_item);