2 * A class to parse color values
4 * NOTE: modified by danvk. I removed the "getHelpXML" function to reduce the
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
11 function RGBColor(color_string
)
15 // strip any leading #
16 if (color_string
.charAt(0) == '#') { // remove # if any
17 color_string
= color_string
.substr(1,6);
20 color_string
= color_string
.replace(/ /g,'');
21 color_string
= color_string
.toLowerCase();
23 // before getting into regexps, try simple matches
24 // and overwrite the input
27 antiquewhite
: 'faebd7',
34 blanchedalmond
: 'ffebcd',
43 cornflowerblue
: '6495ed',
49 darkgoldenrod
: 'b8860b',
53 darkmagenta
: '8b008b',
54 darkolivegreen
: '556b2f',
59 darkseagreen
: '8fbc8f',
60 darkslateblue
: '483d8b',
61 darkslategray
: '2f4f4f',
62 darkturquoise
: '00ced1',
65 deepskyblue
: '00bfff',
70 floralwhite
: 'fffaf0',
71 forestgreen
: '228b22',
79 greenyellow
: 'adff2f',
87 lavenderblush
: 'fff0f5',
89 lemonchiffon
: 'fffacd',
93 lightgoldenrodyellow
: 'fafad2',
97 lightsalmon
: 'ffa07a',
98 lightseagreen
: '20b2aa',
99 lightskyblue
: '87cefa',
100 lightslateblue
: '8470ff',
101 lightslategray
: '778899',
102 lightsteelblue
: 'b0c4de',
103 lightyellow
: 'ffffe0',
109 mediumaquamarine
: '66cdaa',
110 mediumblue
: '0000cd',
111 mediumorchid
: 'ba55d3',
112 mediumpurple
: '9370d8',
113 mediumseagreen
: '3cb371',
114 mediumslateblue
: '7b68ee',
115 mediumspringgreen
: '00fa9a',
116 mediumturquoise
: '48d1cc',
117 mediumvioletred
: 'c71585',
118 midnightblue
: '191970',
122 navajowhite
: 'ffdead',
130 palegoldenrod
: 'eee8aa',
132 paleturquoise
: 'afeeee',
133 palevioletred
: 'd87093',
134 papayawhip
: 'ffefd5',
139 powderblue
: 'b0e0e6',
144 saddlebrown
: '8b4513',
146 sandybrown
: 'f4a460',
155 springgreen
: '00ff7f',
166 whitesmoke
: 'f5f5f5',
168 yellowgreen
: '9acd32'
170 for (var key
in simple_colors
) {
171 if (color_string
== key
) {
172 color_string
= simple_colors
[key
];
175 // emd of simple type-in colors
177 // array of color definition objects
180 re
: /^rgb\((\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3})\)$/,
181 example
: ['rgb(123, 234, 45)', 'rgb(255,234,245)'],
182 process
: function (bits
){
191 re
: /^(\w{2})(\w{2})(\w{2})$/,
192 example
: ['#00ff00', '336699'],
193 process
: function (bits
){
195 parseInt(bits
[1], 16),
196 parseInt(bits
[2], 16),
197 parseInt(bits
[3], 16)
202 re
: /^(\w{1})(\w{1})(\w{1})$/,
203 example
: ['#fb0', 'f0f'],
204 process
: function (bits
){
206 parseInt(bits
[1] + bits
[1], 16),
207 parseInt(bits
[2] + bits
[2], 16),
208 parseInt(bits
[3] + bits
[3], 16)
214 // search through the definitions to find a match
215 for (var i
= 0; i
< color_defs
.length
; i
++) {
216 var re
= color_defs
[i
].re
;
217 var processor
= color_defs
[i
].process
;
218 var bits
= re
.exec(color_string
);
220 channels
= processor(bits
);
221 this.r
= channels
[0];
222 this.g
= channels
[1];
223 this.b
= channels
[2];
229 // validate/cleanup values
230 this.r
= (this.r
< 0 || isNaN(this.r
)) ? 0 : ((this.r
> 255) ? 255 : this.r
);
231 this.g
= (this.g
< 0 || isNaN(this.g
)) ? 0 : ((this.g
> 255) ? 255 : this.g
);
232 this.b
= (this.b
< 0 || isNaN(this.b
)) ? 0 : ((this.b
> 255) ? 255 : this.b
);
235 this.toRGB
= function () {
236 return 'rgb(' + this.r
+ ', ' + this.g
+ ', ' + this.b
+ ')';
238 this.toHex
= function () {
239 var r
= this.r
.toString(16);
240 var g
= this.g
.toString(16);
241 var b
= this.b
.toString(16);
242 if (r
.length
== 1) r
= '0' + r
;
243 if (g
.length
== 1) g
= '0' + g
;
244 if (b
.length
== 1) b
= '0' + b
;
245 return '#' + r
+ g
+ b
;