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 * @author Stoyan Stefanov <sstoo@gmail.com>
8 * @link http://www.phpied.com/rgb-color-parser-in-javascript/
9 * @license Use it if you like it
13 function RGBColor(color_string
)
17 // strip any leading #
18 if (color_string
.charAt(0) == '#') { // remove # if any
19 color_string
= color_string
.substr(1,6);
22 color_string
= color_string
.replace(/ /g,'');
23 color_string
= color_string
.toLowerCase();
25 // before getting into regexps, try simple matches
26 // and overwrite the input
29 antiquewhite
: 'faebd7',
36 blanchedalmond
: 'ffebcd',
45 cornflowerblue
: '6495ed',
51 darkgoldenrod
: 'b8860b',
55 darkmagenta
: '8b008b',
56 darkolivegreen
: '556b2f',
61 darkseagreen
: '8fbc8f',
62 darkslateblue
: '483d8b',
63 darkslategray
: '2f4f4f',
64 darkturquoise
: '00ced1',
67 deepskyblue
: '00bfff',
72 floralwhite
: 'fffaf0',
73 forestgreen
: '228b22',
81 greenyellow
: 'adff2f',
89 lavenderblush
: 'fff0f5',
91 lemonchiffon
: 'fffacd',
95 lightgoldenrodyellow
: 'fafad2',
99 lightsalmon
: 'ffa07a',
100 lightseagreen
: '20b2aa',
101 lightskyblue
: '87cefa',
102 lightslateblue
: '8470ff',
103 lightslategray
: '778899',
104 lightsteelblue
: 'b0c4de',
105 lightyellow
: 'ffffe0',
111 mediumaquamarine
: '66cdaa',
112 mediumblue
: '0000cd',
113 mediumorchid
: 'ba55d3',
114 mediumpurple
: '9370d8',
115 mediumseagreen
: '3cb371',
116 mediumslateblue
: '7b68ee',
117 mediumspringgreen
: '00fa9a',
118 mediumturquoise
: '48d1cc',
119 mediumvioletred
: 'c71585',
120 midnightblue
: '191970',
124 navajowhite
: 'ffdead',
132 palegoldenrod
: 'eee8aa',
134 paleturquoise
: 'afeeee',
135 palevioletred
: 'd87093',
136 papayawhip
: 'ffefd5',
141 powderblue
: 'b0e0e6',
146 saddlebrown
: '8b4513',
148 sandybrown
: 'f4a460',
157 springgreen
: '00ff7f',
168 whitesmoke
: 'f5f5f5',
170 yellowgreen
: '9acd32'
172 for (var key
in simple_colors
) {
173 if (color_string
== key
) {
174 color_string
= simple_colors
[key
];
177 // emd of simple type-in colors
179 // array of color definition objects
182 re
: /^rgb\((\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3})\)$/,
183 example
: ['rgb(123, 234, 45)', 'rgb(255,234,245)'],
184 process
: function (bits
){
193 re
: /^(\w{2})(\w{2})(\w{2})$/,
194 example
: ['#00ff00', '336699'],
195 process
: function (bits
){
197 parseInt(bits
[1], 16),
198 parseInt(bits
[2], 16),
199 parseInt(bits
[3], 16)
204 re
: /^(\w{1})(\w{1})(\w{1})$/,
205 example
: ['#fb0', 'f0f'],
206 process
: function (bits
){
208 parseInt(bits
[1] + bits
[1], 16),
209 parseInt(bits
[2] + bits
[2], 16),
210 parseInt(bits
[3] + bits
[3], 16)
216 // search through the definitions to find a match
217 for (var i
= 0; i
< color_defs
.length
; i
++) {
218 var re
= color_defs
[i
].re
;
219 var processor
= color_defs
[i
].process
;
220 var bits
= re
.exec(color_string
);
222 var channels
= processor(bits
);
223 this.r
= channels
[0];
224 this.g
= channels
[1];
225 this.b
= channels
[2];
231 // validate/cleanup values
232 this.r
= (this.r
< 0 || isNaN(this.r
)) ? 0 : ((this.r
> 255) ? 255 : this.r
);
233 this.g
= (this.g
< 0 || isNaN(this.g
)) ? 0 : ((this.g
> 255) ? 255 : this.g
);
234 this.b
= (this.b
< 0 || isNaN(this.b
)) ? 0 : ((this.b
> 255) ? 255 : this.b
);
237 this.toRGB
= function () {
238 return 'rgb(' + this.r
+ ', ' + this.g
+ ', ' + this.b
+ ')';
240 this.toHex
= function () {
241 var r
= this.r
.toString(16);
242 var g
= this.g
.toString(16);
243 var b
= this.b
.toString(16);
244 if (r
.length
== 1) r
= '0' + r
;
245 if (g
.length
== 1) g
= '0' + g
;
246 if (b
.length
== 1) b
= '0' + b
;
247 return '#' + r
+ g
+ b
;