1 goog.provide('lime.fill.Color');
  2 
  3 goog.require('goog.color');
  4 
  5 goog.require('goog.color.alpha');
  6 goog.require('lime.fill.Fill');
  7 
  8 /**
  9 * Color fill
 10 * @param {*} clr Color value.
 11 * @constructor
 12 * @extends lime.fill.Fill
 13 */
 14 lime.fill.Color = function(clr) {
 15     lime.fill.Fill.call(this);
 16 
 17     this.a = 1;
 18     
 19     this.setColor(clr);
 20 
 21 };
 22 goog.inherits(lime.fill.Color, lime.fill.Fill);
 23 
 24 /**
 25  * Common name for color objects
 26  * @type {string}
 27  */
 28 lime.fill.Color.prototype.id = 'color';
 29 
 30 /**
 31 * Gets color as RGBA array.
 32 * @return {null|Array.<number>} RGBA array.
 33 */
 34 lime.fill.Color.prototype.getRgba = function() {
 35     var out = null;
 36 
 37     if (goog.isNumber(this.r) && goog.isNumber(this.g) &&
 38         goog.isNumber(this.b)) {
 39         out = [this.r, this.g, this.b, this.a];
 40 
 41     } else if (goog.isString(this.str)) {
 42         var color = goog.color.parse(this.str);
 43         if (color.type != 'named') {
 44             out = goog.color.hexToRgb(color.hex);
 45         }
 46         out.push(1);
 47     }
 48 
 49     return out;
 50 };
 51 
 52 /**
 53  * Make color lighter
 54  * @param {number} value Brightness factor.
 55  * @return {lime.fill.Color} object itself.
 56  */
 57 lime.fill.Color.prototype.addBrightness = function(value) {
 58     return this.modifyColor(2, value);
 59 };
 60 
 61 /**
 62  * Modify color value
 63  * @param {number} mode Settings to change.
 64  * @param {number} value Amount factor.
 65  * @return {lime.fill.Color} object itself.
 66  */
 67 lime.fill.Color.prototype.modifyColor = function(mode, value) {
 68     var add = value || .1;
 69 
 70     var rgb = this.getRgba();
 71     if (!rgb) return this;
 72 
 73     rgb.pop();
 74 
 75     var hsl = goog.color.rgbArrayToHsl(rgb);
 76     hsl[mode] += add;
 77     if (hsl[mode] > 1) hsl[mode] = 1;
 78 
 79     rgb = goog.color.hslArrayToRgb(hsl);
 80     rgb.push(this.a);
 81     return this.setColor(rgb);
 82 };
 83 
 84 /**
 85  * Make color more saturated
 86  * @param {number} value Saturation factor.
 87  * @return {lime.fill.Color} ibject itself.
 88  */
 89 lime.fill.Color.prototype.addSaturation = function(value) {
 90     return this.modifyColor(1, value);
 91 };
 92 
 93 /**
 94 * Set color value of the object. Accepts raw RGB(A) values and strings.
 95 * @param {*} clr New color value.
 96 * @return {lime.fill.Color} object itself.
 97 */
 98 lime.fill.Color.prototype.setColor = function(clr) {
 99     var color = clr;
100 
101     if (goog.isString(clr)) {
102         this.str = clr;
103         return this;
104     }
105 
106     if (arguments.length > 2) {
107         color = arguments;
108     }
109     if (color.length >= 3) {
110         this.r = color[0];
111         this.g = color[1];
112         this.b = color[2];
113     }
114     if (color.length == 4) {
115         this.a = color[3];
116     }
117 
118     this.str = this.a == 1 ?
119     'rgb(' + this.r + ',' + this.g + ',' + this.b + ')' :
120     'rgba(' + this.r + ',' + this.g + ',' + this.b + ',' + this.a + ')';
121     return this;
122 };
123 
124 /** @inheritDoc */
125 lime.fill.Color.prototype.setDOMStyle = function(domEl) {
126     domEl.style['background'] = this.str;
127 };
128 
129 /** @inheritDoc */
130 lime.fill.Color.prototype.setCanvasStyle = function(context) {
131     context.fillStyle = this.str;
132 };
133 
134 /**
135  * Clone the color
136  * @return {lime.fill.Color} New cloned color.
137  */
138 lime.fill.Color.prototype.clone = function() {
139     var c = new lime.fill.Color('');
140     c.r = this.r;
141     c.g = this.g;
142     c.b = this.b;
143     c.a = this.a;
144     c.str = this.str;
145     return c;
146 };
147