1 Rico.Color = Class.create();
 
   3 Rico.Color.prototype = {
 
   5    initialize: function(red, green, blue) {
 
   6       this.rgb = { r: red, g : green, b : blue };
 
  13    setGreen: function(g) {
 
  17    setBlue: function(b) {
 
  23       // get an HSB model, and set the new hue...
 
  24       var hsb = this.asHSB();
 
  27       // convert back to RGB...
 
  28       this.rgb = Rico.Color.HSBtoRGB(hsb.h, hsb.s, hsb.b);
 
  31    setSaturation: function(s) {
 
  32       // get an HSB model, and set the new hue...
 
  33       var hsb = this.asHSB();
 
  36       // convert back to RGB and set values...
 
  37       this.rgb = Rico.Color.HSBtoRGB(hsb.h, hsb.s, hsb.b);
 
  40    setBrightness: function(b) {
 
  41       // get an HSB model, and set the new hue...
 
  42       var hsb = this.asHSB();
 
  45       // convert back to RGB and set values...
 
  46       this.rgb = Rico.Color.HSBtoRGB( hsb.h, hsb.s, hsb.b );
 
  49    darken: function(percent) {
 
  50       var hsb  = this.asHSB();
 
  51       this.rgb = Rico.Color.HSBtoRGB(hsb.h, hsb.s, Math.max(hsb.b - percent,0));
 
  54    brighten: function(percent) {
 
  55       var hsb  = this.asHSB();
 
  56       this.rgb = Rico.Color.HSBtoRGB(hsb.h, hsb.s, Math.min(hsb.b + percent,1));
 
  59    blend: function(other) {
 
  60       this.rgb.r = Math.floor((this.rgb.r + other.rgb.r)/2);
 
  61       this.rgb.g = Math.floor((this.rgb.g + other.rgb.g)/2);
 
  62       this.rgb.b = Math.floor((this.rgb.b + other.rgb.b)/2);
 
  65    isBright: function() {
 
  66       var hsb = this.asHSB();
 
  67       return this.asHSB().b > 0.5;
 
  71       return ! this.isBright();
 
  75       return "rgb(" + this.rgb.r + "," + this.rgb.g + "," + this.rgb.b + ")";
 
  79       return "#" + this.rgb.r.toColorPart() + this.rgb.g.toColorPart() + this.rgb.b.toColorPart();
 
  83       return Rico.Color.RGBtoHSB(this.rgb.r, this.rgb.g, this.rgb.b);
 
  86    toString: function() {
 
  92 Rico.Color.createFromHex = function(hexCode) {
 
  93   if(hexCode.length==4) {
 
  94     var shortHexCode = hexCode; 
 
  96     for(var i=1;i<4;i++) hexCode += (shortHexCode.charAt(i) + 
 
  97 shortHexCode.charAt(i));
 
  99    if ( hexCode.indexOf('#') == 0 )
 
 100       hexCode = hexCode.substring(1);
 
 101    var red   = hexCode.substring(0,2);
 
 102    var green = hexCode.substring(2,4);
 
 103    var blue  = hexCode.substring(4,6);
 
 104    return new Rico.Color( parseInt(red,16), parseInt(green,16), parseInt(blue,16) );
 
 108  * Factory method for creating a color from the background of
 
 111 Rico.Color.createColorFromBackground = function(elem) {
 
 113    var actualColor = RicoUtil.getElementsComputedStyle($(elem), "backgroundColor", "background-color");
 
 115    if ( actualColor == "transparent" && elem.parentNode )
 
 116       return Rico.Color.createColorFromBackground(elem.parentNode);
 
 118    if ( actualColor == null )
 
 119       return new Rico.Color(255,255,255);
 
 121    if ( actualColor.indexOf("rgb(") == 0 ) {
 
 122       var colors = actualColor.substring(4, actualColor.length - 1 );
 
 123       var colorArray = colors.split(",");
 
 124       return new Rico.Color( parseInt( colorArray[0] ),
 
 125                             parseInt( colorArray[1] ),
 
 126                             parseInt( colorArray[2] )  );
 
 129    else if ( actualColor.indexOf("#") == 0 ) {
 
 130       return Rico.Color.createFromHex(actualColor);
 
 133       return new Rico.Color(255,255,255);
 
 136 Rico.Color.HSBtoRGB = function(hue, saturation, brightness) {
 
 142    if (saturation == 0) {
 
 143       red = parseInt(brightness * 255.0 + 0.5);
 
 148       var h = (hue - Math.floor(hue)) * 6.0;
 
 149       var f = h - Math.floor(h);
 
 150       var p = brightness * (1.0 - saturation);
 
 151       var q = brightness * (1.0 - saturation * f);
 
 152       var t = brightness * (1.0 - (saturation * (1.0 - f)));
 
 154       switch (parseInt(h)) {
 
 156             red   = (brightness * 255.0 + 0.5);
 
 157             green = (t * 255.0 + 0.5);
 
 158             blue  = (p * 255.0 + 0.5);
 
 161             red   = (q * 255.0 + 0.5);
 
 162             green = (brightness * 255.0 + 0.5);
 
 163             blue  = (p * 255.0 + 0.5);
 
 166             red   = (p * 255.0 + 0.5);
 
 167             green = (brightness * 255.0 + 0.5);
 
 168             blue  = (t * 255.0 + 0.5);
 
 171             red   = (p * 255.0 + 0.5);
 
 172             green = (q * 255.0 + 0.5);
 
 173             blue  = (brightness * 255.0 + 0.5);
 
 176             red   = (t * 255.0 + 0.5);
 
 177             green = (p * 255.0 + 0.5);
 
 178             blue  = (brightness * 255.0 + 0.5);
 
 181             red   = (brightness * 255.0 + 0.5);
 
 182             green = (p * 255.0 + 0.5);
 
 183             blue  = (q * 255.0 + 0.5);
 
 188    return { r : parseInt(red), g : parseInt(green) , b : parseInt(blue) };
 
 191 Rico.Color.RGBtoHSB = function(r, g, b) {
 
 197    var cmax = (r > g) ? r : g;
 
 201    var cmin = (r < g) ? r : g;
 
 205    brightness = cmax / 255.0;
 
 207       saturation = (cmax - cmin)/cmax;
 
 214       var redc   = (cmax - r)/(cmax - cmin);
 
 215         var greenc = (cmax - g)/(cmax - cmin);
 
 216         var bluec  = (cmax - b)/(cmax - cmin);
 
 219            hue = bluec - greenc;
 
 221            hue = 2.0 + redc - bluec;
 
 223            hue = 4.0 + greenc - redc;
 
 230    return { h : hue, s : saturation, b : brightness };