4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../prettify/prettify.js"></script>
8 <style type="text/css">
9 .highlight { display: block; background-color: #ddd; }
11 <script type="text/javascript">
12 function highlight() {
13 document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
17 <body onload="prettyPrint(); highlight();">
18 <pre class="prettyprint lang-js"><span id='Ext-draw-Color'>/**
19 </span> * @class Ext.draw.Color
22 * Represents an RGB color and provides helper functions get
23 * color components in HSL color space.
25 Ext.define('Ext.draw.Color', {
27 /* Begin Definitions */
31 colorToHexRe: /(.*?)rgb\((\d+),\s*(\d+),\s*(\d+)\)/,
32 rgbRe: /\s*rgb\s*\(\s*([0-9]+)\s*,\s*([0-9]+)\s*,\s*([0-9]+)\s*\)\s*/,
33 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*/,
35 <span id='Ext-draw-Color-cfg-lightnessFactor'> /**
36 </span> * @cfg {Number} lightnessFactor
38 * The default factor to compute the lighter or darker color. Defaults to 0.2.
42 <span id='Ext-draw-Color-method-constructor'> /**
43 </span> * @constructor
44 * @param {Number} red Red component (0..255)
45 * @param {Number} green Green component (0..255)
46 * @param {Number} blue Blue component (0..255)
48 constructor : function(red, green, blue) {
50 clamp = Ext.Number.constrain;
51 me.r = clamp(red, 0, 255);
52 me.g = clamp(green, 0, 255);
53 me.b = clamp(blue, 0, 255);
56 <span id='Ext-draw-Color-method-getRed'> /**
57 </span> * Get the red component of the color, in the range 0..255.
64 <span id='Ext-draw-Color-method-getGreen'> /**
65 </span> * Get the green component of the color, in the range 0..255.
68 getGreen: function() {
72 <span id='Ext-draw-Color-method-getBlue'> /**
73 </span> * Get the blue component of the color, in the range 0..255.
80 <span id='Ext-draw-Color-method-getRGB'> /**
81 </span> * Get the RGB values.
86 return [me.r, me.g, me.b];
89 <span id='Ext-draw-Color-method-getHSL'> /**
90 </span> * Get the equivalent HSL components of the color.
98 max = Math.max(r, g, b),
99 min = Math.min(r, g, b),
103 l = 0.5 * (max + min);
105 // min==max means achromatic (hue is undefined)
107 s = (l < 0.5) ? delta / (max + min) : delta / (2 - max - min);
109 h = 60 * (g - b) / delta;
110 } else if (g == max) {
111 h = 120 + 60 * (b - r) / delta;
113 h = 240 + 60 * (r - g) / delta;
125 <span id='Ext-draw-Color-method-getLighter'> /**
126 </span> * Return a new color that is lighter than this color.
127 * @param {Number} factor Lighter factor (0..1), default to 0.2
128 * @return Ext.draw.Color
130 getLighter: function(factor) {
131 var hsl = this.getHSL();
132 factor = factor || this.lightnessFactor;
133 hsl[2] = Ext.Number.constrain(hsl[2] + factor, 0, 1);
134 return this.fromHSL(hsl[0], hsl[1], hsl[2]);
137 <span id='Ext-draw-Color-method-getDarker'> /**
138 </span> * Return a new color that is darker than this color.
139 * @param {Number} factor Darker factor (0..1), default to 0.2
140 * @return Ext.draw.Color
142 getDarker: function(factor) {
143 factor = factor || this.lightnessFactor;
144 return this.getLighter(-factor);
147 <span id='Ext-draw-Color-method-toString'> /**
148 </span> * Return the color in the hex format, i.e. '#rrggbb'.
151 toString: function() {
154 r = round(me.r).toString(16),
155 g = round(me.g).toString(16),
156 b = round(me.b).toString(16);
157 r = (r.length == 1) ? '0' + r : r;
158 g = (g.length == 1) ? '0' + g : g;
159 b = (b.length == 1) ? '0' + b : b;
160 return ['#', r, g, b].join('');
163 <span id='Ext-draw-Color-method-toHex'> /**
164 </span> * Convert a color to hexadecimal format.
166 * @param {String|Array} color The color value (i.e 'rgb(255, 255, 255)', 'color: #ffffff').
167 * Can also be an Array, in this case the function handles the first member.
168 * @returns {String} The color in hexadecimal format.
170 toHex: function(color) {
171 if (Ext.isArray(color)) {
174 if (!Ext.isString(color)) {
177 if (color.substr(0, 1) === '#') {
180 var digits = this.colorToHexRe.exec(color);
182 if (Ext.isArray(digits)) {
183 var red = parseInt(digits[2], 10),
184 green = parseInt(digits[3], 10),
185 blue = parseInt(digits[4], 10),
186 rgb = blue | (green << 8) | (red << 16);
187 return digits[1] + '#' + ("000000" + rgb.toString(16)).slice(-6);
194 <span id='Ext-draw-Color-method-fromString'> /**
195 </span> * Parse the string and create a new color.
197 * Supported formats: '#rrggbb', '#rgb', and 'rgb(r,g,b)'.
199 * If the string is not recognized, an undefined will be returned instead.
201 * @param {String} str Color in string.
202 * @returns Ext.draw.Color
204 fromString: function(str) {
208 if ((str.length == 4 || str.length == 7) && str.substr(0, 1) === '#') {
209 values = str.match(this.hexRe);
211 r = parse(values[1], 16) >> 0;
212 g = parse(values[2], 16) >> 0;
213 b = parse(values[3], 16) >> 0;
214 if (str.length == 4) {
222 values = str.match(this.rgbRe);
230 return (typeof r == 'undefined') ? undefined : Ext.create('Ext.draw.Color', r, g, b);
233 <span id='Ext-draw-Color-method-getGrayscale'> /**
234 </span> * Returns the gray value (0 to 255) of the color.
236 * The gray value is calculated using the formula r*0.3 + g*0.59 + b*0.11.
240 getGrayscale: function() {
241 // http://en.wikipedia.org/wiki/Grayscale#Converting_color_to_grayscale
242 return this.r * 0.3 + this.g * 0.59 + this.b * 0.11;
245 <span id='Ext-draw-Color-method-fromHSL'> /**
246 </span> * Create a new color based on the specified HSL values.
248 * @param {Number} h Hue component (0..359)
249 * @param {Number} s Saturation component (0..1)
250 * @param {Number} l Lightness component (0..1)
251 * @returns Ext.draw.Color
253 fromHSL: function(h, s, l) {
254 var C, X, m, i, rgb = [],
258 if (s == 0 || h == null) {
263 // http://en.wikipedia.org/wiki/HSL_and_HSV#From_HSL
265 // X is the second largest component
266 // m is the lightness adjustment
268 C = s * (1 - abs(2 * l - 1));
269 X = C * (1 - abs(h - 2 * floor(h / 2) - 1));
291 rgb = [rgb[0] + m, rgb[1] + m, rgb[2] + m];
293 return Ext.create('Ext.draw.Color', rgb[0] * 255, rgb[1] * 255, rgb[2] * 255);
296 var prototype = this.prototype;
298 //These functions are both static and instance. TODO: find a more elegant way of copying them
300 fromHSL: function() {
301 return prototype.fromHSL.apply(prototype, arguments);
303 fromString: function() {
304 return prototype.fromString.apply(prototype, arguments);
307 return prototype.toHex.apply(prototype, arguments);