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