author | Giulio Cesare Solaroli <giulio.cesare@clipperz.com> | 2013-08-30 15:56:53 (UTC) |
---|---|---|
committer | Giulio Cesare Solaroli <giulio.cesare@clipperz.com> | 2013-08-30 21:23:42 (UTC) |
commit | 20bea94ab6b91c85b171dcf86baba0a64169d508 (patch) (unidiff) | |
tree | 6e38e91498dcdb861620eba1e237d1026fe79cc5 /frontend/delta/js/MochiKit/Color.js | |
parent | bde3c7b98523112ade9c5bbf7390c4ecb494cd2e (diff) | |
download | clipperz-20bea94ab6b91c85b171dcf86baba0a64169d508.zip clipperz-20bea94ab6b91c85b171dcf86baba0a64169d508.tar.gz clipperz-20bea94ab6b91c85b171dcf86baba0a64169d508.tar.bz2 |
First release of /delta version
Diffstat (limited to 'frontend/delta/js/MochiKit/Color.js') (more/less context) (ignore whitespace changes)
-rw-r--r-- | frontend/delta/js/MochiKit/Color.js | 846 |
1 files changed, 846 insertions, 0 deletions
diff --git a/frontend/delta/js/MochiKit/Color.js b/frontend/delta/js/MochiKit/Color.js new file mode 100644 index 0000000..f421737 --- a/dev/null +++ b/frontend/delta/js/MochiKit/Color.js | |||
@@ -0,0 +1,846 @@ | |||
1 | /* | ||
2 | |||
3 | Copyright 2008-2013 Clipperz Srl | ||
4 | |||
5 | This file is part of Clipperz, the online password manager. | ||
6 | For further information about its features and functionalities please | ||
7 | refer to http://www.clipperz.com. | ||
8 | |||
9 | * Clipperz is free software: you can redistribute it and/or modify it | ||
10 | under the terms of the GNU Affero General Public License as published | ||
11 | by the Free Software Foundation, either version 3 of the License, or | ||
12 | (at your option) any later version. | ||
13 | |||
14 | * Clipperz is distributed in the hope that it will be useful, but | ||
15 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
17 | See the GNU Affero General Public License for more details. | ||
18 | |||
19 | * You should have received a copy of the GNU Affero General Public | ||
20 | License along with Clipperz. If not, see http://www.gnu.org/licenses/. | ||
21 | |||
22 | */ | ||
23 | |||
24 | /*** | ||
25 | |||
26 | MochiKit.Color 1.5 | ||
27 | |||
28 | See <http://mochikit.com/> for documentation, downloads, license, etc. | ||
29 | |||
30 | (c) 2005 Bob Ippolito and others. All rights Reserved. | ||
31 | |||
32 | ***/ | ||
33 | |||
34 | MochiKit.Base.module(MochiKit, 'Color', '1.5', ['Base', 'DOM', 'Style']); | ||
35 | |||
36 | /** @id MochiKit.Color.Color */ | ||
37 | MochiKit.Color.Color = function (red, green, blue, alpha) { | ||
38 | if (typeof(alpha) == 'undefined' || alpha === null) { | ||
39 | alpha = 1.0; | ||
40 | } | ||
41 | this.rgb = { | ||
42 | r: red, | ||
43 | g: green, | ||
44 | b: blue, | ||
45 | a: alpha | ||
46 | }; | ||
47 | }; | ||
48 | |||
49 | |||
50 | // Prototype methods | ||
51 | |||
52 | MochiKit.Color.Color.prototype = { | ||
53 | |||
54 | __class__: MochiKit.Color.Color, | ||
55 | |||
56 | /** @id MochiKit.Color.Color.prototype.colorWithAlpha */ | ||
57 | colorWithAlpha: function (alpha) { | ||
58 | var rgb = this.rgb; | ||
59 | var m = MochiKit.Color; | ||
60 | return m.Color.fromRGB(rgb.r, rgb.g, rgb.b, alpha); | ||
61 | }, | ||
62 | |||
63 | /** @id MochiKit.Color.Color.prototype.colorWithHue */ | ||
64 | colorWithHue: function (hue) { | ||
65 | // get an HSL model, and set the new hue... | ||
66 | var hsl = this.asHSL(); | ||
67 | hsl.h = hue; | ||
68 | var m = MochiKit.Color; | ||
69 | // convert back to RGB... | ||
70 | return m.Color.fromHSL(hsl); | ||
71 | }, | ||
72 | |||
73 | /** @id MochiKit.Color.Color.prototype.colorWithSaturation */ | ||
74 | colorWithSaturation: function (saturation) { | ||
75 | // get an HSL model, and set the new hue... | ||
76 | var hsl = this.asHSL(); | ||
77 | hsl.s = saturation; | ||
78 | var m = MochiKit.Color; | ||
79 | // convert back to RGB... | ||
80 | return m.Color.fromHSL(hsl); | ||
81 | }, | ||
82 | |||
83 | /** @id MochiKit.Color.Color.prototype.colorWithLightness */ | ||
84 | colorWithLightness: function (lightness) { | ||
85 | // get an HSL model, and set the new hue... | ||
86 | var hsl = this.asHSL(); | ||
87 | hsl.l = lightness; | ||
88 | var m = MochiKit.Color; | ||
89 | // convert back to RGB... | ||
90 | return m.Color.fromHSL(hsl); | ||
91 | }, | ||
92 | |||
93 | /** @id MochiKit.Color.Color.prototype.darkerColorWithLevel */ | ||
94 | darkerColorWithLevel: function (level) { | ||
95 | var hsl = this.asHSL(); | ||
96 | hsl.l = Math.max(hsl.l - level, 0); | ||
97 | var m = MochiKit.Color; | ||
98 | return m.Color.fromHSL(hsl); | ||
99 | }, | ||
100 | |||
101 | /** @id MochiKit.Color.Color.prototype.lighterColorWithLevel */ | ||
102 | lighterColorWithLevel: function (level) { | ||
103 | var hsl = this.asHSL(); | ||
104 | hsl.l = Math.min(hsl.l + level, 1); | ||
105 | var m = MochiKit.Color; | ||
106 | return m.Color.fromHSL(hsl); | ||
107 | }, | ||
108 | |||
109 | /** @id MochiKit.Color.Color.prototype.blendedColor */ | ||
110 | blendedColor: function (other, /* optional */ fraction) { | ||
111 | if (typeof(fraction) == 'undefined' || fraction === null) { | ||
112 | fraction = 0.5; | ||
113 | } | ||
114 | var sf = 1.0 - fraction; | ||
115 | var s = this.rgb; | ||
116 | var d = other.rgb; | ||
117 | var df = fraction; | ||
118 | return MochiKit.Color.Color.fromRGB( | ||
119 | (s.r * sf) + (d.r * df), | ||
120 | (s.g * sf) + (d.g * df), | ||
121 | (s.b * sf) + (d.b * df), | ||
122 | (s.a * sf) + (d.a * df) | ||
123 | ); | ||
124 | }, | ||
125 | |||
126 | /** @id MochiKit.Color.Color.prototype.compareRGB */ | ||
127 | compareRGB: function (other) { | ||
128 | var a = this.asRGB(); | ||
129 | var b = other.asRGB(); | ||
130 | return MochiKit.Base.compare( | ||
131 | [a.r, a.g, a.b, a.a], | ||
132 | [b.r, b.g, b.b, b.a] | ||
133 | ); | ||
134 | }, | ||
135 | |||
136 | /** @id MochiKit.Color.Color.prototype.isLight */ | ||
137 | isLight: function () { | ||
138 | return this.asHSL().l > 0.5; | ||
139 | }, | ||
140 | |||
141 | /** @id MochiKit.Color.Color.prototype.isDark */ | ||
142 | isDark: function () { | ||
143 | return (!this.isLight()); | ||
144 | }, | ||
145 | |||
146 | /** @id MochiKit.Color.Color.prototype.toHSLString */ | ||
147 | toHSLString: function () { | ||
148 | var c = this.asHSL(); | ||
149 | var ccc = MochiKit.Color.clampColorComponent; | ||
150 | var rval = this._hslString; | ||
151 | if (!rval) { | ||
152 | var mid = ( | ||
153 | ccc(c.h, 360).toFixed(0) | ||
154 | + "," + ccc(c.s, 100).toPrecision(4) + "%" | ||
155 | + "," + ccc(c.l, 100).toPrecision(4) + "%" | ||
156 | ); | ||
157 | var a = c.a; | ||
158 | if (a >= 1) { | ||
159 | a = 1; | ||
160 | rval = "hsl(" + mid + ")"; | ||
161 | } else { | ||
162 | if (a <= 0) { | ||
163 | a = 0; | ||
164 | } | ||
165 | rval = "hsla(" + mid + "," + a + ")"; | ||
166 | } | ||
167 | this._hslString = rval; | ||
168 | } | ||
169 | return rval; | ||
170 | }, | ||
171 | |||
172 | /** @id MochiKit.Color.Color.prototype.toRGBString */ | ||
173 | toRGBString: function () { | ||
174 | var c = this.rgb; | ||
175 | var ccc = MochiKit.Color.clampColorComponent; | ||
176 | var rval = this._rgbString; | ||
177 | if (!rval) { | ||
178 | var mid = ( | ||
179 | ccc(c.r, 255).toFixed(0) | ||
180 | + "," + ccc(c.g, 255).toFixed(0) | ||
181 | + "," + ccc(c.b, 255).toFixed(0) | ||
182 | ); | ||
183 | if (c.a != 1) { | ||
184 | rval = "rgba(" + mid + "," + c.a + ")"; | ||
185 | } else { | ||
186 | rval = "rgb(" + mid + ")"; | ||
187 | } | ||
188 | this._rgbString = rval; | ||
189 | } | ||
190 | return rval; | ||
191 | }, | ||
192 | |||
193 | /** @id MochiKit.Color.Color.prototype.asRGB */ | ||
194 | asRGB: function () { | ||
195 | return MochiKit.Base.clone(this.rgb); | ||
196 | }, | ||
197 | |||
198 | /** @id MochiKit.Color.Color.prototype.toHexString */ | ||
199 | toHexString: function () { | ||
200 | var m = MochiKit.Color; | ||
201 | var c = this.rgb; | ||
202 | var ccc = MochiKit.Color.clampColorComponent; | ||
203 | var rval = this._hexString; | ||
204 | if (!rval) { | ||
205 | rval = ("#" + | ||
206 | m.toColorPart(ccc(c.r, 255)) + | ||
207 | m.toColorPart(ccc(c.g, 255)) + | ||
208 | m.toColorPart(ccc(c.b, 255)) | ||
209 | ); | ||
210 | this._hexString = rval; | ||
211 | } | ||
212 | return rval; | ||
213 | }, | ||
214 | |||
215 | /** @id MochiKit.Color.Color.prototype.asHSV */ | ||
216 | asHSV: function () { | ||
217 | var hsv = this.hsv; | ||
218 | var c = this.rgb; | ||
219 | if (typeof(hsv) == 'undefined' || hsv === null) { | ||
220 | hsv = MochiKit.Color.rgbToHSV(this.rgb); | ||
221 | this.hsv = hsv; | ||
222 | } | ||
223 | return MochiKit.Base.clone(hsv); | ||
224 | }, | ||
225 | |||
226 | /** @id MochiKit.Color.Color.prototype.asHSL */ | ||
227 | asHSL: function () { | ||
228 | var hsl = this.hsl; | ||
229 | var c = this.rgb; | ||
230 | if (typeof(hsl) == 'undefined' || hsl === null) { | ||
231 | hsl = MochiKit.Color.rgbToHSL(this.rgb); | ||
232 | this.hsl = hsl; | ||
233 | } | ||
234 | return MochiKit.Base.clone(hsl); | ||
235 | }, | ||
236 | |||
237 | /** @id MochiKit.Color.Color.prototype.toString */ | ||
238 | toString: function () { | ||
239 | return this.toRGBString(); | ||
240 | }, | ||
241 | |||
242 | /** @id MochiKit.Color.Color.prototype.repr */ | ||
243 | repr: function () { | ||
244 | var c = this.rgb; | ||
245 | var col = [c.r, c.g, c.b, c.a]; | ||
246 | return this.__class__.NAME + "(" + col.join(", ") + ")"; | ||
247 | } | ||
248 | |||
249 | }; | ||
250 | |||
251 | // Constructor methods | ||
252 | |||
253 | MochiKit.Base.update(MochiKit.Color.Color, { | ||
254 | /** @id MochiKit.Color.Color.fromRGB */ | ||
255 | fromRGB: function (red, green, blue, alpha) { | ||
256 | // designated initializer | ||
257 | var Color = MochiKit.Color.Color; | ||
258 | if (arguments.length == 1) { | ||
259 | var rgb = red; | ||
260 | red = rgb.r; | ||
261 | green = rgb.g; | ||
262 | blue = rgb.b; | ||
263 | if (typeof(rgb.a) == 'undefined') { | ||
264 | alpha = undefined; | ||
265 | } else { | ||
266 | alpha = rgb.a; | ||
267 | } | ||
268 | } | ||
269 | return new Color(red, green, blue, alpha); | ||
270 | }, | ||
271 | |||
272 | /** @id MochiKit.Color.Color.fromHSL */ | ||
273 | fromHSL: function (hue, saturation, lightness, alpha) { | ||
274 | var m = MochiKit.Color; | ||
275 | return m.Color.fromRGB(m.hslToRGB.apply(m, arguments)); | ||
276 | }, | ||
277 | |||
278 | /** @id MochiKit.Color.Color.fromHSV */ | ||
279 | fromHSV: function (hue, saturation, value, alpha) { | ||
280 | var m = MochiKit.Color; | ||
281 | return m.Color.fromRGB(m.hsvToRGB.apply(m, arguments)); | ||
282 | }, | ||
283 | |||
284 | /** @id MochiKit.Color.Color.fromName */ | ||
285 | fromName: function (name) { | ||
286 | var Color = MochiKit.Color.Color; | ||
287 | // Opera 9 seems to "quote" named colors(?!) | ||
288 | if (name.charAt(0) == '"') { | ||
289 | name = name.substr(1, name.length - 2); | ||
290 | } | ||
291 | var htmlColor = Color._namedColors[name.toLowerCase()]; | ||
292 | if (typeof(htmlColor) == 'string') { | ||
293 | return Color.fromHexString(htmlColor); | ||
294 | } else if (name == "transparent") { | ||
295 | return Color.transparentColor(); | ||
296 | } | ||
297 | return null; | ||
298 | }, | ||
299 | |||
300 | /** @id MochiKit.Color.Color.fromString */ | ||
301 | fromString: function (colorString) { | ||
302 | var self = MochiKit.Color.Color; | ||
303 | var three = colorString.substr(0, 3); | ||
304 | if (three == "rgb") { | ||
305 | return self.fromRGBString(colorString); | ||
306 | } else if (three == "hsl") { | ||
307 | return self.fromHSLString(colorString); | ||
308 | } else if (colorString.charAt(0) == "#") { | ||
309 | return self.fromHexString(colorString); | ||
310 | } | ||
311 | return self.fromName(colorString); | ||
312 | }, | ||
313 | |||
314 | |||
315 | /** @id MochiKit.Color.Color.fromHexString */ | ||
316 | fromHexString: function (hexCode) { | ||
317 | if (hexCode.charAt(0) == '#') { | ||
318 | hexCode = hexCode.substring(1); | ||
319 | } | ||
320 | var components = []; | ||
321 | var i, hex; | ||
322 | if (hexCode.length == 3) { | ||
323 | for (i = 0; i < 3; i++) { | ||
324 | hex = hexCode.substr(i, 1); | ||
325 | components.push(parseInt(hex + hex, 16) / 255.0); | ||
326 | } | ||
327 | } else { | ||
328 | for (i = 0; i < 6; i += 2) { | ||
329 | hex = hexCode.substr(i, 2); | ||
330 | components.push(parseInt(hex, 16) / 255.0); | ||
331 | } | ||
332 | } | ||
333 | var Color = MochiKit.Color.Color; | ||
334 | return Color.fromRGB.apply(Color, components); | ||
335 | }, | ||
336 | |||
337 | |||
338 | _fromColorString: function (pre, method, scales, colorCode) { | ||
339 | // parses either HSL or RGB | ||
340 | if (colorCode.indexOf(pre) === 0) { | ||
341 | colorCode = colorCode.substring(colorCode.indexOf("(", 3) + 1, colorCode.length - 1); | ||
342 | } | ||
343 | var colorChunks = colorCode.split(/\s*,\s*/); | ||
344 | var colorFloats = []; | ||
345 | for (var i = 0; i < colorChunks.length; i++) { | ||
346 | var c = colorChunks[i]; | ||
347 | var val; | ||
348 | var three = c.substring(c.length - 3); | ||
349 | if (c.charAt(c.length - 1) == '%') { | ||
350 | val = 0.01 * parseFloat(c.substring(0, c.length - 1)); | ||
351 | } else if (three == "deg") { | ||
352 | val = parseFloat(c) / 360.0; | ||
353 | } else if (three == "rad") { | ||
354 | val = parseFloat(c) / (Math.PI * 2); | ||
355 | } else { | ||
356 | val = scales[i] * parseFloat(c); | ||
357 | } | ||
358 | colorFloats.push(val); | ||
359 | } | ||
360 | return this[method].apply(this, colorFloats); | ||
361 | }, | ||
362 | |||
363 | /** @id MochiKit.Color.Color.fromComputedStyle */ | ||
364 | fromComputedStyle: function (elem, style) { | ||
365 | var d = MochiKit.DOM; | ||
366 | var cls = MochiKit.Color.Color; | ||
367 | for (elem = d.getElement(elem); elem; elem = elem.parentNode) { | ||
368 | var actualColor = MochiKit.Style.getStyle.apply(d, arguments); | ||
369 | if (!actualColor) { | ||
370 | continue; | ||
371 | } | ||
372 | var color = cls.fromString(actualColor); | ||
373 | if (!color) { | ||
374 | break; | ||
375 | } | ||
376 | if (color.asRGB().a > 0) { | ||
377 | return color; | ||
378 | } | ||
379 | } | ||
380 | return null; | ||
381 | }, | ||
382 | |||
383 | /** @id MochiKit.Color.Color.fromBackground */ | ||
384 | fromBackground: function (elem) { | ||
385 | var cls = MochiKit.Color.Color; | ||
386 | return cls.fromComputedStyle( | ||
387 | elem, "backgroundColor", "background-color") || cls.whiteColor(); | ||
388 | }, | ||
389 | |||
390 | /** @id MochiKit.Color.Color.fromText */ | ||
391 | fromText: function (elem) { | ||
392 | var cls = MochiKit.Color.Color; | ||
393 | return cls.fromComputedStyle( | ||
394 | elem, "color", "color") || cls.blackColor(); | ||
395 | }, | ||
396 | |||
397 | /** @id MochiKit.Color.Color.namedColors */ | ||
398 | namedColors: function () { | ||
399 | return MochiKit.Base.clone(MochiKit.Color.Color._namedColors); | ||
400 | } | ||
401 | }); | ||
402 | |||
403 | |||
404 | // Module level functions | ||
405 | |||
406 | MochiKit.Base.update(MochiKit.Color, { | ||
407 | /** @id MochiKit.Color.clampColorComponent */ | ||
408 | clampColorComponent: function (v, scale) { | ||
409 | v *= scale; | ||
410 | if (v < 0) { | ||
411 | return 0; | ||
412 | } else if (v > scale) { | ||
413 | return scale; | ||
414 | } else { | ||
415 | return v; | ||
416 | } | ||
417 | }, | ||
418 | |||
419 | _hslValue: function (n1, n2, hue) { | ||
420 | if (hue > 6.0) { | ||
421 | hue -= 6.0; | ||
422 | } else if (hue < 0.0) { | ||
423 | hue += 6.0; | ||
424 | } | ||
425 | var val; | ||
426 | if (hue < 1.0) { | ||
427 | val = n1 + (n2 - n1) * hue; | ||
428 | } else if (hue < 3.0) { | ||
429 | val = n2; | ||
430 | } else if (hue < 4.0) { | ||
431 | val = n1 + (n2 - n1) * (4.0 - hue); | ||
432 | } else { | ||
433 | val = n1; | ||
434 | } | ||
435 | return val; | ||
436 | }, | ||
437 | |||
438 | /** @id MochiKit.Color.hsvToRGB */ | ||
439 | hsvToRGB: function (hue, saturation, value, alpha) { | ||
440 | if (arguments.length == 1) { | ||
441 | var hsv = hue; | ||
442 | hue = hsv.h; | ||
443 | saturation = hsv.s; | ||
444 | value = hsv.v; | ||
445 | alpha = hsv.a; | ||
446 | } | ||
447 | var red; | ||
448 | var green; | ||
449 | var blue; | ||
450 | if (saturation === 0) { | ||
451 | red = value; | ||
452 | green = value; | ||
453 | blue = value; | ||
454 | } else { | ||
455 | var i = Math.floor(hue * 6); | ||
456 | var f = (hue * 6) - i; | ||
457 | var p = value * (1 - saturation); | ||
458 | var q = value * (1 - (saturation * f)); | ||
459 | var t = value * (1 - (saturation * (1 - f))); | ||
460 | switch (i) { | ||
461 | case 1: red = q; green = value; blue = p; break; | ||
462 | case 2: red = p; green = value; blue = t; break; | ||
463 | case 3: red = p; green = q; blue = value; break; | ||
464 | case 4: red = t; green = p; blue = value; break; | ||
465 | case 5: red = value; green = p; blue = q; break; | ||
466 | case 6: // fall through | ||
467 | case 0: red = value; green = t; blue = p; break; | ||
468 | } | ||
469 | } | ||
470 | return { | ||
471 | r: red, | ||
472 | g: green, | ||
473 | b: blue, | ||
474 | a: alpha | ||
475 | }; | ||
476 | }, | ||
477 | |||
478 | /** @id MochiKit.Color.hslToRGB */ | ||
479 | hslToRGB: function (hue, saturation, lightness, alpha) { | ||
480 | if (arguments.length == 1) { | ||
481 | var hsl = hue; | ||
482 | hue = hsl.h; | ||
483 | saturation = hsl.s; | ||
484 | lightness = hsl.l; | ||
485 | alpha = hsl.a; | ||
486 | } | ||
487 | var red; | ||
488 | var green; | ||
489 | var blue; | ||
490 | if (saturation === 0) { | ||
491 | red = lightness; | ||
492 | green = lightness; | ||
493 | blue = lightness; | ||
494 | } else { | ||
495 | var m2; | ||
496 | if (lightness <= 0.5) { | ||
497 | m2 = lightness * (1.0 + saturation); | ||
498 | } else { | ||
499 | m2 = lightness + saturation - (lightness * saturation); | ||
500 | } | ||
501 | var m1 = (2.0 * lightness) - m2; | ||
502 | var f = MochiKit.Color._hslValue; | ||
503 | var h6 = hue * 6.0; | ||
504 | red = f(m1, m2, h6 + 2); | ||
505 | green = f(m1, m2, h6); | ||
506 | blue = f(m1, m2, h6 - 2); | ||
507 | } | ||
508 | return { | ||
509 | r: red, | ||
510 | g: green, | ||
511 | b: blue, | ||
512 | a: alpha | ||
513 | }; | ||
514 | }, | ||
515 | |||
516 | /** @id MochiKit.Color.rgbToHSV */ | ||
517 | rgbToHSV: function (red, green, blue, alpha) { | ||
518 | if (arguments.length == 1) { | ||
519 | var rgb = red; | ||
520 | red = rgb.r; | ||
521 | green = rgb.g; | ||
522 | blue = rgb.b; | ||
523 | alpha = rgb.a; | ||
524 | } | ||
525 | var max = Math.max(Math.max(red, green), blue); | ||
526 | var min = Math.min(Math.min(red, green), blue); | ||
527 | var hue; | ||
528 | var saturation; | ||
529 | var value = max; | ||
530 | if (min == max) { | ||
531 | hue = 0; | ||
532 | saturation = 0; | ||
533 | } else { | ||
534 | var delta = (max - min); | ||
535 | saturation = delta / max; | ||
536 | |||
537 | if (red == max) { | ||
538 | hue = (green - blue) / delta; | ||
539 | } else if (green == max) { | ||
540 | hue = 2 + ((blue - red) / delta); | ||
541 | } else { | ||
542 | hue = 4 + ((red - green) / delta); | ||
543 | } | ||
544 | hue /= 6; | ||
545 | if (hue < 0) { | ||
546 | hue += 1; | ||
547 | } | ||
548 | if (hue > 1) { | ||
549 | hue -= 1; | ||
550 | } | ||
551 | } | ||
552 | return { | ||
553 | h: hue, | ||
554 | s: saturation, | ||
555 | v: value, | ||
556 | a: alpha | ||
557 | }; | ||
558 | }, | ||
559 | |||
560 | /** @id MochiKit.Color.rgbToHSL */ | ||
561 | rgbToHSL: function (red, green, blue, alpha) { | ||
562 | if (arguments.length == 1) { | ||
563 | var rgb = red; | ||
564 | red = rgb.r; | ||
565 | green = rgb.g; | ||
566 | blue = rgb.b; | ||
567 | alpha = rgb.a; | ||
568 | } | ||
569 | var max = Math.max(red, Math.max(green, blue)); | ||
570 | var min = Math.min(red, Math.min(green, blue)); | ||
571 | var hue; | ||
572 | var saturation; | ||
573 | var lightness = (max + min) / 2.0; | ||
574 | var delta = max - min; | ||
575 | if (delta === 0) { | ||
576 | hue = 0; | ||
577 | saturation = 0; | ||
578 | } else { | ||
579 | if (lightness <= 0.5) { | ||
580 | saturation = delta / (max + min); | ||
581 | } else { | ||
582 | saturation = delta / (2 - max - min); | ||
583 | } | ||
584 | if (red == max) { | ||
585 | hue = (green - blue) / delta; | ||
586 | } else if (green == max) { | ||
587 | hue = 2 + ((blue - red) / delta); | ||
588 | } else { | ||
589 | hue = 4 + ((red - green) / delta); | ||
590 | } | ||
591 | hue /= 6; | ||
592 | if (hue < 0) { | ||
593 | hue += 1; | ||
594 | } | ||
595 | if (hue > 1) { | ||
596 | hue -= 1; | ||
597 | } | ||
598 | |||
599 | } | ||
600 | return { | ||
601 | h: hue, | ||
602 | s: saturation, | ||
603 | l: lightness, | ||
604 | a: alpha | ||
605 | }; | ||
606 | }, | ||
607 | |||
608 | /** @id MochiKit.Color.toColorPart */ | ||
609 | toColorPart: function (num) { | ||
610 | num = Math.round(num); | ||
611 | var digits = num.toString(16); | ||
612 | if (num < 16) { | ||
613 | return '0' + digits; | ||
614 | } | ||
615 | return digits; | ||
616 | }, | ||
617 | |||
618 | __new__: function () { | ||
619 | var m = MochiKit.Base; | ||
620 | /** @id MochiKit.Color.Color.fromRGBString */ | ||
621 | this.Color.fromRGBString = m.bind( | ||
622 | this.Color._fromColorString, this.Color, "rgb", "fromRGB", | ||
623 | [1.0/255.0, 1.0/255.0, 1.0/255.0, 1] | ||
624 | ); | ||
625 | /** @id MochiKit.Color.Color.fromHSLString */ | ||
626 | this.Color.fromHSLString = m.bind( | ||
627 | this.Color._fromColorString, this.Color, "hsl", "fromHSL", | ||
628 | [1.0/360.0, 0.01, 0.01, 1] | ||
629 | ); | ||
630 | |||
631 | var third = 1.0 / 3.0; | ||
632 | /** @id MochiKit.Color.colors */ | ||
633 | var colors = { | ||
634 | // NSColor colors plus transparent | ||
635 | /** @id MochiKit.Color.blackColor */ | ||
636 | black: [0, 0, 0], | ||
637 | /** @id MochiKit.Color.blueColor */ | ||
638 | blue: [0, 0, 1], | ||
639 | /** @id MochiKit.Color.brownColor */ | ||
640 | brown: [0.6, 0.4, 0.2], | ||
641 | /** @id MochiKit.Color.cyanColor */ | ||
642 | cyan: [0, 1, 1], | ||
643 | /** @id MochiKit.Color.darkGrayColor */ | ||
644 | darkGray: [third, third, third], | ||
645 | /** @id MochiKit.Color.grayColor */ | ||
646 | gray: [0.5, 0.5, 0.5], | ||
647 | /** @id MochiKit.Color.greenColor */ | ||
648 | green: [0, 1, 0], | ||
649 | /** @id MochiKit.Color.lightGrayColor */ | ||
650 | lightGray: [2 * third, 2 * third, 2 * third], | ||
651 | /** @id MochiKit.Color.magentaColor */ | ||
652 | magenta: [1, 0, 1], | ||
653 | /** @id MochiKit.Color.orangeColor */ | ||
654 | orange: [1, 0.5, 0], | ||
655 | /** @id MochiKit.Color.purpleColor */ | ||
656 | purple: [0.5, 0, 0.5], | ||
657 | /** @id MochiKit.Color.redColor */ | ||
658 | red: [1, 0, 0], | ||
659 | /** @id MochiKit.Color.transparentColor */ | ||
660 | transparent: [0, 0, 0, 0], | ||
661 | /** @id MochiKit.Color.whiteColor */ | ||
662 | white: [1, 1, 1], | ||
663 | /** @id MochiKit.Color.yellowColor */ | ||
664 | yellow: [1, 1, 0] | ||
665 | }; | ||
666 | |||
667 | for (var k in colors) { | ||
668 | var name = k + "Color"; | ||
669 | var value = this.Color.fromRGB.apply(this.Color, colors[k]); | ||
670 | this.Color[name] = m.partial(m.operator.identity, value); | ||
671 | } | ||
672 | |||
673 | var isColor = function () { | ||
674 | for (var i = 0; i < arguments.length; i++) { | ||
675 | if (!(arguments[i] instanceof MochiKit.Color.Color)) { | ||
676 | return false; | ||
677 | } | ||
678 | } | ||
679 | return true; | ||
680 | }; | ||
681 | |||
682 | var compareColor = function (a, b) { | ||
683 | return a.compareRGB(b); | ||
684 | }; | ||
685 | |||
686 | m.nameFunctions(this); | ||
687 | |||
688 | m.registerComparator(this.Color.NAME, isColor, compareColor); | ||
689 | } | ||
690 | }); | ||
691 | |||
692 | MochiKit.Color.__new__(); | ||
693 | |||
694 | // Full table of css3 X11 colors <http://www.w3.org/TR/css3-color/#X11COLORS> | ||
695 | |||
696 | MochiKit.Color.Color._namedColors = { | ||
697 | aliceblue: "#f0f8ff", | ||
698 | antiquewhite: "#faebd7", | ||
699 | aqua: "#00ffff", | ||
700 | aquamarine: "#7fffd4", | ||
701 | azure: "#f0ffff", | ||
702 | beige: "#f5f5dc", | ||
703 | bisque: "#ffe4c4", | ||
704 | black: "#000000", | ||
705 | blanchedalmond: "#ffebcd", | ||
706 | blue: "#0000ff", | ||
707 | blueviolet: "#8a2be2", | ||
708 | brown: "#a52a2a", | ||
709 | burlywood: "#deb887", | ||
710 | cadetblue: "#5f9ea0", | ||
711 | chartreuse: "#7fff00", | ||
712 | chocolate: "#d2691e", | ||
713 | coral: "#ff7f50", | ||
714 | cornflowerblue: "#6495ed", | ||
715 | cornsilk: "#fff8dc", | ||
716 | crimson: "#dc143c", | ||
717 | cyan: "#00ffff", | ||
718 | darkblue: "#00008b", | ||
719 | darkcyan: "#008b8b", | ||
720 | darkgoldenrod: "#b8860b", | ||
721 | darkgray: "#a9a9a9", | ||
722 | darkgreen: "#006400", | ||
723 | darkgrey: "#a9a9a9", | ||
724 | darkkhaki: "#bdb76b", | ||
725 | darkmagenta: "#8b008b", | ||
726 | darkolivegreen: "#556b2f", | ||
727 | darkorange: "#ff8c00", | ||
728 | darkorchid: "#9932cc", | ||
729 | darkred: "#8b0000", | ||
730 | darksalmon: "#e9967a", | ||
731 | darkseagreen: "#8fbc8f", | ||
732 | darkslateblue: "#483d8b", | ||
733 | darkslategray: "#2f4f4f", | ||
734 | darkslategrey: "#2f4f4f", | ||
735 | darkturquoise: "#00ced1", | ||
736 | darkviolet: "#9400d3", | ||
737 | deeppink: "#ff1493", | ||
738 | deepskyblue: "#00bfff", | ||
739 | dimgray: "#696969", | ||
740 | dimgrey: "#696969", | ||
741 | dodgerblue: "#1e90ff", | ||
742 | firebrick: "#b22222", | ||
743 | floralwhite: "#fffaf0", | ||
744 | forestgreen: "#228b22", | ||
745 | fuchsia: "#ff00ff", | ||
746 | gainsboro: "#dcdcdc", | ||
747 | ghostwhite: "#f8f8ff", | ||
748 | gold: "#ffd700", | ||
749 | goldenrod: "#daa520", | ||
750 | gray: "#808080", | ||
751 | green: "#008000", | ||
752 | greenyellow: "#adff2f", | ||
753 | grey: "#808080", | ||
754 | honeydew: "#f0fff0", | ||
755 | hotpink: "#ff69b4", | ||
756 | indianred: "#cd5c5c", | ||
757 | indigo: "#4b0082", | ||
758 | ivory: "#fffff0", | ||
759 | khaki: "#f0e68c", | ||
760 | lavender: "#e6e6fa", | ||
761 | lavenderblush: "#fff0f5", | ||
762 | lawngreen: "#7cfc00", | ||
763 | lemonchiffon: "#fffacd", | ||
764 | lightblue: "#add8e6", | ||
765 | lightcoral: "#f08080", | ||
766 | lightcyan: "#e0ffff", | ||
767 | lightgoldenrodyellow: "#fafad2", | ||
768 | lightgray: "#d3d3d3", | ||
769 | lightgreen: "#90ee90", | ||
770 | lightgrey: "#d3d3d3", | ||
771 | lightpink: "#ffb6c1", | ||
772 | lightsalmon: "#ffa07a", | ||
773 | lightseagreen: "#20b2aa", | ||
774 | lightskyblue: "#87cefa", | ||
775 | lightslategray: "#778899", | ||
776 | lightslategrey: "#778899", | ||
777 | lightsteelblue: "#b0c4de", | ||
778 | lightyellow: "#ffffe0", | ||
779 | lime: "#00ff00", | ||
780 | limegreen: "#32cd32", | ||
781 | linen: "#faf0e6", | ||
782 | magenta: "#ff00ff", | ||
783 | maroon: "#800000", | ||
784 | mediumaquamarine: "#66cdaa", | ||
785 | mediumblue: "#0000cd", | ||
786 | mediumorchid: "#ba55d3", | ||
787 | mediumpurple: "#9370db", | ||
788 | mediumseagreen: "#3cb371", | ||
789 | mediumslateblue: "#7b68ee", | ||
790 | mediumspringgreen: "#00fa9a", | ||
791 | mediumturquoise: "#48d1cc", | ||
792 | mediumvioletred: "#c71585", | ||
793 | midnightblue: "#191970", | ||
794 | mintcream: "#f5fffa", | ||
795 | mistyrose: "#ffe4e1", | ||
796 | moccasin: "#ffe4b5", | ||
797 | navajowhite: "#ffdead", | ||
798 | navy: "#000080", | ||
799 | oldlace: "#fdf5e6", | ||
800 | olive: "#808000", | ||
801 | olivedrab: "#6b8e23", | ||
802 | orange: "#ffa500", | ||
803 | orangered: "#ff4500", | ||
804 | orchid: "#da70d6", | ||
805 | palegoldenrod: "#eee8aa", | ||
806 | palegreen: "#98fb98", | ||
807 | paleturquoise: "#afeeee", | ||
808 | palevioletred: "#db7093", | ||
809 | papayawhip: "#ffefd5", | ||
810 | peachpuff: "#ffdab9", | ||
811 | peru: "#cd853f", | ||
812 | pink: "#ffc0cb", | ||
813 | plum: "#dda0dd", | ||
814 | powderblue: "#b0e0e6", | ||
815 | purple: "#800080", | ||
816 | red: "#ff0000", | ||
817 | rosybrown: "#bc8f8f", | ||
818 | royalblue: "#4169e1", | ||
819 | saddlebrown: "#8b4513", | ||
820 | salmon: "#fa8072", | ||
821 | sandybrown: "#f4a460", | ||
822 | seagreen: "#2e8b57", | ||
823 | seashell: "#fff5ee", | ||
824 | sienna: "#a0522d", | ||
825 | silver: "#c0c0c0", | ||
826 | skyblue: "#87ceeb", | ||
827 | slateblue: "#6a5acd", | ||
828 | slategray: "#708090", | ||
829 | slategrey: "#708090", | ||
830 | snow: "#fffafa", | ||
831 | springgreen: "#00ff7f", | ||
832 | steelblue: "#4682b4", | ||
833 | tan: "#d2b48c", | ||
834 | teal: "#008080", | ||
835 | thistle: "#d8bfd8", | ||
836 | tomato: "#ff6347", | ||
837 | turquoise: "#40e0d0", | ||
838 | violet: "#ee82ee", | ||
839 | wheat: "#f5deb3", | ||
840 | white: "#ffffff", | ||
841 | whitesmoke: "#f5f5f5", | ||
842 | yellow: "#ffff00", | ||
843 | yellowgreen: "#9acd32" | ||
844 | }; | ||
845 | |||
846 | MochiKit.Base._exportSymbols(this, MochiKit.Color); | ||