[ Index ] |
|
Code source de PRADO 3.0.6 |
1 //-------------------- ricoColor.js 2 if(typeof(Rico) == "undefined") Rico = {}; 3 4 Rico.Color = Class.create(); 5 6 Rico.Color.prototype = { 7 8 initialize: function(red, green, blue) { 9 this.rgb = { r: red, g : green, b : blue }; 10 }, 11 12 setRed: function(r) { 13 this.rgb.r = r; 14 }, 15 16 setGreen: function(g) { 17 this.rgb.g = g; 18 }, 19 20 setBlue: function(b) { 21 this.rgb.b = b; 22 }, 23 24 setHue: function(h) { 25 26 // get an HSB model, and set the new hue... 27 var hsb = this.asHSB(); 28 hsb.h = h; 29 30 // convert back to RGB... 31 this.rgb = Rico.Color.HSBtoRGB(hsb.h, hsb.s, hsb.b); 32 }, 33 34 setSaturation: function(s) { 35 // get an HSB model, and set the new hue... 36 var hsb = this.asHSB(); 37 hsb.s = s; 38 39 // convert back to RGB and set values... 40 this.rgb = Rico.Color.HSBtoRGB(hsb.h, hsb.s, hsb.b); 41 }, 42 43 setBrightness: function(b) { 44 // get an HSB model, and set the new hue... 45 var hsb = this.asHSB(); 46 hsb.b = b; 47 48 // convert back to RGB and set values... 49 this.rgb = Rico.Color.HSBtoRGB( hsb.h, hsb.s, hsb.b ); 50 }, 51 52 darken: function(percent) { 53 var hsb = this.asHSB(); 54 this.rgb = Rico.Color.HSBtoRGB(hsb.h, hsb.s, Math.max(hsb.b - percent,0)); 55 }, 56 57 brighten: function(percent) { 58 var hsb = this.asHSB(); 59 this.rgb = Rico.Color.HSBtoRGB(hsb.h, hsb.s, Math.min(hsb.b + percent,1)); 60 }, 61 62 blend: function(other) { 63 this.rgb.r = Math.floor((this.rgb.r + other.rgb.r)/2); 64 this.rgb.g = Math.floor((this.rgb.g + other.rgb.g)/2); 65 this.rgb.b = Math.floor((this.rgb.b + other.rgb.b)/2); 66 }, 67 68 isBright: function() { 69 var hsb = this.asHSB(); 70 return this.asHSB().b > 0.5; 71 }, 72 73 isDark: function() { 74 return ! this.isBright(); 75 }, 76 77 asRGB: function() { 78 return "rgb(" + this.rgb.r + "," + this.rgb.g + "," + this.rgb.b + ")"; 79 }, 80 81 asHex: function() { 82 return "#" + this.rgb.r.toColorPart() + this.rgb.g.toColorPart() + this.rgb.b.toColorPart(); 83 }, 84 85 asHSB: function() { 86 return Rico.Color.RGBtoHSB(this.rgb.r, this.rgb.g, this.rgb.b); 87 }, 88 89 toString: function() { 90 return this.asHex(); 91 } 92 93 }; 94 95 Rico.Color.createFromHex = function(hexCode) { 96 97 if ( hexCode.indexOf('#') == 0 ) 98 hexCode = hexCode.substring(1); 99 100 var red = "ff", green = "ff", blue="ff"; 101 if(hexCode.length > 4) 102 { 103 red = hexCode.substring(0,2); 104 green = hexCode.substring(2,4); 105 blue = hexCode.substring(4,6); 106 } 107 else if(hexCode.length > 0 & hexCode.length < 4) 108 { 109 var r = hexCode.substring(0,1); 110 var g = hexCode.substring(1,2); 111 var b = hexCode.substring(2); 112 red = r+r; 113 green = g+g; 114 blue = b+b; 115 } 116 return new Rico.Color( parseInt(red,16), parseInt(green,16), parseInt(blue,16) ); 117 } 118 119 /** 120 * Factory method for creating a color from the background of 121 * an HTML element. 122 */ 123 Rico.Color.createColorFromBackground = function(elem) { 124 125 var actualColor = Element.getStyle($(elem), "background-color"); 126 if ( actualColor == "transparent" && elem.parent ) 127 return Rico.Color.createColorFromBackground(elem.parent); 128 129 if ( actualColor == null ) 130 return new Rico.Color(255,255,255); 131 132 if ( actualColor.indexOf("rgb(") == 0 ) { 133 var colors = actualColor.substring(4, actualColor.length - 1 ); 134 var colorArray = colors.split(","); 135 return new Rico.Color( parseInt( colorArray[0] ), 136 parseInt( colorArray[1] ), 137 parseInt( colorArray[2] ) ); 138 139 } 140 else if ( actualColor.indexOf("#") == 0 ) { 141 return Rico.Color.createFromHex(actualColor); 142 } 143 else 144 return new Rico.Color(255,255,255); 145 } 146 147 Rico.Color.HSBtoRGB = function(hue, saturation, brightness) { 148 149 var red = 0; 150 var green = 0; 151 var blue = 0; 152 153 if (saturation == 0) { 154 red = parseInt(brightness * 255.0 + 0.5); 155 green = red; 156 blue = red; 157 } 158 else { 159 var h = (hue - Math.floor(hue)) * 6.0; 160 var f = h - Math.floor(h); 161 var p = brightness * (1.0 - saturation); 162 var q = brightness * (1.0 - saturation * f); 163 var t = brightness * (1.0 - (saturation * (1.0 - f))); 164 165 switch (parseInt(h)) { 166 case 0: 167 red = (brightness * 255.0 + 0.5); 168 green = (t * 255.0 + 0.5); 169 blue = (p * 255.0 + 0.5); 170 break; 171 case 1: 172 red = (q * 255.0 + 0.5); 173 green = (brightness * 255.0 + 0.5); 174 blue = (p * 255.0 + 0.5); 175 break; 176 case 2: 177 red = (p * 255.0 + 0.5); 178 green = (brightness * 255.0 + 0.5); 179 blue = (t * 255.0 + 0.5); 180 break; 181 case 3: 182 red = (p * 255.0 + 0.5); 183 green = (q * 255.0 + 0.5); 184 blue = (brightness * 255.0 + 0.5); 185 break; 186 case 4: 187 red = (t * 255.0 + 0.5); 188 green = (p * 255.0 + 0.5); 189 blue = (brightness * 255.0 + 0.5); 190 break; 191 case 5: 192 red = (brightness * 255.0 + 0.5); 193 green = (p * 255.0 + 0.5); 194 blue = (q * 255.0 + 0.5); 195 break; 196 } 197 } 198 199 return { r : parseInt(red), g : parseInt(green) , b : parseInt(blue) }; 200 } 201 202 Rico.Color.RGBtoHSB = function(r, g, b) { 203 204 var hue; 205 var saturaton; 206 var brightness; 207 208 var cmax = (r > g) ? r : g; 209 if (b > cmax) 210 cmax = b; 211 212 var cmin = (r < g) ? r : g; 213 if (b < cmin) 214 cmin = b; 215 216 brightness = cmax / 255.0; 217 if (cmax != 0) 218 saturation = (cmax - cmin)/cmax; 219 else 220 saturation = 0; 221 222 if (saturation == 0) 223 hue = 0; 224 else { 225 var redc = (cmax - r)/(cmax - cmin); 226 var greenc = (cmax - g)/(cmax - cmin); 227 var bluec = (cmax - b)/(cmax - cmin); 228 229 if (r == cmax) 230 hue = bluec - greenc; 231 else if (g == cmax) 232 hue = 2.0 + redc - bluec; 233 else 234 hue = 4.0 + greenc - redc; 235 236 hue = hue / 6.0; 237 if (hue < 0) 238 hue = hue + 1.0; 239 } 240 241 return { h : hue, s : saturation, b : brightness }; 242 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Feb 25 21:07:04 2007 | par Balluche grâce à PHPXref 0.7 |