Mercurial > repos > adam-novak > hexagram
comparison hexagram-6ae12361157c/hexagram/color-0.4.1.js @ 0:1407e3634bcf draft default tip
Uploaded r11 from test tool shed.
author | adam-novak |
---|---|
date | Tue, 22 Oct 2013 14:17:59 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:1407e3634bcf |
---|---|
1 (function(){var global = this;function debug(){return debug};function require(p, parent){ var path = require.resolve(p) , mod = require.modules[path]; if (!mod) throw new Error('failed to require "' + p + '" from ' + parent); if (!mod.exports) { mod.exports = {}; mod.call(mod.exports, mod, mod.exports, require.relative(path), global); } return mod.exports;}require.modules = {};require.resolve = function(path){ var orig = path , reg = path + '.js' , index = path + '/index.js'; return require.modules[reg] && reg || require.modules[index] && index || orig;};require.register = function(path, fn){ require.modules[path] = fn;};require.relative = function(parent) { return function(p){ if ('debug' == p) return debug; if ('.' != p.charAt(0)) return require(p); var path = parent.split('/') , segs = p.split('/'); path.pop(); for (var i = 0; i < segs.length; i++) { var seg = segs[i]; if ('..' == seg) path.pop(); else if ('.' != seg) path.push(seg); } return require(path.join('/'), parent); };};require.register("color.js", function(module, exports, require, global){ | |
2 /* MIT license */ | |
3 var convert = require("color-convert"), | |
4 string = require("color-string"); | |
5 | |
6 module.exports = function(cssString) { | |
7 return new Color(cssString); | |
8 }; | |
9 | |
10 var Color = function(cssString) { | |
11 this.values = { | |
12 rgb: [0, 0, 0], | |
13 hsl: [0, 0, 0], | |
14 hsv: [0, 0, 0], | |
15 cmyk: [0, 0, 0, 0], | |
16 alpha: 1 | |
17 } | |
18 | |
19 // parse Color() argument | |
20 if (typeof cssString == "string") { | |
21 var vals = string.getRgba(cssString); | |
22 if (vals) { | |
23 this.setValues("rgb", vals); | |
24 } | |
25 else if(vals = string.getHsla(cssString)) { | |
26 this.setValues("hsl", vals); | |
27 } | |
28 } | |
29 else if (typeof cssString == "object") { | |
30 var vals = cssString; | |
31 if(vals["r"] !== undefined || vals["red"] !== undefined) { | |
32 this.setValues("rgb", vals) | |
33 } | |
34 else if(vals["l"] !== undefined || vals["lightness"] !== undefined) { | |
35 this.setValues("hsl", vals) | |
36 } | |
37 else if(vals["v"] !== undefined || vals["value"] !== undefined) { | |
38 this.setValues("hsv", vals) | |
39 } | |
40 else if(vals["c"] !== undefined || vals["cyan"] !== undefined) { | |
41 this.setValues("cmyk", vals) | |
42 } | |
43 } | |
44 } | |
45 | |
46 Color.prototype = { | |
47 rgb: function (vals) { | |
48 return this.setSpace("rgb", arguments); | |
49 }, | |
50 hsl: function(vals) { | |
51 return this.setSpace("hsl", arguments); | |
52 }, | |
53 hsv: function(vals) { | |
54 return this.setSpace("hsv", arguments); | |
55 }, | |
56 cmyk: function(vals) { | |
57 return this.setSpace("cmyk", arguments); | |
58 }, | |
59 | |
60 rgbArray: function() { | |
61 return this.values.rgb; | |
62 }, | |
63 hslArray: function() { | |
64 return this.values.hsl; | |
65 }, | |
66 hsvArray: function() { | |
67 return this.values.hsv; | |
68 }, | |
69 cmykArray: function() { | |
70 return this.values.cmyk; | |
71 }, | |
72 rgbaArray: function() { | |
73 var rgb = this.values.rgb; | |
74 rgb.push(this.values.alpha); | |
75 return rgb; | |
76 }, | |
77 hslaArray: function() { | |
78 var hsl = this.values.hsl; | |
79 hsl.push(this.values.alpha); | |
80 return hsl; | |
81 }, | |
82 | |
83 alpha: function(val) { | |
84 if (val === undefined) { | |
85 return this.values.alpha; | |
86 } | |
87 this.setValues("alpha", val); | |
88 return this; | |
89 }, | |
90 | |
91 red: function(val) { | |
92 return this.setChannel("rgb", 0, val); | |
93 }, | |
94 green: function(val) { | |
95 return this.setChannel("rgb", 1, val); | |
96 }, | |
97 blue: function(val) { | |
98 return this.setChannel("rgb", 2, val); | |
99 }, | |
100 hue: function(val) { | |
101 return this.setChannel("hsl", 0, val); | |
102 }, | |
103 saturation: function(val) { | |
104 return this.setChannel("hsl", 1, val); | |
105 }, | |
106 lightness: function(val) { | |
107 return this.setChannel("hsl", 2, val); | |
108 }, | |
109 saturationv: function(val) { | |
110 return this.setChannel("hsv", 1, val); | |
111 }, | |
112 value: function(val) { | |
113 return this.setChannel("hsv", 2, val); | |
114 }, | |
115 cyan: function(val) { | |
116 return this.setChannel("cmyk", 0, val); | |
117 }, | |
118 magenta: function(val) { | |
119 return this.setChannel("cmyk", 1, val); | |
120 }, | |
121 yellow: function(val) { | |
122 return this.setChannel("cmyk", 2, val); | |
123 }, | |
124 black: function(val) { | |
125 return this.setChannel("cmyk", 3, val); | |
126 }, | |
127 | |
128 hexString: function() { | |
129 return string.hexString(this.values.rgb); | |
130 }, | |
131 rgbString: function() { | |
132 return string.rgbString(this.values.rgb, this.values.alpha); | |
133 }, | |
134 rgbaString: function() { | |
135 return string.rgbaString(this.values.rgb, this.values.alpha); | |
136 }, | |
137 percentString: function() { | |
138 return string.percentString(this.values.rgb, this.values.alpha); | |
139 }, | |
140 hslString: function() { | |
141 return string.hslString(this.values.hsl, this.values.alpha); | |
142 }, | |
143 hslaString: function() { | |
144 return string.hslaString(this.values.hsl, this.values.alpha); | |
145 }, | |
146 keyword: function() { | |
147 return string.keyword(this.values.rgb, this.values.alpha); | |
148 }, | |
149 | |
150 luminosity: function() { | |
151 // http://www.w3.org/TR/WCAG20/#relativeluminancedef | |
152 var rgb = this.values.rgb; | |
153 for (var i = 0; i < rgb.length; i++) { | |
154 var chan = rgb[i] / 255; | |
155 rgb[i] = (chan <= 0.03928) ? chan / 12.92 | |
156 : Math.pow(((chan + 0.055) / 1.055), 2.4) | |
157 } | |
158 return 0.2126 * rgb[0] + 0.7152 * rgb[1] + 0.0722 * rgb[2]; | |
159 }, | |
160 | |
161 contrast: function(color2) { | |
162 // http://www.w3.org/TR/WCAG20/#contrast-ratiodef | |
163 var lum1 = this.luminosity(); | |
164 var lum2 = color2.luminosity(); | |
165 if (lum1 > lum2) { | |
166 return (lum1 + 0.05) / (lum2 + 0.05) | |
167 }; | |
168 return (lum2 + 0.05) / (lum1 + 0.05); | |
169 }, | |
170 | |
171 dark: function() { | |
172 // YIQ equation from http://24ways.org/2010/calculating-color-contrast | |
173 var rgb = this.values.rgb, | |
174 yiq = (rgb[0] * 299 + rgb[1] * 587 + rgb[2] * 114) / 1000; | |
175 return yiq < 128; | |
176 }, | |
177 | |
178 light: function() { | |
179 return !this.dark(); | |
180 }, | |
181 | |
182 negate: function() { | |
183 var rgb = [] | |
184 for (var i = 0; i < 3; i++) { | |
185 rgb[i] = 255 - this.values.rgb[i]; | |
186 } | |
187 this.setValues("rgb", rgb); | |
188 return this; | |
189 }, | |
190 | |
191 lighten: function(ratio) { | |
192 this.values.hsl[2] += this.values.hsl[2] * ratio; | |
193 this.setValues("hsl", this.values.hsl); | |
194 return this; | |
195 }, | |
196 | |
197 darken: function(ratio) { | |
198 this.values.hsl[2] -= this.values.hsl[2] * ratio; | |
199 this.setValues("hsl", this.values.hsl); | |
200 return this; | |
201 }, | |
202 | |
203 saturate: function(ratio) { | |
204 this.values.hsl[1] += this.values.hsl[1] * ratio; | |
205 this.setValues("hsl", this.values.hsl); | |
206 return this; | |
207 }, | |
208 | |
209 desaturate: function(ratio) { | |
210 this.values.hsl[1] -= this.values.hsl[1] * ratio; | |
211 this.setValues("hsl", this.values.hsl); | |
212 return this; | |
213 }, | |
214 | |
215 greyscale: function() { | |
216 var rgb = this.values.rgb; | |
217 // http://en.wikipedia.org/wiki/Grayscale#Converting_color_to_grayscale | |
218 var val = rgb[0] * 0.3 + rgb[1] * 0.59 + rgb[2] * 0.11; | |
219 this.setValues("rgb", [val, val, val]); | |
220 return this; | |
221 }, | |
222 | |
223 clearer: function(ratio) { | |
224 this.setValues("alpha", this.values.alpha - (this.values.alpha * ratio)); | |
225 return this; | |
226 }, | |
227 | |
228 opaquer: function(ratio) { | |
229 this.setValues("alpha", this.values.alpha + (this.values.alpha * ratio)); | |
230 return this; | |
231 }, | |
232 | |
233 rotate: function(degrees) { | |
234 var hue = this.values.hsl[0]; | |
235 hue = (hue + degrees) % 360; | |
236 hue = hue < 0 ? 360 + hue : hue; | |
237 this.values.hsl[0] = hue; | |
238 this.setValues("hsl", this.values.hsl); | |
239 return this; | |
240 }, | |
241 | |
242 mix: function(color2, weight) { | |
243 weight = 1 - (weight || 0.5); | |
244 | |
245 // algorithm from Sass's mix(). Ratio of first color in mix is | |
246 // determined by the alphas of both colors and the weight | |
247 var t1 = weight * 2 - 1, | |
248 d = this.alpha() - color2.alpha(); | |
249 | |
250 var weight1 = (((t1 * d == -1) ? t1 : (t1 + d) / (1 + t1 * d)) + 1) / 2; | |
251 var weight2 = 1 - weight1; | |
252 | |
253 var rgb = this.rgbArray(); | |
254 var rgb2 = color2.rgbArray(); | |
255 | |
256 for (var i = 0; i < rgb.length; i++) { | |
257 rgb[i] = rgb[i] * weight1 + rgb2[i] * weight2; | |
258 } | |
259 this.setValues("rgb", rgb); | |
260 | |
261 var alpha = this.alpha() * weight + color2.alpha() * (1 - weight); | |
262 this.setValues("alpha", alpha); | |
263 | |
264 return this; | |
265 }, | |
266 | |
267 toJSON: function() { | |
268 return this.rgb(); | |
269 } | |
270 } | |
271 | |
272 | |
273 Color.prototype.getValues = function(space) { | |
274 var vals = {}; | |
275 for (var i = 0; i < space.length; i++) { | |
276 vals[space[i]] = this.values[space][i]; | |
277 } | |
278 if (this.values.alpha != 1) { | |
279 vals["a"] = this.values.alpha; | |
280 } | |
281 // {r: 255, g: 255, b: 255, a: 0.4} | |
282 return vals; | |
283 } | |
284 | |
285 Color.prototype.setValues = function(space, vals) { | |
286 var spaces = { | |
287 "rgb": ["red", "green", "blue"], | |
288 "hsl": ["hue", "saturation", "lightness"], | |
289 "hsv": ["hue", "saturation", "value"], | |
290 "cmyk": ["cyan", "magenta", "yellow", "black"] | |
291 }; | |
292 | |
293 var maxes = { | |
294 "rgb": [255, 255, 255], | |
295 "hsl": [360, 100, 100], | |
296 "hsv": [360, 100, 100], | |
297 "cmyk": [100, 100, 100, 100], | |
298 }; | |
299 | |
300 var alpha = 1; | |
301 if (space == "alpha") { | |
302 alpha = vals; | |
303 } | |
304 else if (vals.length) { | |
305 // [10, 10, 10] | |
306 this.values[space] = vals.slice(0, space.length); | |
307 alpha = vals[space.length]; | |
308 } | |
309 else if (vals[space[0]] !== undefined) { | |
310 // {r: 10, g: 10, b: 10} | |
311 for (var i = 0; i < space.length; i++) { | |
312 this.values[space][i] = vals[space[i]]; | |
313 } | |
314 alpha = vals.a; | |
315 } | |
316 else if (vals[spaces[space][0]] !== undefined) { | |
317 // {red: 10, green: 10, blue: 10} | |
318 var chans = spaces[space]; | |
319 for (var i = 0; i < space.length; i++) { | |
320 this.values[space][i] = vals[chans[i]]; | |
321 } | |
322 alpha = vals.alpha; | |
323 } | |
324 this.values.alpha = Math.max(0, Math.min(1, alpha || this.values.alpha)); | |
325 if (space == "alpha") { | |
326 return; | |
327 } | |
328 | |
329 // convert to all the other color spaces | |
330 for (var sname in spaces) { | |
331 if (sname != space) { | |
332 this.values[sname] = convert[space][sname](this.values[space]) | |
333 } | |
334 | |
335 // cap values | |
336 for (var i = 0; i < sname.length; i++) { | |
337 var capped = Math.max(0, Math.min(maxes[sname][i], this.values[sname][i])); | |
338 this.values[sname][i] = Math.round(capped); | |
339 } | |
340 } | |
341 return true; | |
342 } | |
343 | |
344 Color.prototype.setSpace = function(space, args) { | |
345 var vals = args[0]; | |
346 if (vals === undefined) { | |
347 // color.rgb() | |
348 return this.getValues(space); | |
349 } | |
350 // color.rgb(10, 10, 10) | |
351 if (typeof vals == "number") { | |
352 vals = Array.prototype.slice.call(args); | |
353 } | |
354 this.setValues(space, vals); | |
355 return this; | |
356 } | |
357 | |
358 Color.prototype.setChannel = function(space, index, val) { | |
359 if (val === undefined) { | |
360 // color.red() | |
361 return this.values[space][index]; | |
362 } | |
363 // color.red(100) | |
364 this.values[space][index] = val; | |
365 this.setValues(space, this.values[space]); | |
366 return this; | |
367 } | |
368 | |
369 });require.register("color-string", function(module, exports, require, global){ | |
370 /* MIT license */ | |
371 var convert = require("color-convert"); | |
372 | |
373 module.exports = { | |
374 getRgba: getRgba, | |
375 getHsla: getHsla, | |
376 getRgb: getRgb, | |
377 getHsl: getHsl, | |
378 getAlpha: getAlpha, | |
379 | |
380 hexString: hexString, | |
381 rgbString: rgbString, | |
382 rgbaString: rgbaString, | |
383 percentString: percentString, | |
384 percentaString: percentaString, | |
385 hslString: hslString, | |
386 hslaString: hslaString, | |
387 keyword: keyword | |
388 } | |
389 | |
390 function getRgba(string) { | |
391 if (!string) { | |
392 return; | |
393 } | |
394 var abbr = /^#([a-fA-F0-9]{3})$/, | |
395 hex = /^#([a-fA-F0-9]{6})$/, | |
396 rgba = /^rgba?\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*(?:,\s*([\d\.]+)\s*)?\)$/, | |
397 per = /^rgba?\(\s*([\d\.]+)\%\s*,\s*([\d\.]+)\%\s*,\s*([\d\.]+)\%\s*(?:,\s*([\d\.]+)\s*)?\)$/, | |
398 keyword = /(\D+)/; | |
399 | |
400 var rgb = [0, 0, 0], | |
401 a = 1, | |
402 match = string.match(abbr); | |
403 if (match) { | |
404 match = match[1]; | |
405 for (var i = 0; i < rgb.length; i++) { | |
406 rgb[i] = parseInt(match[i] + match[i], 16); | |
407 } | |
408 } | |
409 else if (match = string.match(hex)) { | |
410 match = match[1]; | |
411 for (var i = 0; i < rgb.length; i++) { | |
412 rgb[i] = parseInt(match.slice(i * 2, i * 2 + 2), 16); | |
413 } | |
414 } | |
415 else if (match = string.match(rgba)) { | |
416 for (var i = 0; i < rgb.length; i++) { | |
417 rgb[i] = parseInt(match[i + 1]); | |
418 } | |
419 a = parseFloat(match[4]); | |
420 } | |
421 else if (match = string.match(per)) { | |
422 for (var i = 0; i < rgb.length; i++) { | |
423 rgb[i] = Math.round(parseFloat(match[i + 1]) * 2.55); | |
424 } | |
425 a = parseFloat(match[4]); | |
426 } | |
427 else if (match = string.match(keyword)) { | |
428 if (match[1] == "transparent") { | |
429 return [0, 0, 0, 0]; | |
430 } | |
431 rgb = convert.keyword2rgb(match[1]); | |
432 if (!rgb) { | |
433 return; | |
434 } | |
435 } | |
436 | |
437 for (var i = 0; i < rgb.length; i++) { | |
438 rgb[i] = scale(rgb[i], 0, 255); | |
439 } | |
440 if (!a) { | |
441 a = 1; | |
442 } | |
443 else { | |
444 a = scale(a, 0, 1); | |
445 } | |
446 rgb.push(a); | |
447 return rgb; | |
448 } | |
449 | |
450 function getHsla(string) { | |
451 if (!string) { | |
452 return; | |
453 } | |
454 var hsl = /^hsla?\(\s*(\d+)\s*,\s*([\d\.]+)%\s*,\s*([\d\.]+)%\s*(?:,\s*([\d\.]+)\s*)?\)/; | |
455 var match = string.match(hsl); | |
456 if (match) { | |
457 var h = scale(parseInt(match[1]), 0, 360), | |
458 s = scale(parseFloat(match[2]), 0, 100), | |
459 l = scale(parseFloat(match[3]), 0, 100), | |
460 a = scale(parseFloat(match[4]) || 1, 0, 1); | |
461 return [h, s, l, a]; | |
462 } | |
463 } | |
464 | |
465 function getRgb(string) { | |
466 var rgba = getRgba(string); | |
467 return rgba && rgba.slice(0, 3); | |
468 } | |
469 | |
470 function getHsl(string) { | |
471 var hsla = getHsla(string); | |
472 return hsla && hsla.slice(0, 3); | |
473 } | |
474 | |
475 function getAlpha(string) { | |
476 var vals = getRgba(string); | |
477 if (vals) { | |
478 return vals[3]; | |
479 } | |
480 else if (vals = getHsla(string)) { | |
481 return vals[3]; | |
482 } | |
483 } | |
484 | |
485 // generators | |
486 function hexString(rgb) { | |
487 return "#" + hexDouble(rgb[0]) + hexDouble(rgb[1]) | |
488 + hexDouble(rgb[2]); | |
489 } | |
490 | |
491 function rgbString(rgba, alpha) { | |
492 if (alpha < 1 || (rgba[3] && rgba[3] < 1)) { | |
493 return rgbaString(rgba, alpha); | |
494 } | |
495 return "rgb(" + rgba[0] + ", " + rgba[1] + ", " + rgba[2] + ")"; | |
496 } | |
497 | |
498 function rgbaString(rgba, alpha) { | |
499 return "rgba(" + rgba[0] + ", " + rgba[1] + ", " + rgba[2] | |
500 + ", " + (alpha || rgba[3] || 1) + ")"; | |
501 } | |
502 | |
503 function percentString(rgba, alpha) { | |
504 if (alpha < 1 || (rgba[3] && rgba[3] < 1)) { | |
505 return percentaString(rgba, alpha); | |
506 } | |
507 var r = Math.round(rgba[0]/255 * 100), | |
508 g = Math.round(rgba[1]/255 * 100), | |
509 b = Math.round(rgba[2]/255 * 100); | |
510 | |
511 return "rgb(" + r + "%, " + g + "%, " + b + "%)"; | |
512 } | |
513 | |
514 function percentaString(rgba, alpha) { | |
515 var r = Math.round(rgba[0]/255 * 100), | |
516 g = Math.round(rgba[1]/255 * 100), | |
517 b = Math.round(rgba[2]/255 * 100); | |
518 return "rgba(" + r + "%, " + g + "%, " + b + "%, " + (alpha || rgba[3] || 1) + ")"; | |
519 } | |
520 | |
521 function hslString(hsla, alpha) { | |
522 if (alpha < 1 || (hsla[3] && hsla[3] < 1)) { | |
523 return hslaString(hsla, alpha); | |
524 } | |
525 return "hsl(" + hsla[0] + ", " + hsla[1] + "%, " + hsla[2] + "%)"; | |
526 } | |
527 | |
528 function hslaString(hsla, alpha) { | |
529 return "hsla(" + hsla[0] + ", " + hsla[1] + "%, " + hsla[2] + "%, " | |
530 + (alpha || hsla[3] || 1) + ")"; | |
531 } | |
532 | |
533 function keyword(rgb) { | |
534 return convert.rgb2keyword(rgb.slice(0, 3)); | |
535 } | |
536 | |
537 // helpers | |
538 function scale(num, min, max) { | |
539 return Math.min(Math.max(min, num), max); | |
540 } | |
541 | |
542 function hexDouble(num) { | |
543 var str = num.toString(16).toUpperCase(); | |
544 return (str.length < 2) ? "0" + str : str; | |
545 } | |
546 | |
547 });require.register("color-convert", function(module, exports, require, global){ | |
548 var conversions = require("conversions"); | |
549 | |
550 var convert = function() { | |
551 return new Converter(); | |
552 } | |
553 | |
554 for (var func in conversions) { | |
555 // export Raw versions | |
556 convert[func + "Raw"] = (function(func) { | |
557 // accept array or plain args | |
558 return function(arg) { | |
559 if (typeof arg == "number") | |
560 arg = Array.prototype.slice.call(arguments); | |
561 return conversions[func](arg); | |
562 } | |
563 })(func); | |
564 | |
565 var pair = /(\w+)2(\w+)/.exec(func), | |
566 from = pair[1], | |
567 to = pair[2]; | |
568 | |
569 // export rgb2hsl and ["rgb"]["hsl"] | |
570 convert[from] = convert[from] || {}; | |
571 | |
572 convert[from][to] = convert[func] = (function(func) { | |
573 return function(arg) { | |
574 if (typeof arg == "number") { | |
575 arg = Array.prototype.slice.call(arguments); | |
576 } | |
577 | |
578 var val = conversions[func](arg); | |
579 if (typeof val == "string" || val === undefined) { | |
580 return val; // keyword | |
581 } | |
582 | |
583 round(val) | |
584 return val; | |
585 } | |
586 })(func); | |
587 } | |
588 | |
589 | |
590 /* Converter does lazy conversion and caching */ | |
591 var Converter = function() { | |
592 this.space = "rgb"; | |
593 this.convs = { | |
594 'rgb': [0, 0, 0] | |
595 }; | |
596 }; | |
597 | |
598 /* Either get the values for a space or | |
599 set the values for a space, depending on args */ | |
600 Converter.prototype.routeSpace = function(space, args) { | |
601 var values = args[0]; | |
602 if (values === undefined) { | |
603 // color.rgb() | |
604 return this.getValues(space); | |
605 } | |
606 // color.rgb(10, 10, 10) | |
607 if (typeof values == "number") { | |
608 values = Array.prototype.slice.call(args); | |
609 } | |
610 | |
611 return this.setValues(space, values); | |
612 }; | |
613 | |
614 /* Set the values for a space, invalidating cache */ | |
615 Converter.prototype.setValues = function(space, values) { | |
616 this.space = space; | |
617 this.convs = {}; | |
618 this.convs[space] = values; | |
619 return this; | |
620 }; | |
621 | |
622 /* Get the values for a space. If there's already | |
623 a conversion for the space, fetch it, otherwise | |
624 compute it */ | |
625 Converter.prototype.getValues = function(space) { | |
626 var vals = this.convs[space]; | |
627 if (!vals) { | |
628 var fspace = this.space, | |
629 from = this.convs[fspace]; | |
630 vals = convert[fspace][space](from); | |
631 | |
632 this.convs[space] = vals; | |
633 } | |
634 else { | |
635 round(vals); | |
636 } | |
637 return vals; | |
638 }; | |
639 | |
640 function round(val) { | |
641 for (var i = 0; i < val.length; i++) { | |
642 val[i] = Math.round(val[i]); | |
643 } | |
644 }; | |
645 | |
646 ["rgb", "hsl", "hsv", "cmyk", "keyword"].forEach(function(space) { | |
647 Converter.prototype[space] = function(vals) { | |
648 return this.routeSpace(space, arguments); | |
649 } | |
650 }); | |
651 | |
652 module.exports = convert; | |
653 });require.register("conversions", function(module, exports, require, global){ | |
654 /* MIT license */ | |
655 | |
656 module.exports = { | |
657 rgb2hsl: rgb2hsl, | |
658 rgb2hsv: rgb2hsv, | |
659 rgb2cmyk: rgb2cmyk, | |
660 rgb2keyword: rgb2keyword, | |
661 rgb2xyz: rgb2xyz, | |
662 rgb2lab: rgb2lab, | |
663 | |
664 hsl2rgb: hsl2rgb, | |
665 hsl2hsv: hsl2hsv, | |
666 hsl2cmyk: hsl2cmyk, | |
667 hsl2keyword: hsl2keyword, | |
668 | |
669 hsv2rgb: hsv2rgb, | |
670 hsv2hsl: hsv2hsl, | |
671 hsv2cmyk: hsv2cmyk, | |
672 hsv2keyword: hsv2keyword, | |
673 | |
674 cmyk2rgb: cmyk2rgb, | |
675 cmyk2hsl: cmyk2hsl, | |
676 cmyk2hsv: cmyk2hsv, | |
677 cmyk2keyword: cmyk2keyword, | |
678 | |
679 keyword2rgb: keyword2rgb, | |
680 keyword2hsl: keyword2hsl, | |
681 keyword2hsv: keyword2hsv, | |
682 keyword2cmyk: keyword2cmyk, | |
683 keyword2lab: keyword2lab, | |
684 keyword2xyz: keyword2xyz, | |
685 | |
686 xyz2rgb: xyz2rgb, | |
687 xyz2lab: xyz2lab, | |
688 | |
689 lab2xyz: lab2xyz, | |
690 } | |
691 | |
692 | |
693 function rgb2hsl(rgb) { | |
694 var r = rgb[0]/255, | |
695 g = rgb[1]/255, | |
696 b = rgb[2]/255, | |
697 min = Math.min(r, g, b), | |
698 max = Math.max(r, g, b), | |
699 delta = max - min, | |
700 h, s, l; | |
701 | |
702 if (max == min) | |
703 h = 0; | |
704 else if (r == max) | |
705 h = (g - b) / delta; | |
706 else if (g == max) | |
707 h = 2 + (b - r) / delta; | |
708 else if (b == max) | |
709 h = 4 + (r - g)/ delta; | |
710 | |
711 h = Math.min(h * 60, 360); | |
712 | |
713 if (h < 0) | |
714 h += 360; | |
715 | |
716 l = (min + max) / 2; | |
717 | |
718 if (max == min) | |
719 s = 0; | |
720 else if (l <= 0.5) | |
721 s = delta / (max + min); | |
722 else | |
723 s = delta / (2 - max - min); | |
724 | |
725 return [h, s * 100, l * 100]; | |
726 } | |
727 | |
728 function rgb2hsv(rgb) { | |
729 var r = rgb[0], | |
730 g = rgb[1], | |
731 b = rgb[2], | |
732 min = Math.min(r, g, b), | |
733 max = Math.max(r, g, b), | |
734 delta = max - min, | |
735 h, s, v; | |
736 | |
737 if (max == 0) | |
738 s = 0; | |
739 else | |
740 s = (delta/max * 1000)/10; | |
741 | |
742 if (max == min) | |
743 h = 0; | |
744 else if (r == max) | |
745 h = (g - b) / delta; | |
746 else if (g == max) | |
747 h = 2 + (b - r) / delta; | |
748 else if (b == max) | |
749 h = 4 + (r - g) / delta; | |
750 | |
751 h = Math.min(h * 60, 360); | |
752 | |
753 if (h < 0) | |
754 h += 360; | |
755 | |
756 v = ((max / 255) * 1000) / 10; | |
757 | |
758 return [h, s, v]; | |
759 } | |
760 | |
761 function rgb2cmyk(rgb) { | |
762 var r = rgb[0] / 255, | |
763 g = rgb[1] / 255, | |
764 b = rgb[2] / 255, | |
765 c, m, y, k; | |
766 | |
767 k = Math.min(1 - r, 1 - g, 1 - b); | |
768 c = (1 - r - k) / (1 - k); | |
769 m = (1 - g - k) / (1 - k); | |
770 y = (1 - b - k) / (1 - k); | |
771 return [c * 100, m * 100, y * 100, k * 100]; | |
772 } | |
773 | |
774 function rgb2keyword(rgb) { | |
775 return reverseKeywords[JSON.stringify(rgb)]; | |
776 } | |
777 | |
778 function rgb2xyz(rgb) { | |
779 var r = rgb[0] / 255, | |
780 g = rgb[1] / 255, | |
781 b = rgb[2] / 255; | |
782 | |
783 // assume sRGB | |
784 r = r > 0.04045 ? Math.pow(((r + 0.055) / 1.055), 2.4) : (r / 12.92); | |
785 g = g > 0.04045 ? Math.pow(((g + 0.055) / 1.055), 2.4) : (g / 12.92); | |
786 b = b > 0.04045 ? Math.pow(((b + 0.055) / 1.055), 2.4) : (b / 12.92); | |
787 | |
788 var x = (r * 0.4124) + (g * 0.3576) + (b * 0.1805); | |
789 var y = (r * 0.2126) + (g * 0.7152) + (b * 0.0722); | |
790 var z = (r * 0.0193) + (g * 0.1192) + (b * 0.9505); | |
791 | |
792 return [x * 100, y *100, z * 100]; | |
793 } | |
794 | |
795 function rgb2lab(rgb) { | |
796 var xyz = rgb2xyz(rgb), | |
797 x = xyz[0], | |
798 y = xyz[1], | |
799 z = xyz[2], | |
800 l, a, b; | |
801 | |
802 x /= 95.047; | |
803 y /= 100; | |
804 z /= 108.883; | |
805 | |
806 x = x > 0.008856 ? Math.pow(x, 1/3) : (7.787 * x) + (16 / 116); | |
807 y = y > 0.008856 ? Math.pow(y, 1/3) : (7.787 * y) + (16 / 116); | |
808 z = z > 0.008856 ? Math.pow(z, 1/3) : (7.787 * z) + (16 / 116); | |
809 | |
810 l = (116 * y) - 16; | |
811 a = 500 * (x - y); | |
812 b = 200 * (y - z); | |
813 | |
814 return [l, a, b]; | |
815 } | |
816 | |
817 | |
818 function hsl2rgb(hsl) { | |
819 var h = hsl[0] / 360, | |
820 s = hsl[1] / 100, | |
821 l = hsl[2] / 100, | |
822 t1, t2, t3, rgb, val; | |
823 | |
824 if (s == 0) { | |
825 val = l * 255; | |
826 return [val, val, val]; | |
827 } | |
828 | |
829 if (l < 0.5) | |
830 t2 = l * (1 + s); | |
831 else | |
832 t2 = l + s - l * s; | |
833 t1 = 2 * l - t2; | |
834 | |
835 rgb = [0, 0, 0]; | |
836 for (var i = 0; i < 3; i++) { | |
837 t3 = h + 1 / 3 * - (i - 1); | |
838 t3 < 0 && t3++; | |
839 t3 > 1 && t3--; | |
840 | |
841 if (6 * t3 < 1) | |
842 val = t1 + (t2 - t1) * 6 * t3; | |
843 else if (2 * t3 < 1) | |
844 val = t2; | |
845 else if (3 * t3 < 2) | |
846 val = t1 + (t2 - t1) * (2 / 3 - t3) * 6; | |
847 else | |
848 val = t1; | |
849 | |
850 rgb[i] = val * 255; | |
851 } | |
852 | |
853 return rgb; | |
854 } | |
855 | |
856 function hsl2hsv(hsl) { | |
857 var h = hsl[0], | |
858 s = hsl[1] / 100, | |
859 l = hsl[2] / 100, | |
860 sv, v; | |
861 l *= 2; | |
862 s *= (l <= 1) ? l : 2 - l; | |
863 v = (l + s) / 2; | |
864 sv = (2 * s) / (l + s); | |
865 return [h, sv * 100, v * 100]; | |
866 } | |
867 | |
868 function hsl2cmyk(args) { | |
869 return rgb2cmyk(hsl2rgb(args)); | |
870 } | |
871 | |
872 function hsl2keyword(args) { | |
873 return rgb2keyword(hsl2rgb(args)); | |
874 } | |
875 | |
876 | |
877 function hsv2rgb(hsv) { | |
878 var h = hsv[0] / 60, | |
879 s = hsv[1] / 100, | |
880 v = hsv[2] / 100, | |
881 hi = Math.floor(h) % 6; | |
882 | |
883 var f = h - Math.floor(h), | |
884 p = 255 * v * (1 - s), | |
885 q = 255 * v * (1 - (s * f)), | |
886 t = 255 * v * (1 - (s * (1 - f))), | |
887 v = 255 * v; | |
888 | |
889 switch(hi) { | |
890 case 0: | |
891 return [v, t, p]; | |
892 case 1: | |
893 return [q, v, p]; | |
894 case 2: | |
895 return [p, v, t]; | |
896 case 3: | |
897 return [p, q, v]; | |
898 case 4: | |
899 return [t, p, v]; | |
900 case 5: | |
901 return [v, p, q]; | |
902 } | |
903 } | |
904 | |
905 function hsv2hsl(hsv) { | |
906 var h = hsv[0], | |
907 s = hsv[1] / 100, | |
908 v = hsv[2] / 100, | |
909 sl, l; | |
910 | |
911 l = (2 - s) * v; | |
912 sl = s * v; | |
913 sl /= (l <= 1) ? l : 2 - l; | |
914 l /= 2; | |
915 return [h, sl * 100, l * 100]; | |
916 } | |
917 | |
918 function hsv2cmyk(args) { | |
919 return rgb2cmyk(hsv2rgb(args)); | |
920 } | |
921 | |
922 function hsv2keyword(args) { | |
923 return rgb2keyword(hsv2rgb(args)); | |
924 } | |
925 | |
926 function cmyk2rgb(cmyk) { | |
927 var c = cmyk[0] / 100, | |
928 m = cmyk[1] / 100, | |
929 y = cmyk[2] / 100, | |
930 k = cmyk[3] / 100, | |
931 r, g, b; | |
932 | |
933 r = 1 - Math.min(1, c * (1 - k) + k); | |
934 g = 1 - Math.min(1, m * (1 - k) + k); | |
935 b = 1 - Math.min(1, y * (1 - k) + k); | |
936 return [r * 255, g * 255, b * 255]; | |
937 } | |
938 | |
939 function cmyk2hsl(args) { | |
940 return rgb2hsl(cmyk2rgb(args)); | |
941 } | |
942 | |
943 function cmyk2hsv(args) { | |
944 return rgb2hsv(cmyk2rgb(args)); | |
945 } | |
946 | |
947 function cmyk2keyword(args) { | |
948 return rgb2keyword(cmyk2rgb(args)); | |
949 } | |
950 | |
951 | |
952 function xyz2rgb(xyz) { | |
953 var x = xyz[0] / 100, | |
954 y = xyz[1] / 100, | |
955 z = xyz[2] / 100, | |
956 r, g, b; | |
957 | |
958 r = (x * 3.2406) + (y * -1.5372) + (z * -0.4986); | |
959 g = (x * -0.9689) + (y * 1.8758) + (z * 0.0415); | |
960 b = (x * 0.0557) + (y * -0.2040) + (z * 1.0570); | |
961 | |
962 // assume sRGB | |
963 r = r > 0.0031308 ? ((1.055 * Math.pow(r, 1.0 / 2.4)) - 0.055) | |
964 : r = (r * 12.92); | |
965 | |
966 g = g > 0.0031308 ? ((1.055 * Math.pow(g, 1.0 / 2.4)) - 0.055) | |
967 : g = (g * 12.92); | |
968 | |
969 b = b > 0.0031308 ? ((1.055 * Math.pow(b, 1.0 / 2.4)) - 0.055) | |
970 : b = (b * 12.92); | |
971 | |
972 r = (r < 0) ? 0 : r; | |
973 g = (g < 0) ? 0 : g; | |
974 b = (b < 0) ? 0 : b; | |
975 | |
976 return [r * 255, g * 255, b * 255]; | |
977 } | |
978 | |
979 function xyz2lab(xyz) { | |
980 var x = xyz[0], | |
981 y = xyz[1], | |
982 z = xyz[2], | |
983 l, a, b; | |
984 | |
985 x /= 95.047; | |
986 y /= 100; | |
987 z /= 108.883; | |
988 | |
989 x = x > 0.008856 ? Math.pow(x, 1/3) : (7.787 * x) + (16 / 116); | |
990 y = y > 0.008856 ? Math.pow(y, 1/3) : (7.787 * y) + (16 / 116); | |
991 z = z > 0.008856 ? Math.pow(z, 1/3) : (7.787 * z) + (16 / 116); | |
992 | |
993 l = (116 * y) - 16; | |
994 a = 500 * (x - y); | |
995 b = 200 * (y - z); | |
996 | |
997 return [l, a, b]; | |
998 } | |
999 | |
1000 function lab2xyz(lab) { | |
1001 var l = lab[0], | |
1002 a = lab[1], | |
1003 b = lab[2], | |
1004 x, y, z, y2; | |
1005 | |
1006 if (l <= 8) { | |
1007 y = (l * 100) / 903.3; | |
1008 y2 = (7.787 * (y / 100)) + (16 / 116); | |
1009 } else { | |
1010 y = 100 * Math.pow((l + 16) / 116, 3); | |
1011 y2 = Math.pow(y / 100, 1/3); | |
1012 } | |
1013 | |
1014 x = x / 95.047 <= 0.008856 ? x = (95.047 * ((a / 500) + y2 - (16 / 116))) / 7.787 : 95.047 * Math.pow((a / 500) + y2, 3); | |
1015 | |
1016 z = z / 108.883 <= 0.008859 ? z = (108.883 * (y2 - (b / 200) - (16 / 116))) / 7.787 : 108.883 * Math.pow(y2 - (b / 200), 3); | |
1017 | |
1018 return [x, y, z]; | |
1019 } | |
1020 | |
1021 function keyword2rgb(keyword) { | |
1022 return cssKeywords[keyword]; | |
1023 } | |
1024 | |
1025 function keyword2hsl(args) { | |
1026 return rgb2hsl(keyword2rgb(args)); | |
1027 } | |
1028 | |
1029 function keyword2hsv(args) { | |
1030 return rgb2hsv(keyword2rgb(args)); | |
1031 } | |
1032 | |
1033 function keyword2cmyk(args) { | |
1034 return rgb2cmyk(keyword2rgb(args)); | |
1035 } | |
1036 | |
1037 function keyword2lab(args) { | |
1038 return rgb2lab(keyword2rgb(args)); | |
1039 } | |
1040 | |
1041 function keyword2xyz(args) { | |
1042 return rgb2xyz(keyword2rgb(args)); | |
1043 } | |
1044 | |
1045 var cssKeywords = { | |
1046 aliceblue: [240,248,255], | |
1047 antiquewhite: [250,235,215], | |
1048 aqua: [0,255,255], | |
1049 aquamarine: [127,255,212], | |
1050 azure: [240,255,255], | |
1051 beige: [245,245,220], | |
1052 bisque: [255,228,196], | |
1053 black: [0,0,0], | |
1054 blanchedalmond: [255,235,205], | |
1055 blue: [0,0,255], | |
1056 blueviolet: [138,43,226], | |
1057 brown: [165,42,42], | |
1058 burlywood: [222,184,135], | |
1059 cadetblue: [95,158,160], | |
1060 chartreuse: [127,255,0], | |
1061 chocolate: [210,105,30], | |
1062 coral: [255,127,80], | |
1063 cornflowerblue: [100,149,237], | |
1064 cornsilk: [255,248,220], | |
1065 crimson: [220,20,60], | |
1066 cyan: [0,255,255], | |
1067 darkblue: [0,0,139], | |
1068 darkcyan: [0,139,139], | |
1069 darkgoldenrod: [184,134,11], | |
1070 darkgray: [169,169,169], | |
1071 darkgreen: [0,100,0], | |
1072 darkgrey: [169,169,169], | |
1073 darkkhaki: [189,183,107], | |
1074 darkmagenta: [139,0,139], | |
1075 darkolivegreen: [85,107,47], | |
1076 darkorange: [255,140,0], | |
1077 darkorchid: [153,50,204], | |
1078 darkred: [139,0,0], | |
1079 darksalmon: [233,150,122], | |
1080 darkseagreen: [143,188,143], | |
1081 darkslateblue: [72,61,139], | |
1082 darkslategray: [47,79,79], | |
1083 darkslategrey: [47,79,79], | |
1084 darkturquoise: [0,206,209], | |
1085 darkviolet: [148,0,211], | |
1086 deeppink: [255,20,147], | |
1087 deepskyblue: [0,191,255], | |
1088 dimgray: [105,105,105], | |
1089 dimgrey: [105,105,105], | |
1090 dodgerblue: [30,144,255], | |
1091 firebrick: [178,34,34], | |
1092 floralwhite: [255,250,240], | |
1093 forestgreen: [34,139,34], | |
1094 fuchsia: [255,0,255], | |
1095 gainsboro: [220,220,220], | |
1096 ghostwhite: [248,248,255], | |
1097 gold: [255,215,0], | |
1098 goldenrod: [218,165,32], | |
1099 gray: [128,128,128], | |
1100 green: [0,128,0], | |
1101 greenyellow: [173,255,47], | |
1102 grey: [128,128,128], | |
1103 honeydew: [240,255,240], | |
1104 hotpink: [255,105,180], | |
1105 indianred: [205,92,92], | |
1106 indigo: [75,0,130], | |
1107 ivory: [255,255,240], | |
1108 khaki: [240,230,140], | |
1109 lavender: [230,230,250], | |
1110 lavenderblush: [255,240,245], | |
1111 lawngreen: [124,252,0], | |
1112 lemonchiffon: [255,250,205], | |
1113 lightblue: [173,216,230], | |
1114 lightcoral: [240,128,128], | |
1115 lightcyan: [224,255,255], | |
1116 lightgoldenrodyellow: [250,250,210], | |
1117 lightgray: [211,211,211], | |
1118 lightgreen: [144,238,144], | |
1119 lightgrey: [211,211,211], | |
1120 lightpink: [255,182,193], | |
1121 lightsalmon: [255,160,122], | |
1122 lightseagreen: [32,178,170], | |
1123 lightskyblue: [135,206,250], | |
1124 lightslategray: [119,136,153], | |
1125 lightslategrey: [119,136,153], | |
1126 lightsteelblue: [176,196,222], | |
1127 lightyellow: [255,255,224], | |
1128 lime: [0,255,0], | |
1129 limegreen: [50,205,50], | |
1130 linen: [250,240,230], | |
1131 magenta: [255,0,255], | |
1132 maroon: [128,0,0], | |
1133 mediumaquamarine: [102,205,170], | |
1134 mediumblue: [0,0,205], | |
1135 mediumorchid: [186,85,211], | |
1136 mediumpurple: [147,112,219], | |
1137 mediumseagreen: [60,179,113], | |
1138 mediumslateblue: [123,104,238], | |
1139 mediumspringgreen: [0,250,154], | |
1140 mediumturquoise: [72,209,204], | |
1141 mediumvioletred: [199,21,133], | |
1142 midnightblue: [25,25,112], | |
1143 mintcream: [245,255,250], | |
1144 mistyrose: [255,228,225], | |
1145 moccasin: [255,228,181], | |
1146 navajowhite: [255,222,173], | |
1147 navy: [0,0,128], | |
1148 oldlace: [253,245,230], | |
1149 olive: [128,128,0], | |
1150 olivedrab: [107,142,35], | |
1151 orange: [255,165,0], | |
1152 orangered: [255,69,0], | |
1153 orchid: [218,112,214], | |
1154 palegoldenrod: [238,232,170], | |
1155 palegreen: [152,251,152], | |
1156 paleturquoise: [175,238,238], | |
1157 palevioletred: [219,112,147], | |
1158 papayawhip: [255,239,213], | |
1159 peachpuff: [255,218,185], | |
1160 peru: [205,133,63], | |
1161 pink: [255,192,203], | |
1162 plum: [221,160,221], | |
1163 powderblue: [176,224,230], | |
1164 purple: [128,0,128], | |
1165 red: [255,0,0], | |
1166 rosybrown: [188,143,143], | |
1167 royalblue: [65,105,225], | |
1168 saddlebrown: [139,69,19], | |
1169 salmon: [250,128,114], | |
1170 sandybrown: [244,164,96], | |
1171 seagreen: [46,139,87], | |
1172 seashell: [255,245,238], | |
1173 sienna: [160,82,45], | |
1174 silver: [192,192,192], | |
1175 skyblue: [135,206,235], | |
1176 slateblue: [106,90,205], | |
1177 slategray: [112,128,144], | |
1178 slategrey: [112,128,144], | |
1179 snow: [255,250,250], | |
1180 springgreen: [0,255,127], | |
1181 steelblue: [70,130,180], | |
1182 tan: [210,180,140], | |
1183 teal: [0,128,128], | |
1184 thistle: [216,191,216], | |
1185 tomato: [255,99,71], | |
1186 turquoise: [64,224,208], | |
1187 violet: [238,130,238], | |
1188 wheat: [245,222,179], | |
1189 white: [255,255,255], | |
1190 whitesmoke: [245,245,245], | |
1191 yellow: [255,255,0], | |
1192 yellowgreen: [154,205,50] | |
1193 }; | |
1194 | |
1195 var reverseKeywords = {}; | |
1196 for (var key in cssKeywords) { | |
1197 reverseKeywords[JSON.stringify(cssKeywords[key])] = key; | |
1198 } | |
1199 | |
1200 });Color = require('color.js'); | |
1201 })(); |