PageRenderTime 344ms CodeModel.GetById 181ms app.highlight 8ms RepoModel.GetById 153ms app.codeStats 0ms

/timeplot/scripts/color.js

http://showslow.googlecode.com/
JavaScript | 152 lines | 85 code | 13 blank | 54 comment | 17 complexity | c592069411b571837e6fc7073287e23c MD5 | raw file
  1/**
  2 * Color
  3 *
  4 * @fileOverview Color
  5 * @name Color
  6 */
  7
  8/*
  9 * Inspired by Plotr
 10 * Copyright 2007 (c) Bas Wenneker <sabmann[a]gmail[d]com>
 11 * For use under the BSD license. <http://www.solutoire.com/plotr>
 12 */
 13
 14/**
 15 * Create a Color object that can be used to manipulate colors programmatically.
 16 */
 17Timeplot.Color = function(color) {
 18    this._fromHex(color);
 19};
 20
 21Timeplot.Color.prototype = {
 22
 23    /**
 24     * Sets the RGB values of this coor
 25     * 
 26     * @param {Number} r,g,b    Red green and blue values (between 0 and 255)
 27     */
 28    set: function (r,g,b,a) {
 29        this.r = r;
 30        this.g = g;
 31        this.b = b;
 32        this.a = (a) ? a : 1.0;
 33        return this.check();
 34    },
 35
 36    /**
 37     * Set the color transparency
 38     * 
 39     * @param {float} a   Transparency value, between 0.0 (fully transparent) and 1.0 (fully opaque).
 40     */
 41    transparency: function(a) {
 42    	this.a = a;
 43    	return this.check();
 44    },
 45    
 46    /**
 47     * Lightens the color.
 48     * 
 49     * @param {integer} level   Level to lighten the color with.
 50     */
 51    lighten: function(level) {
 52        var color = new Timeplot.Color();
 53        return color.set(
 54            this.r += parseInt(level, 10),
 55            this.g += parseInt(level, 10),
 56            this.b += parseInt(level, 10)
 57        );
 58    },
 59
 60    /**
 61     * Darkens the color.
 62     * 
 63     * @param {integer} level   Level to darken the color with.
 64     */
 65    darken: function(level){
 66        var color = new Timeplot.Color();
 67        return color.set(
 68            this.r -= parseInt(level, 10),
 69            this.g -= parseInt(level, 10),
 70            this.b -= parseInt(level, 10)
 71        );
 72    },
 73
 74    /**
 75     * Checks and validates if the hex values r, g and b are
 76     * between 0 and 255.
 77     */
 78    check: function() {
 79        if (this.r > 255) { 
 80        	this.r = 255;
 81        } else if (this.r < 0){
 82        	this.r = 0;
 83        }
 84        if (this.g > 255) {
 85        	this.g = 255;
 86        } else if (this.g < 0) {
 87        	this.g = 0;
 88        }
 89        if (this.b > 255){
 90        	this.b = 255;
 91        } else if (this.b < 0){
 92        	this.b = 0;
 93        }
 94        if (this.a > 1.0){
 95            this.a = 1.0;
 96        } else if (this.a < 0.0){
 97            this.a = 0.0;
 98        }
 99        return this;
100    },
101
102    /**
103     * Returns a string representation of this color.
104     * 
105     * @param {float} alpha   (optional) Transparency value, between 0.0 (fully transparent) and 1.0 (fully opaque).
106     */
107    toString: function(alpha) {
108        var a = (alpha) ? alpha : ((this.a) ? this.a : 1.0);
109        return 'rgba(' + this.r + ',' + this.g + ',' + this.b + ',' + a + ')';
110    },
111
112    /**
113     * Returns the hexadecimal representation of this color (without the alpha channel as hex colors don't support it)
114     */
115    toHexString: function() {
116    	return "#" + this._toHex(this.r) + this._toHex(this.g) + this._toHex(this.b); 
117    },
118    
119    /*
120     * Parses and stores the hex values of the input color string.
121     * 
122     * @param {String} color    Hex or rgb() css string.
123     */
124    _fromHex: function(color) {
125        if(/^#?([\da-f]{3}|[\da-f]{6})$/i.test(color)){
126            color = color.replace(/^#/, '').replace(/^([\da-f])([\da-f])([\da-f])$/i, "$1$1$2$2$3$3");
127            this.r = parseInt(color.substr(0,2), 16);
128            this.g = parseInt(color.substr(2,2), 16);
129            this.b = parseInt(color.substr(4,2), 16);
130        } else if(/^rgb *\( *\d{0,3} *, *\d{0,3} *, *\d{0,3} *\)$/i.test(color)){
131            color = color.match(/^rgb *\( *(\d{0,3}) *, *(\d{0,3}) *, *(\d{0,3}) *\)$/i);
132            this.r = parseInt(color[1], 10);
133            this.g = parseInt(color[2], 10);
134            this.b = parseInt(color[3], 10);
135        }
136        this.a = 1.0;
137        return this.check();
138    },
139    
140    /*
141     * Returns an hexadecimal representation of a 8 bit integer 
142     */
143    _toHex: function(dec) {
144        var hex = "0123456789ABCDEF"
145        if (dec < 0) return "00";
146        if (dec > 255) return "FF";
147        var i = Math.floor(dec / 16);
148        var j = dec % 16;
149        return hex.charAt(i) + hex.charAt(j);
150    }
151
152};