4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../resources/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> * Represents an RGB color and provides helper functions get
20 * color components in HSL color space.
22 Ext.define('Ext.draw.Color', {
24 /* Begin Definitions */
28 colorToHexRe: /(.*?)rgb\((\d+),\s*(\d+),\s*(\d+)\)/,
29 rgbRe: /\s*rgb\s*\(\s*([0-9]+)\s*,\s*([0-9]+)\s*,\s*([0-9]+)\s*\)\s*/,
30 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*/,
32 <span id='Ext-draw-Color-cfg-lightnessFactor'> /**
33 </span> * @cfg {Number} lightnessFactor
35 * The default factor to compute the lighter or darker color. Defaults to 0.2.
39 <span id='Ext-draw-Color-method-constructor'> /**
40 </span> * Creates new Color.
41 * @param {Number} red Red component (0..255)
42 * @param {Number} green Green component (0..255)
43 * @param {Number} blue Blue component (0..255)
45 constructor : function(red, green, blue) {
47 clamp = Ext.Number.constrain;
48 me.r = clamp(red, 0, 255);
49 me.g = clamp(green, 0, 255);
50 me.b = clamp(blue, 0, 255);
53 <span id='Ext-draw-Color-method-getRed'> /**
54 </span> * Get the red component of the color, in the range 0..255.
61 <span id='Ext-draw-Color-method-getGreen'> /**
62 </span> * Get the green component of the color, in the range 0..255.
65 getGreen: function() {
69 <span id='Ext-draw-Color-method-getBlue'> /**
70 </span> * Get the blue component of the color, in the range 0..255.
77 <span id='Ext-draw-Color-method-getRGB'> /**
78 </span> * Get the RGB values.
83 return [me.r, me.g, me.b];
86 <span id='Ext-draw-Color-method-getHSL'> /**
87 </span> * Get the equivalent HSL components of the color.
95 max = Math.max(r, g, b),
96 min = Math.min(r, g, b),
100 l = 0.5 * (max + min);
102 // min==max means achromatic (hue is undefined)
104 s = (l < 0.5) ? delta / (max + min) : delta / (2 - max - min);
106 h = 60 * (g - b) / delta;
107 } else if (g == max) {
108 h = 120 + 60 * (b - r) / delta;
110 h = 240 + 60 * (r - g) / delta;
122 <span id='Ext-draw-Color-method-getLighter'> /**
123 </span> * Return a new color that is lighter than this color.
124 * @param {Number} factor Lighter factor (0..1), default to 0.2
125 * @return Ext.draw.Color
127 getLighter: function(factor) {
128 var hsl = this.getHSL();
129 factor = factor || this.lightnessFactor;
130 hsl[2] = Ext.Number.constrain(hsl[2] + factor, 0, 1);
131 return this.fromHSL(hsl[0], hsl[1], hsl[2]);
134 <span id='Ext-draw-Color-method-getDarker'> /**
135 </span> * Return a new color that is darker than this color.
136 * @param {Number} factor Darker factor (0..1), default to 0.2
137 * @return Ext.draw.Color
139 getDarker: function(factor) {
140 factor = factor || this.lightnessFactor;
141 return this.getLighter(-factor);
144 <span id='Ext-draw-Color-method-toString'> /**
145 </span> * Return the color in the hex format, i.e. '#rrggbb'.
148 toString: function() {
151 r = round(me.r).toString(16),
152 g = round(me.g).toString(16),
153 b = round(me.b).toString(16);
154 r = (r.length == 1) ? '0' + r : r;
155 g = (g.length == 1) ? '0' + g : g;
156 b = (b.length == 1) ? '0' + b : b;
157 return ['#', r, g, b].join('');
160 <span id='Ext-draw-Color-static-method-toHex'> /**
161 </span> * Convert a color to hexadecimal format.
163 * **Note:** This method is both static and instance.
165 * @param {String/String[]} color The color value (i.e 'rgb(255, 255, 255)', 'color: #ffffff').
166 * Can also be an Array, in this case the function handles the first member.
167 * @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-static-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 * **Note:** This method is both static and instance.
203 * @param {String} str Color in string.
204 * @returns Ext.draw.Color
207 fromString: function(str) {
211 if ((str.length == 4 || str.length == 7) && str.substr(0, 1) === '#') {
212 values = str.match(this.hexRe);
214 r = parse(values[1], 16) >> 0;
215 g = parse(values[2], 16) >> 0;
216 b = parse(values[3], 16) >> 0;
217 if (str.length == 4) {
225 values = str.match(this.rgbRe);
233 return (typeof r == 'undefined') ? undefined : Ext.create('Ext.draw.Color', r, g, b);
236 <span id='Ext-draw-Color-method-getGrayscale'> /**
237 </span> * Returns the gray value (0 to 255) of the color.
239 * The gray value is calculated using the formula r*0.3 + g*0.59 + b*0.11.
243 getGrayscale: function() {
244 // http://en.wikipedia.org/wiki/Grayscale#Converting_color_to_grayscale
245 return this.r * 0.3 + this.g * 0.59 + this.b * 0.11;
248 <span id='Ext-draw-Color-static-method-fromHSL'> /**
249 </span> * Create a new color based on the specified HSL values.
251 * **Note:** This method is both static and instance.
253 * @param {Number} h Hue component (0..359)
254 * @param {Number} s Saturation component (0..1)
255 * @param {Number} l Lightness component (0..1)
256 * @returns Ext.draw.Color
259 fromHSL: function(h, s, l) {
260 var C, X, m, i, rgb = [],
264 if (s == 0 || h == null) {
269 // http://en.wikipedia.org/wiki/HSL_and_HSV#From_HSL
271 // X is the second largest component
272 // m is the lightness adjustment
274 C = s * (1 - abs(2 * l - 1));
275 X = C * (1 - abs(h - 2 * floor(h / 2) - 1));
297 rgb = [rgb[0] + m, rgb[1] + m, rgb[2] + m];
299 return Ext.create('Ext.draw.Color', rgb[0] * 255, rgb[1] * 255, rgb[2] * 255);
302 var prototype = this.prototype;
304 //These functions are both static and instance. TODO: find a more elegant way of copying them
306 fromHSL: function() {
307 return prototype.fromHSL.apply(prototype, arguments);
309 fromString: function() {
310 return prototype.fromString.apply(prototype, arguments);
313 return prototype.toHex.apply(prototype, arguments);