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 * Modifications by konigsberg:
13 * Make Closure-compatible.
15 * @author Stoyan Stefanov <sstoo@gmail.com>
16 * @link http://www.phpied.com/rgb-color-parser-in-javascript/
17 * @license Use it if you like it
22 * Create a new instance.
24 * @param {string} color_string the string to be parsed.
27 function RGBColorParser(color_string
)
31 // strip any leading #
32 if (color_string
.charAt(0) == '#') { // remove # if any
33 color_string
= color_string
.substr(1,6);
36 color_string
= color_string
.replace(/ /g,'');
37 color_string
= color_string
.toLowerCase();
39 // before getting into regexps, try simple matches
40 // and overwrite the input
43 antiquewhite
: 'faebd7',
50 blanchedalmond
: 'ffebcd',
59 cornflowerblue
: '6495ed',
65 darkgoldenrod
: 'b8860b',
69 darkmagenta
: '8b008b',
70 darkolivegreen
: '556b2f',
75 darkseagreen
: '8fbc8f',
76 darkslateblue
: '483d8b',
77 darkslategray
: '2f4f4f',
78 darkturquoise
: '00ced1',
81 deepskyblue
: '00bfff',
86 floralwhite
: 'fffaf0',
87 forestgreen
: '228b22',
95 greenyellow
: 'adff2f',
103 lavenderblush
: 'fff0f5',
105 lemonchiffon
: 'fffacd',
107 lightcoral
: 'f08080',
109 lightgoldenrodyellow
: 'fafad2',
111 lightgreen
: '90ee90',
113 lightsalmon
: 'ffa07a',
114 lightseagreen
: '20b2aa',
115 lightskyblue
: '87cefa',
116 lightslateblue
: '8470ff',
117 lightslategray
: '778899',
118 lightsteelblue
: 'b0c4de',
119 lightyellow
: 'ffffe0',
125 mediumaquamarine
: '66cdaa',
126 mediumblue
: '0000cd',
127 mediumorchid
: 'ba55d3',
128 mediumpurple
: '9370d8',
129 mediumseagreen
: '3cb371',
130 mediumslateblue
: '7b68ee',
131 mediumspringgreen
: '00fa9a',
132 mediumturquoise
: '48d1cc',
133 mediumvioletred
: 'c71585',
134 midnightblue
: '191970',
138 navajowhite
: 'ffdead',
146 palegoldenrod
: 'eee8aa',
148 paleturquoise
: 'afeeee',
149 palevioletred
: 'd87093',
150 papayawhip
: 'ffefd5',
155 powderblue
: 'b0e0e6',
160 saddlebrown
: '8b4513',
162 sandybrown
: 'f4a460',
171 springgreen
: '00ff7f',
182 whitesmoke
: 'f5f5f5',
184 yellowgreen
: '9acd32'
186 for (var key
in simple_colors
) {
187 if (color_string
== key
) {
188 color_string
= simple_colors
[key
];
191 // emd of simple type-in colors
193 // array of color definition objects
196 re
: /^rgb\((\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3})\)$/,
197 example
: ['rgb(123, 234, 45)', 'rgb(255,234,245)'],
198 process
: function (bits
){
200 parseInt(bits
[1], 10),
201 parseInt(bits
[2], 10),
202 parseInt(bits
[3], 10)
207 re
: /^(\w{2})(\w{2})(\w{2})$/,
208 example
: ['#00ff00', '336699'],
209 process
: function (bits
){
211 parseInt(bits
[1], 16),
212 parseInt(bits
[2], 16),
213 parseInt(bits
[3], 16)
218 re
: /^(\w{1})(\w{1})(\w{1})$/,
219 example
: ['#fb0', 'f0f'],
220 process
: function (bits
){
222 parseInt(bits
[1] + bits
[1], 16),
223 parseInt(bits
[2] + bits
[2], 16),
224 parseInt(bits
[3] + bits
[3], 16)
230 // search through the definitions to find a match
231 for (var i
= 0; i
< color_defs
.length
; i
++) {
232 var re
= color_defs
[i
].re
;
233 var processor
= color_defs
[i
].process
;
234 var bits
= re
.exec(color_string
);
236 var channels
= processor(bits
);
237 this.r
= channels
[0];
238 this.g
= channels
[1];
239 this.b
= channels
[2];
245 // validate/cleanup values
246 this.r
= (this.r
< 0 || isNaN(this.r
)) ? 0 : ((this.r
> 255) ? 255 : this.r
);
247 this.g
= (this.g
< 0 || isNaN(this.g
)) ? 0 : ((this.g
> 255) ? 255 : this.g
);
248 this.b
= (this.b
< 0 || isNaN(this.b
)) ? 0 : ((this.b
> 255) ? 255 : this.b
);
251 this.toRGB
= function () {
252 return 'rgb(' + this.r
+ ', ' + this.g
+ ', ' + this.b
+ ')';
254 this.toHex
= function () {
255 var r
= this.r
.toString(16);
256 var g
= this.g
.toString(16);
257 var b
= this.b
.toString(16);
258 if (r
.length
== 1) r
= '0' + r
;
259 if (g
.length
== 1) g
= '0' + g
;
260 if (b
.length
== 1) b
= '0' + b
;
261 return '#' + r
+ g
+ b
;