├── README.md ├── license.txt └── Source └── Color.js /README.md: -------------------------------------------------------------------------------- 1 | MooTools Color 2 | ============== 3 | 4 | Color.js ported from 2.o to stand-alone js. Requires a browser with Array.prototype.map support. -------------------------------------------------------------------------------- /license.txt: -------------------------------------------------------------------------------- 1 | The MIT License 2 | 3 | Copyright (c) Valerio Proietti, 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. -------------------------------------------------------------------------------- /Source/Color.js: -------------------------------------------------------------------------------- 1 | /* 2 | --- 3 | name: Color 4 | description: Class to create and manipulate colors. Includes HSB «-» RGB «-» HEX conversions. 5 | provides: Color 6 | ... 7 | */ 8 | 9 | (function(exports){ 10 | 11 | var Color = exports.Color = function(color, type){ 12 | 13 | if (color.isColor){ 14 | 15 | this.red = color.red; 16 | this.green = color.green; 17 | this.blue = color.blue; 18 | this.alpha = color.alpha; 19 | 20 | } else { 21 | 22 | switch (typeof color){ 23 | case 'string': if (!type) type = (type = color.match(/^rgb|^hsb/)) ? type[0] : 'hex'; break; 24 | case 'object': type = type || 'rgb'; color = color.toString(); break; 25 | case 'number': type = 'hex'; color = color.toString(16); break; 26 | } 27 | 28 | color = Color['parse' + type.toUpperCase()](color); 29 | this.red = color[0]; 30 | this.green = color[1]; 31 | this.blue = color[2]; 32 | this.alpha = color[3]; 33 | } 34 | 35 | this.isColor = true; 36 | 37 | }; 38 | 39 | var limit = function(number, min, max){ 40 | return Math.min(max, Math.max(min, number)); 41 | }; 42 | 43 | var listMatch = /([-.\d]+)\s*,\s*([-.\d]+)\s*,\s*([-.\d]+)\s*,?\s*([-.\d]*)/; 44 | var hexMatch = /^#?([a-f0-9]{1,2})([a-f0-9]{1,2})([a-f0-9]{1,2})([a-f0-9]{0,2})$/i; 45 | 46 | exports.Color.parseRGB = function(color){ 47 | return color.match(listMatch).slice(1).map(function(bit, i){ 48 | return (i < 3) ? Math.round(((bit %= 256) < 0) ? bit + 256 : bit) : limit(((bit === '') ? 1 : Number(bit)), 0, 1); 49 | }); 50 | }; 51 | 52 | exports.Color.parseHEX = function(color){ 53 | if (color.length == 1) color = color + color + color; 54 | return color.match(hexMatch).slice(1).map(function(bit, i){ 55 | if (i == 3) return (bit) ? parseInt(bit, 16) / 255 : 1; 56 | return parseInt((bit.length == 1) ? bit + bit : bit, 16); 57 | }); 58 | }; 59 | 60 | exports.Color.parseHSB = function(color){ 61 | var hsb = color.match(listMatch).slice(1).map(function(bit, i){ 62 | if (i === 0) return Math.round(((bit %= 360) < 0) ? (bit + 360) : bit); 63 | else if (i < 3) return limit(Math.round(bit), 0, 100); 64 | else return limit(((bit === '') ? 1 : Number(bit)), 0, 1); 65 | }); 66 | 67 | var a = hsb[3]; 68 | var br = Math.round(hsb[2] / 100 * 255); 69 | if (hsb[1] == 0) return [br, br, br, a]; 70 | 71 | var hue = hsb[0]; 72 | var f = hue % 60; 73 | var p = Math.round((hsb[2] * (100 - hsb[1])) / 10000 * 255); 74 | var q = Math.round((hsb[2] * (6000 - hsb[1] * f)) / 600000 * 255); 75 | var t = Math.round((hsb[2] * (6000 - hsb[1] * (60 - f))) / 600000 * 255); 76 | 77 | switch (Math.floor(hue / 60)){ 78 | case 0: return [br, t, p, a]; 79 | case 1: return [q, br, p, a]; 80 | case 2: return [p, br, t, a]; 81 | case 3: return [p, q, br, a]; 82 | case 4: return [t, p, br, a]; 83 | default: return [br, p, q, a]; 84 | } 85 | }; 86 | 87 | var toString = function(type, array){ 88 | if (array[3] != 1) type += 'a'; 89 | else array.pop(); 90 | return type + '(' + array.join(', ') + ')'; 91 | }; 92 | 93 | exports.Color.prototype = { 94 | 95 | toHSB: function(array){ 96 | var red = this.red, green = this.green, blue = this.blue, alpha = this.alpha; 97 | 98 | var max = Math.max(red, green, blue), min = Math.min(red, green, blue), delta = max - min; 99 | var hue = 0, saturation = (max != 0) ? delta / max : 0, brightness = max / 255; 100 | if (saturation){ 101 | var rr = (max - red) / delta, gr = (max - green) / delta, br = (max - blue) / delta; 102 | hue = (red == max) ? br - gr : (green == max) ? 2 + rr - br : 4 + gr - rr; 103 | if ((hue /= 6) < 0) hue++; 104 | } 105 | 106 | var hsb = [Math.round(hue * 360), Math.round(saturation * 100), Math.round(brightness * 100), alpha]; 107 | 108 | return (array) ? hsb : toString('hsb', hsb); 109 | }, 110 | 111 | toHEX: function(array){ 112 | 113 | var a = this.alpha; 114 | var alpha = ((a = Math.round((a * 255)).toString(16)).length == 1) ? a + a : a; 115 | 116 | var hex = [this.red, this.green, this.blue].map(function(bit){ 117 | bit = bit.toString(16); 118 | return (bit.length == 1) ? '0' + bit : bit; 119 | }); 120 | 121 | return (array) ? hex.concat(alpha) : '#' + hex.join('') + ((alpha == 'ff') ? '' : alpha); 122 | }, 123 | 124 | toRGB: function(array){ 125 | var rgb = [this.red, this.green, this.blue, this.alpha]; 126 | return (array) ? rgb : toString('rgb', rgb); 127 | } 128 | 129 | }; 130 | 131 | exports.Color.prototype.toString = Color.prototype.toRGB; 132 | 133 | exports.Color.hex = function(hex){ 134 | return new Color(hex, 'hex'); 135 | }; 136 | 137 | if (exports.hex == null) exports.hex = Color.hex; 138 | 139 | exports.Color.hsb = function(h, s, b, a){ 140 | return new Color([h || 0, s || 0, b || 0, (a == null) ? 1 : a], 'hsb'); 141 | }; 142 | 143 | if (exports.hsb == null) exports.hsb = Color.hsb; 144 | 145 | exports.Color.rgb = function(r, g, b, a){ 146 | return new Color([r || 0, g || 0, b || 0, (a == null) ? 1 : a], 'rgb'); 147 | }; 148 | 149 | if (exports.rgb == null) exports.rgb = Color.rgb; 150 | 151 | })(typeof exports < 'u' ? exports : this); 152 | --------------------------------------------------------------------------------