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