Closurize rgbcolor.js
[dygraphs.git] / rgbcolor / rgbcolor.js
CommitLineData
a232a35c
DV
1/**
2 * A class to parse color values
3 *
4 * NOTE: modified by danvk. I removed the "getHelpXML" function to reduce the
3856711f 5 * file size, added "use strict" and a few "var" declarations where needed.
a232a35c 6 *
a96b8ba3
A
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"
11 *
14ea2141
RK
12 * Modifications by konigsberg:
13 * Make Closure-compatible.
14 *
a232a35c
DV
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
18 */
3856711f
DV
19"use strict";
20
14ea2141
RK
21/**
22 * Create a new instance.
23 *
24 * @param {string} color_string the string to be parsed.
25 * @constructor
26 */
a96b8ba3 27function RGBColorParser(color_string)
a232a35c
DV
28{
29 this.ok = false;
30
31 // strip any leading #
32 if (color_string.charAt(0) == '#') { // remove # if any
33 color_string = color_string.substr(1,6);
34 }
35
36 color_string = color_string.replace(/ /g,'');
37 color_string = color_string.toLowerCase();
38
39 // before getting into regexps, try simple matches
40 // and overwrite the input
41 var simple_colors = {
42 aliceblue: 'f0f8ff',
43 antiquewhite: 'faebd7',
44 aqua: '00ffff',
45 aquamarine: '7fffd4',
46 azure: 'f0ffff',
47 beige: 'f5f5dc',
48 bisque: 'ffe4c4',
49 black: '000000',
50 blanchedalmond: 'ffebcd',
51 blue: '0000ff',
52 blueviolet: '8a2be2',
53 brown: 'a52a2a',
54 burlywood: 'deb887',
55 cadetblue: '5f9ea0',
56 chartreuse: '7fff00',
57 chocolate: 'd2691e',
58 coral: 'ff7f50',
59 cornflowerblue: '6495ed',
60 cornsilk: 'fff8dc',
61 crimson: 'dc143c',
62 cyan: '00ffff',
63 darkblue: '00008b',
64 darkcyan: '008b8b',
65 darkgoldenrod: 'b8860b',
66 darkgray: 'a9a9a9',
67 darkgreen: '006400',
68 darkkhaki: 'bdb76b',
69 darkmagenta: '8b008b',
70 darkolivegreen: '556b2f',
71 darkorange: 'ff8c00',
72 darkorchid: '9932cc',
73 darkred: '8b0000',
74 darksalmon: 'e9967a',
75 darkseagreen: '8fbc8f',
76 darkslateblue: '483d8b',
77 darkslategray: '2f4f4f',
78 darkturquoise: '00ced1',
79 darkviolet: '9400d3',
80 deeppink: 'ff1493',
81 deepskyblue: '00bfff',
82 dimgray: '696969',
83 dodgerblue: '1e90ff',
84 feldspar: 'd19275',
85 firebrick: 'b22222',
86 floralwhite: 'fffaf0',
87 forestgreen: '228b22',
88 fuchsia: 'ff00ff',
89 gainsboro: 'dcdcdc',
90 ghostwhite: 'f8f8ff',
91 gold: 'ffd700',
92 goldenrod: 'daa520',
93 gray: '808080',
94 green: '008000',
95 greenyellow: 'adff2f',
96 honeydew: 'f0fff0',
97 hotpink: 'ff69b4',
98 indianred : 'cd5c5c',
99 indigo : '4b0082',
100 ivory: 'fffff0',
101 khaki: 'f0e68c',
102 lavender: 'e6e6fa',
103 lavenderblush: 'fff0f5',
104 lawngreen: '7cfc00',
105 lemonchiffon: 'fffacd',
106 lightblue: 'add8e6',
107 lightcoral: 'f08080',
108 lightcyan: 'e0ffff',
109 lightgoldenrodyellow: 'fafad2',
110 lightgrey: 'd3d3d3',
111 lightgreen: '90ee90',
112 lightpink: 'ffb6c1',
113 lightsalmon: 'ffa07a',
114 lightseagreen: '20b2aa',
115 lightskyblue: '87cefa',
116 lightslateblue: '8470ff',
117 lightslategray: '778899',
118 lightsteelblue: 'b0c4de',
119 lightyellow: 'ffffe0',
120 lime: '00ff00',
121 limegreen: '32cd32',
122 linen: 'faf0e6',
123 magenta: 'ff00ff',
124 maroon: '800000',
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',
135 mintcream: 'f5fffa',
136 mistyrose: 'ffe4e1',
137 moccasin: 'ffe4b5',
138 navajowhite: 'ffdead',
139 navy: '000080',
140 oldlace: 'fdf5e6',
141 olive: '808000',
142 olivedrab: '6b8e23',
143 orange: 'ffa500',
144 orangered: 'ff4500',
145 orchid: 'da70d6',
146 palegoldenrod: 'eee8aa',
147 palegreen: '98fb98',
148 paleturquoise: 'afeeee',
149 palevioletred: 'd87093',
150 papayawhip: 'ffefd5',
151 peachpuff: 'ffdab9',
152 peru: 'cd853f',
153 pink: 'ffc0cb',
154 plum: 'dda0dd',
155 powderblue: 'b0e0e6',
156 purple: '800080',
157 red: 'ff0000',
158 rosybrown: 'bc8f8f',
159 royalblue: '4169e1',
160 saddlebrown: '8b4513',
161 salmon: 'fa8072',
162 sandybrown: 'f4a460',
163 seagreen: '2e8b57',
164 seashell: 'fff5ee',
165 sienna: 'a0522d',
166 silver: 'c0c0c0',
167 skyblue: '87ceeb',
168 slateblue: '6a5acd',
169 slategray: '708090',
170 snow: 'fffafa',
171 springgreen: '00ff7f',
172 steelblue: '4682b4',
173 tan: 'd2b48c',
174 teal: '008080',
175 thistle: 'd8bfd8',
176 tomato: 'ff6347',
177 turquoise: '40e0d0',
178 violet: 'ee82ee',
179 violetred: 'd02090',
180 wheat: 'f5deb3',
181 white: 'ffffff',
182 whitesmoke: 'f5f5f5',
183 yellow: 'ffff00',
184 yellowgreen: '9acd32'
185 };
186 for (var key in simple_colors) {
187 if (color_string == key) {
188 color_string = simple_colors[key];
189 }
190 }
191 // emd of simple type-in colors
192
193 // array of color definition objects
194 var color_defs = [
195 {
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){
199 return [
14ea2141
RK
200 parseInt(bits[1], 10),
201 parseInt(bits[2], 10),
202 parseInt(bits[3], 10)
a232a35c
DV
203 ];
204 }
205 },
206 {
207 re: /^(\w{2})(\w{2})(\w{2})$/,
208 example: ['#00ff00', '336699'],
209 process: function (bits){
210 return [
211 parseInt(bits[1], 16),
212 parseInt(bits[2], 16),
213 parseInt(bits[3], 16)
214 ];
215 }
216 },
217 {
218 re: /^(\w{1})(\w{1})(\w{1})$/,
219 example: ['#fb0', 'f0f'],
220 process: function (bits){
221 return [
222 parseInt(bits[1] + bits[1], 16),
223 parseInt(bits[2] + bits[2], 16),
224 parseInt(bits[3] + bits[3], 16)
225 ];
226 }
227 }
228 ];
229
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);
235 if (bits) {
3856711f 236 var channels = processor(bits);
a232a35c
DV
237 this.r = channels[0];
238 this.g = channels[1];
239 this.b = channels[2];
240 this.ok = true;
241 }
242
243 }
244
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);
249
250 // some getters
251 this.toRGB = function () {
252 return 'rgb(' + this.r + ', ' + this.g + ', ' + this.b + ')';
253 }
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;
262 }
263
264
265}
266