1 | /*
|
---|
2 | Based on rgbcolor.js by Stoyan Stefanov <sstoo@gmail.com>
|
---|
3 | http://www.phpied.com/rgb-color-parser-in-javascript/
|
---|
4 | */
|
---|
5 |
|
---|
6 | module.exports = function(color_string) {
|
---|
7 | this.ok = false;
|
---|
8 | this.alpha = 1.0;
|
---|
9 |
|
---|
10 | // strip any leading #
|
---|
11 | if (color_string.charAt(0) == '#') { // remove # if any
|
---|
12 | color_string = color_string.substr(1,6);
|
---|
13 | }
|
---|
14 |
|
---|
15 | color_string = color_string.replace(/ /g,'');
|
---|
16 | color_string = color_string.toLowerCase();
|
---|
17 |
|
---|
18 | // before getting into regexps, try simple matches
|
---|
19 | // and overwrite the input
|
---|
20 | var simple_colors = {
|
---|
21 | aliceblue: 'f0f8ff',
|
---|
22 | antiquewhite: 'faebd7',
|
---|
23 | aqua: '00ffff',
|
---|
24 | aquamarine: '7fffd4',
|
---|
25 | azure: 'f0ffff',
|
---|
26 | beige: 'f5f5dc',
|
---|
27 | bisque: 'ffe4c4',
|
---|
28 | black: '000000',
|
---|
29 | blanchedalmond: 'ffebcd',
|
---|
30 | blue: '0000ff',
|
---|
31 | blueviolet: '8a2be2',
|
---|
32 | brown: 'a52a2a',
|
---|
33 | burlywood: 'deb887',
|
---|
34 | cadetblue: '5f9ea0',
|
---|
35 | chartreuse: '7fff00',
|
---|
36 | chocolate: 'd2691e',
|
---|
37 | coral: 'ff7f50',
|
---|
38 | cornflowerblue: '6495ed',
|
---|
39 | cornsilk: 'fff8dc',
|
---|
40 | crimson: 'dc143c',
|
---|
41 | cyan: '00ffff',
|
---|
42 | darkblue: '00008b',
|
---|
43 | darkcyan: '008b8b',
|
---|
44 | darkgoldenrod: 'b8860b',
|
---|
45 | darkgray: 'a9a9a9',
|
---|
46 | darkgreen: '006400',
|
---|
47 | darkkhaki: 'bdb76b',
|
---|
48 | darkmagenta: '8b008b',
|
---|
49 | darkolivegreen: '556b2f',
|
---|
50 | darkorange: 'ff8c00',
|
---|
51 | darkorchid: '9932cc',
|
---|
52 | darkred: '8b0000',
|
---|
53 | darksalmon: 'e9967a',
|
---|
54 | darkseagreen: '8fbc8f',
|
---|
55 | darkslateblue: '483d8b',
|
---|
56 | darkslategray: '2f4f4f',
|
---|
57 | darkturquoise: '00ced1',
|
---|
58 | darkviolet: '9400d3',
|
---|
59 | deeppink: 'ff1493',
|
---|
60 | deepskyblue: '00bfff',
|
---|
61 | dimgray: '696969',
|
---|
62 | dodgerblue: '1e90ff',
|
---|
63 | feldspar: 'd19275',
|
---|
64 | firebrick: 'b22222',
|
---|
65 | floralwhite: 'fffaf0',
|
---|
66 | forestgreen: '228b22',
|
---|
67 | fuchsia: 'ff00ff',
|
---|
68 | gainsboro: 'dcdcdc',
|
---|
69 | ghostwhite: 'f8f8ff',
|
---|
70 | gold: 'ffd700',
|
---|
71 | goldenrod: 'daa520',
|
---|
72 | gray: '808080',
|
---|
73 | green: '008000',
|
---|
74 | greenyellow: 'adff2f',
|
---|
75 | honeydew: 'f0fff0',
|
---|
76 | hotpink: 'ff69b4',
|
---|
77 | indianred : 'cd5c5c',
|
---|
78 | indigo : '4b0082',
|
---|
79 | ivory: 'fffff0',
|
---|
80 | khaki: 'f0e68c',
|
---|
81 | lavender: 'e6e6fa',
|
---|
82 | lavenderblush: 'fff0f5',
|
---|
83 | lawngreen: '7cfc00',
|
---|
84 | lemonchiffon: 'fffacd',
|
---|
85 | lightblue: 'add8e6',
|
---|
86 | lightcoral: 'f08080',
|
---|
87 | lightcyan: 'e0ffff',
|
---|
88 | lightgoldenrodyellow: 'fafad2',
|
---|
89 | lightgrey: 'd3d3d3',
|
---|
90 | lightgreen: '90ee90',
|
---|
91 | lightpink: 'ffb6c1',
|
---|
92 | lightsalmon: 'ffa07a',
|
---|
93 | lightseagreen: '20b2aa',
|
---|
94 | lightskyblue: '87cefa',
|
---|
95 | lightslateblue: '8470ff',
|
---|
96 | lightslategray: '778899',
|
---|
97 | lightsteelblue: 'b0c4de',
|
---|
98 | lightyellow: 'ffffe0',
|
---|
99 | lime: '00ff00',
|
---|
100 | limegreen: '32cd32',
|
---|
101 | linen: 'faf0e6',
|
---|
102 | magenta: 'ff00ff',
|
---|
103 | maroon: '800000',
|
---|
104 | mediumaquamarine: '66cdaa',
|
---|
105 | mediumblue: '0000cd',
|
---|
106 | mediumorchid: 'ba55d3',
|
---|
107 | mediumpurple: '9370d8',
|
---|
108 | mediumseagreen: '3cb371',
|
---|
109 | mediumslateblue: '7b68ee',
|
---|
110 | mediumspringgreen: '00fa9a',
|
---|
111 | mediumturquoise: '48d1cc',
|
---|
112 | mediumvioletred: 'c71585',
|
---|
113 | midnightblue: '191970',
|
---|
114 | mintcream: 'f5fffa',
|
---|
115 | mistyrose: 'ffe4e1',
|
---|
116 | moccasin: 'ffe4b5',
|
---|
117 | navajowhite: 'ffdead',
|
---|
118 | navy: '000080',
|
---|
119 | oldlace: 'fdf5e6',
|
---|
120 | olive: '808000',
|
---|
121 | olivedrab: '6b8e23',
|
---|
122 | orange: 'ffa500',
|
---|
123 | orangered: 'ff4500',
|
---|
124 | orchid: 'da70d6',
|
---|
125 | palegoldenrod: 'eee8aa',
|
---|
126 | palegreen: '98fb98',
|
---|
127 | paleturquoise: 'afeeee',
|
---|
128 | palevioletred: 'd87093',
|
---|
129 | papayawhip: 'ffefd5',
|
---|
130 | peachpuff: 'ffdab9',
|
---|
131 | peru: 'cd853f',
|
---|
132 | pink: 'ffc0cb',
|
---|
133 | plum: 'dda0dd',
|
---|
134 | powderblue: 'b0e0e6',
|
---|
135 | purple: '800080',
|
---|
136 | rebeccapurple: '663399',
|
---|
137 | red: 'ff0000',
|
---|
138 | rosybrown: 'bc8f8f',
|
---|
139 | royalblue: '4169e1',
|
---|
140 | saddlebrown: '8b4513',
|
---|
141 | salmon: 'fa8072',
|
---|
142 | sandybrown: 'f4a460',
|
---|
143 | seagreen: '2e8b57',
|
---|
144 | seashell: 'fff5ee',
|
---|
145 | sienna: 'a0522d',
|
---|
146 | silver: 'c0c0c0',
|
---|
147 | skyblue: '87ceeb',
|
---|
148 | slateblue: '6a5acd',
|
---|
149 | slategray: '708090',
|
---|
150 | snow: 'fffafa',
|
---|
151 | springgreen: '00ff7f',
|
---|
152 | steelblue: '4682b4',
|
---|
153 | tan: 'd2b48c',
|
---|
154 | teal: '008080',
|
---|
155 | thistle: 'd8bfd8',
|
---|
156 | tomato: 'ff6347',
|
---|
157 | turquoise: '40e0d0',
|
---|
158 | violet: 'ee82ee',
|
---|
159 | violetred: 'd02090',
|
---|
160 | wheat: 'f5deb3',
|
---|
161 | white: 'ffffff',
|
---|
162 | whitesmoke: 'f5f5f5',
|
---|
163 | yellow: 'ffff00',
|
---|
164 | yellowgreen: '9acd32'
|
---|
165 | };
|
---|
166 | color_string = simple_colors[color_string] || color_string;
|
---|
167 | // emd of simple type-in colors
|
---|
168 |
|
---|
169 | // array of color definition objects
|
---|
170 | var color_defs = [
|
---|
171 | {
|
---|
172 | re: /^rgba\((\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3}),\s*((?:\d?\.)?\d)\)$/,
|
---|
173 | example: ['rgba(123, 234, 45, 0.8)', 'rgba(255,234,245,1.0)'],
|
---|
174 | process: function (bits){
|
---|
175 | return [
|
---|
176 | parseInt(bits[1]),
|
---|
177 | parseInt(bits[2]),
|
---|
178 | parseInt(bits[3]),
|
---|
179 | parseFloat(bits[4])
|
---|
180 | ];
|
---|
181 | }
|
---|
182 | },
|
---|
183 | {
|
---|
184 | re: /^rgb\((\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3})\)$/,
|
---|
185 | example: ['rgb(123, 234, 45)', 'rgb(255,234,245)'],
|
---|
186 | process: function (bits){
|
---|
187 | return [
|
---|
188 | parseInt(bits[1]),
|
---|
189 | parseInt(bits[2]),
|
---|
190 | parseInt(bits[3])
|
---|
191 | ];
|
---|
192 | }
|
---|
193 | },
|
---|
194 | {
|
---|
195 | re: /^([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})$/,
|
---|
196 | example: ['#00ff00', '336699'],
|
---|
197 | process: function (bits){
|
---|
198 | return [
|
---|
199 | parseInt(bits[1], 16),
|
---|
200 | parseInt(bits[2], 16),
|
---|
201 | parseInt(bits[3], 16)
|
---|
202 | ];
|
---|
203 | }
|
---|
204 | },
|
---|
205 | {
|
---|
206 | re: /^([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})$/,
|
---|
207 | example: ['#fb0', 'f0f'],
|
---|
208 | process: function (bits){
|
---|
209 | return [
|
---|
210 | parseInt(bits[1] + bits[1], 16),
|
---|
211 | parseInt(bits[2] + bits[2], 16),
|
---|
212 | parseInt(bits[3] + bits[3], 16)
|
---|
213 | ];
|
---|
214 | }
|
---|
215 | }
|
---|
216 | ];
|
---|
217 |
|
---|
218 | // search through the definitions to find a match
|
---|
219 | for (var i = 0; i < color_defs.length; i++) {
|
---|
220 | var re = color_defs[i].re;
|
---|
221 | var processor = color_defs[i].process;
|
---|
222 | var bits = re.exec(color_string);
|
---|
223 | if (bits) {
|
---|
224 | var channels = processor(bits);
|
---|
225 | this.r = channels[0];
|
---|
226 | this.g = channels[1];
|
---|
227 | this.b = channels[2];
|
---|
228 | if (channels.length > 3) {
|
---|
229 | this.alpha = channels[3];
|
---|
230 | }
|
---|
231 | this.ok = true;
|
---|
232 | }
|
---|
233 |
|
---|
234 | }
|
---|
235 |
|
---|
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);
|
---|
240 | this.alpha = (this.alpha < 0) ? 0 : ((this.alpha > 1.0 || isNaN(this.alpha)) ? 1.0 : this.alpha);
|
---|
241 |
|
---|
242 | // some getters
|
---|
243 | this.toRGB = function () {
|
---|
244 | return 'rgb(' + this.r + ', ' + this.g + ', ' + this.b + ')';
|
---|
245 | }
|
---|
246 | this.toRGBA = function () {
|
---|
247 | return 'rgba(' + this.r + ', ' + this.g + ', ' + this.b + ', ' + this.alpha + ')';
|
---|
248 | }
|
---|
249 | this.toHex = function () {
|
---|
250 | var r = this.r.toString(16);
|
---|
251 | var g = this.g.toString(16);
|
---|
252 | var b = this.b.toString(16);
|
---|
253 | if (r.length == 1) r = '0' + r;
|
---|
254 | if (g.length == 1) g = '0' + g;
|
---|
255 | if (b.length == 1) b = '0' + b;
|
---|
256 | return '#' + r + g + b;
|
---|
257 | }
|
---|
258 |
|
---|
259 | // help
|
---|
260 | this.getHelpXML = function () {
|
---|
261 |
|
---|
262 | var examples = new Array();
|
---|
263 | // add regexps
|
---|
264 | for (var i = 0; i < color_defs.length; i++) {
|
---|
265 | var example = color_defs[i].example;
|
---|
266 | for (var j = 0; j < example.length; j++) {
|
---|
267 | examples[examples.length] = example[j];
|
---|
268 | }
|
---|
269 | }
|
---|
270 | // add type-in colors
|
---|
271 | for (var sc in simple_colors) {
|
---|
272 | examples[examples.length] = sc;
|
---|
273 | }
|
---|
274 |
|
---|
275 | var xml = document.createElement('ul');
|
---|
276 | xml.setAttribute('id', 'rgbcolor-examples');
|
---|
277 | for (var i = 0; i < examples.length; i++) {
|
---|
278 | try {
|
---|
279 | var list_item = document.createElement('li');
|
---|
280 | var list_color = new RGBColor(examples[i]);
|
---|
281 | var example_div = document.createElement('div');
|
---|
282 | example_div.style.cssText =
|
---|
283 | 'margin: 3px; '
|
---|
284 | + 'border: 1px solid black; '
|
---|
285 | + 'background:' + list_color.toHex() + '; '
|
---|
286 | + 'color:' + list_color.toHex()
|
---|
287 | ;
|
---|
288 | example_div.appendChild(document.createTextNode('test'));
|
---|
289 | var list_item_value = document.createTextNode(
|
---|
290 | ' ' + examples[i] + ' -> ' + list_color.toRGB() + ' -> ' + list_color.toHex()
|
---|
291 | );
|
---|
292 | list_item.appendChild(example_div);
|
---|
293 | list_item.appendChild(list_item_value);
|
---|
294 | xml.appendChild(list_item);
|
---|
295 |
|
---|
296 | } catch(e){}
|
---|
297 | }
|
---|
298 | return xml;
|
---|
299 |
|
---|
300 | }
|
---|
301 |
|
---|
302 | }
|
---|