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