3 Contains the Color class.
11 Creates a new Color Object, which is an array with some color specific methods.
13 color - the hex, the RGB array or the HSB array of the color to create. For HSB colors, you need to specify the second argument.
14 type - a string representing the type of the color to create. needs to be specified if you intend to create the color with HSB values, or an array of HEX values. Can be 'rgb', 'hsb' or 'hex'.
18 var black = new Color('#000');
19 var purple = new Color([255,0,255]);
20 // mix black with white and purple, each time at 10% of the new color
21 var darkpurple = black.mix('#fff', purple, 10);
22 $('myDiv').setStyle('background-color', darkpurple);
26 var Color = new Class({
28 initialize: function(color, type){
29 type = type || (color.push ? 'rgb' : 'hex');
37 rgb = color.hsbToRgb();
41 rgb = color.hexToRgb(true);
45 rgb.hex = rgb.rgbToHex();
46 return $extend(rgb, Color.prototype);
51 Mixes two or more colors with the Color.
54 color - a color to mix. you can use as arguments how many colors as you want to mix with the original one.
55 alpha - if you use a number as the last argument, it will be threated as the amount of the color to mix.
59 var colors = $A(arguments);
60 var alpha = ($type(colors[colors.length - 1]) == 'number') ? colors.pop() : 50;
61 var rgb = this.copy();
62 colors.each(function(color){
63 color = new Color(color);
64 for (var i = 0; i < 3; i++) rgb[i] = Math.round((rgb[i] / 100 * (100 - alpha)) + (color[i] / 100 * alpha));
66 return new Color(rgb, 'rgb');
75 return new Color(this.map(function(value){
82 Modifies the hue of the Color, and returns a new one.
85 value - the hue to set
88 setHue: function(value){
89 return new Color([value, this.hsb[1], this.hsb[2]], 'hsb');
93 Property: setSaturation
94 Changes the saturation of the Color, and returns a new one.
97 percent - the percentage of the saturation to set
100 setSaturation: function(percent){
101 return new Color([this.hsb[0], percent, this.hsb[2]], 'hsb');
105 Property: setBrightness
106 Changes the brightness of the Color, and returns a new one.
109 percent - the percentage of the brightness to set
112 setBrightness: function(percent){
113 return new Color([this.hsb[0], this.hsb[1], percent], 'hsb');
118 /* Section: Utility Functions */
122 Shortcut to create a new color, based on red, green, blue values.
125 r - (integer) red value (0-255)
126 g - (integer) green value (0-255)
127 b - (integer) blue value (0-255)
131 function $RGB(r, g, b){
132 return new Color([r, g, b], 'rgb');
137 Shortcut to create a new color, based on hue, saturation, brightness values.
140 h - (integer) hue value (0-100)
141 s - (integer) saturation value (0-100)
142 b - (integer) brightness value (0-100)
145 function $HSB(h, s, b){
146 return new Color([h, s, b], 'hsb');
151 A collection of The Array Object prototype methods.
158 Converts a RGB array to an HSB array.
164 rgbToHsb: function(){
165 var red = this[0], green = this[1], blue = this[2];
166 var hue, saturation, brightness;
167 var max = Math.max(red, green, blue), min = Math.min(red, green, blue);
168 var delta = max - min;
169 brightness = max / 255;
170 saturation = (max != 0) ? delta / max : 0;
171 if (saturation == 0){
174 var rr = (max - red) / delta;
175 var gr = (max - green) / delta;
176 var br = (max - blue) / delta;
177 if (red == max) hue = br - gr;
178 else if (green == max) hue = 2 + rr - br;
179 else hue = 4 + gr - rr;
183 return [Math.round(hue * 360), Math.round(saturation * 100), Math.round(brightness * 100)];
188 Converts an HSB array to an RGB array.
194 hsbToRgb: function(){
195 var br = Math.round(this[2] / 100 * 255);
199 var hue = this[0] % 360;
201 var p = Math.round((this[2] * (100 - this[1])) / 10000 * 255);
202 var q = Math.round((this[2] * (6000 - this[1] * f)) / 600000 * 255);
203 var t = Math.round((this[2] * (6000 - this[1] * (60 - f))) / 600000 * 255);
204 switch(Math.floor(hue / 60)){
205 case 0: return [br, t, p];
206 case 1: return [q, br, p];
207 case 2: return [p, br, t];
208 case 3: return [p, q, br];
209 case 4: return [t, p, br];
210 case 5: return [br, p, q];