1 <!DOCTYPE html><html><head><title>Sencha Documentation Project</title><link rel="stylesheet" href="../reset.css" type="text/css"><link rel="stylesheet" href="../prettify.css" type="text/css"><link rel="stylesheet" href="../prettify_sa.css" type="text/css"><script type="text/javascript" src="../prettify.js"></script></head><body onload="prettyPrint()"><pre class="prettyprint"><pre><span id='Ext-draw.Color'>/**
2 </span> * @class Ext.draw.Color
5 * Represents an RGB color and provides helper functions get
6 * color components in HSL color space.
8 Ext.define('Ext.draw.Color', {
10 /* Begin Definitions */
14 colorToHexRe: /(.*?)rgb\((\d+),\s*(\d+),\s*(\d+)\)/,
15 rgbRe: /\s*rgb\s*\(\s*([0-9]+)\s*,\s*([0-9]+)\s*,\s*([0-9]+)\s*\)\s*/,
16 hexRe: /\s*#([0-9a-fA-F][0-9a-fA-F]?)([0-9a-fA-F][0-9a-fA-F]?)([0-9a-fA-F][0-9a-fA-F]?)\s*/,
18 <span id='Ext-draw.Color-cfg-lightnessFactor'> /**
19 </span> * @cfg {Number} lightnessFactor
21 * The default factor to compute the lighter or darker color. Defaults to 0.2.
25 <span id='Ext-draw.Color-method-constructor'> /**
26 </span> * @constructor
27 * @param {Number} red Red component (0..255)
28 * @param {Number} green Green component (0..255)
29 * @param {Number} blue Blue component (0..255)
31 constructor : function(red, green, blue) {
33 clamp = Ext.Number.constrain;
34 me.r = clamp(red, 0, 255);
35 me.g = clamp(green, 0, 255);
36 me.b = clamp(blue, 0, 255);
39 <span id='Ext-draw.Color-method-getRed'> /**
40 </span> * Get the red component of the color, in the range 0..255.
47 <span id='Ext-draw.Color-method-getGreen'> /**
48 </span> * Get the green component of the color, in the range 0..255.
51 getGreen: function() {
55 <span id='Ext-draw.Color-method-getBlue'> /**
56 </span> * Get the blue component of the color, in the range 0..255.
63 <span id='Ext-draw.Color-method-getRGB'> /**
64 </span> * Get the RGB values.
69 return [me.r, me.g, me.b];
72 <span id='Ext-draw.Color-method-getHSL'> /**
73 </span> * Get the equivalent HSL components of the color.
81 max = Math.max(r, g, b),
82 min = Math.min(r, g, b),
86 l = 0.5 * (max + min);
88 // min==max means achromatic (hue is undefined)
90 s = (l < 0.5) ? delta / (max + min) : delta / (2 - max - min);
92 h = 60 * (g - b) / delta;
93 } else if (g == max) {
94 h = 120 + 60 * (b - r) / delta;
96 h = 240 + 60 * (r - g) / delta;
108 <span id='Ext-draw.Color-method-getLighter'> /**
109 </span> * Return a new color that is lighter than this color.
110 * @param {Number} factor Lighter factor (0..1), default to 0.2
111 * @return Ext.draw.Color
113 getLighter: function(factor) {
114 var hsl = this.getHSL();
115 factor = factor || this.lightnessFactor;
116 hsl[2] = Ext.Number.constrain(hsl[2] + factor, 0, 1);
117 return this.fromHSL(hsl[0], hsl[1], hsl[2]);
120 <span id='Ext-draw.Color-method-getDarker'> /**
121 </span> * Return a new color that is darker than this color.
122 * @param {Number} factor Darker factor (0..1), default to 0.2
123 * @return Ext.draw.Color
125 getDarker: function(factor) {
126 factor = factor || this.lightnessFactor;
127 return this.getLighter(-factor);
130 <span id='Ext-draw.Color-method-toString'> /**
131 </span> * Return the color in the hex format, i.e. '#rrggbb'.
134 toString: function() {
137 r = round(me.r).toString(16),
138 g = round(me.g).toString(16),
139 b = round(me.b).toString(16);
140 r = (r.length == 1) ? '0' + r : r;
141 g = (g.length == 1) ? '0' + g : g;
142 b = (b.length == 1) ? '0' + b : b;
143 return ['#', r, g, b].join('');
146 <span id='Ext-draw.Color-method-toHex'> /**
147 </span> * Convert a color to hexadecimal format.
149 * @param {String|Array} color The color value (i.e 'rgb(255, 255, 255)', 'color: #ffffff').
150 * Can also be an Array, in this case the function handles the first member.
151 * @returns {String} The color in hexadecimal format.
153 toHex: function(color) {
154 if (Ext.isArray(color)) {
157 if (!Ext.isString(color)) {
160 if (color.substr(0, 1) === '#') {
163 var digits = this.colorToHexRe.exec(color);
165 if (Ext.isArray(digits)) {
166 var red = parseInt(digits[2], 10),
167 green = parseInt(digits[3], 10),
168 blue = parseInt(digits[4], 10),
169 rgb = blue | (green << 8) | (red << 16);
170 return digits[1] + '#' + ("000000" + rgb.toString(16)).slice(-6);
177 <span id='Ext-draw.Color-method-fromString'> /**
178 </span> * Parse the string and create a new color.
180 * Supported formats: '#rrggbb', '#rgb', and 'rgb(r,g,b)'.
182 * If the string is not recognized, an undefined will be returned instead.
184 * @param {String} str Color in string.
185 * @returns Ext.draw.Color
187 fromString: function(str) {
191 if ((str.length == 4 || str.length == 7) && str.substr(0, 1) === '#') {
192 values = str.match(this.hexRe);
194 r = parse(values[1], 16) >> 0;
195 g = parse(values[2], 16) >> 0;
196 b = parse(values[3], 16) >> 0;
197 if (str.length == 4) {
205 values = str.match(this.rgbRe);
213 return (typeof r == 'undefined') ? undefined : Ext.create('Ext.draw.Color', r, g, b);
216 <span id='Ext-draw.Color-method-getGrayscale'> /**
217 </span> * Returns the gray value (0 to 255) of the color.
219 * The gray value is calculated using the formula r*0.3 + g*0.59 + b*0.11.
223 getGrayscale: function() {
224 // http://en.wikipedia.org/wiki/Grayscale#Converting_color_to_grayscale
225 return this.r * 0.3 + this.g * 0.59 + this.b * 0.11;
228 <span id='Ext-draw.Color-method-fromHSL'> /**
229 </span> * Create a new color based on the specified HSL values.
231 * @param {Number} h Hue component (0..359)
232 * @param {Number} s Saturation component (0..1)
233 * @param {Number} l Lightness component (0..1)
234 * @returns Ext.draw.Color
236 fromHSL: function(h, s, l) {
237 var C, X, m, i, rgb = [],
241 if (s == 0 || h == null) {
246 // http://en.wikipedia.org/wiki/HSL_and_HSV#From_HSL
248 // X is the second largest component
249 // m is the lightness adjustment
251 C = s * (1 - abs(2 * l - 1));
252 X = C * (1 - abs(h - 2 * floor(h / 2) - 1));
274 rgb = [rgb[0] + m, rgb[1] + m, rgb[2] + m];
276 return Ext.create('Ext.draw.Color', rgb[0] * 255, rgb[1] * 255, rgb[2] * 255);
279 var prototype = this.prototype;
281 //These functions are both static and instance. TODO: find a more elegant way of copying them
283 fromHSL: function() {
284 return prototype.fromHSL.apply(prototype, arguments);
286 fromString: function() {
287 return prototype.fromString.apply(prototype, arguments);
290 return prototype.toHex.apply(prototype, arguments);
294 </pre></pre></body></html>