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