5 Functions for Color handling and processing.
7 http://www.safalra.com/web-design/javascript/Color-handling-and-processing/
9 The author of this program, Safalra (Stephen Morley), irrevocably releases all
10 rights to this program, with the intention of it becoming part of the public
11 domain. Because this program is released into the public domain, it comes with
12 no warranty either expressed or implied, to the extent permitted by law.
14 For more free and public domain JavaScript code by the same author, visit:
15 http://www.safalra.com/web-design/javascript/
21 * @class Roo.lib.Color
23 * An abstract Color implementation. Concrete Color implementations should use
24 * an instance of this function as their prototype, and implement the getRGB and
25 * getHSL functions. getRGB should return an object representing the RGB
26 * components of this Color, with the red, green, and blue components in the
27 * range [0,255] and the alpha component in the range [0,100]. getHSL should
28 * return an object representing the HSL components of this Color, with the hue
29 * component in the range [0,360), the saturation and lightness components in
30 * the range [0,100], and the alpha component in the range [0,1].
32 Roo.lib.Color = function() { }
35 Roo.apply(Roo.lib.Color.prototype, {
42 * @returns an object representing the RGBA components of this Color. The red,
43 * green, and blue components are converted to integers in the range [0,255].
44 * The alpha is a value in the range [0,1].
46 getIntegerRGB : function(){
48 // get the RGB components of this Color
49 var rgb = this.getRGB();
51 // return the integer components
53 'r' : Math.round(rgb.r),
54 'g' : Math.round(rgb.g),
55 'b' : Math.round(rgb.b),
62 * @returns an object representing the RGBA components of this Color. The red,
63 * green, and blue components are converted to numbers in the range [0,100].
64 * The alpha is a value in the range [0,1].
66 getPercentageRGB : function(){
68 // get the RGB components of this Color
69 var rgb = this.getRGB();
71 // return the percentage components
73 'r' : 100 * rgb.r / 255,
74 'g' : 100 * rgb.g / 255,
75 'b' : 100 * rgb.b / 255,
82 * @returns a string representing this Color as a CSS hexadecimal RGB Color
83 * value - that is, a string of the form #RRGGBB where each of RR, GG, and BB
84 * are two-digit hexadecimal numbers.
86 getCSSHexadecimalRGB : function()
89 // get the integer RGB components
90 var rgb = this.getIntegerRGB();
92 // determine the hexadecimal equivalents
93 var r16 = rgb.r.toString(16);
94 var g16 = rgb.g.toString(16);
95 var b16 = rgb.b.toString(16);
97 // return the CSS RGB Color value
99 + (r16.length == 2 ? r16 : '0' + r16)
100 + (g16.length == 2 ? g16 : '0' + g16)
101 + (b16.length == 2 ? b16 : '0' + b16);
106 * @returns a string representing this Color as a CSS integer RGB Color
107 * value - that is, a string of the form rgb(r,g,b) where each of r, g, and b
108 * are integers in the range [0,255].
110 getCSSIntegerRGB : function(){
112 // get the integer RGB components
113 var rgb = this.getIntegerRGB();
115 // return the CSS RGB Color value
116 return 'rgb(' + rgb.r + ',' + rgb.g + ',' + rgb.b + ')';
121 * @returns Returns a string representing this Color as a CSS integer RGBA Color
122 * value - that is, a string of the form rgba(r,g,b,a) where each of r, g, and
123 * b are integers in the range [0,255] and a is in the range [0,1].
125 getCSSIntegerRGBA : function(){
127 // get the integer RGB components
128 var rgb = this.getIntegerRGB();
130 // return the CSS integer RGBA Color value
131 return 'rgb(' + rgb.r + ',' + rgb.g + ',' + rgb.b + ',' + rgb.a + ')';
136 * @returns a string representing this Color as a CSS percentage RGB Color
137 * value - that is, a string of the form rgb(r%,g%,b%) where each of r, g, and
138 * b are in the range [0,100].
140 getCSSPercentageRGB : function(){
142 // get the percentage RGB components
143 var rgb = this.getPercentageRGB();
145 // return the CSS RGB Color value
146 return 'rgb(' + rgb.r + '%,' + rgb.g + '%,' + rgb.b + '%)';
151 * @returns a string representing this Color as a CSS percentage RGBA Color
152 * value - that is, a string of the form rgba(r%,g%,b%,a) where each of r, g,
153 * and b are in the range [0,100] and a is in the range [0,1].
155 getCSSPercentageRGBA : function(){
157 // get the percentage RGB components
158 var rgb = this.getPercentageRGB();
160 // return the CSS percentage RGBA Color value
161 return 'rgb(' + rgb.r + '%,' + rgb.g + '%,' + rgb.b + '%,' + rgb.a + ')';
166 * @returns a string representing this Color as a CSS HSL Color value - that
167 * is, a string of the form hsl(h,s%,l%) where h is in the range [0,100] and
168 * s and l are in the range [0,100].
170 getCSSHSL : function(){
172 // get the HSL components
173 var hsl = this.getHSL();
175 // return the CSS HSL Color value
176 return 'hsl(' + hsl.h + ',' + hsl.s + '%,' + hsl.l + '%)';
181 * @returns a string representing this Color as a CSS HSLA Color value - that
182 * is, a string of the form hsla(h,s%,l%,a) where h is in the range [0,100],
183 * s and l are in the range [0,100], and a is in the range [0,1].
185 getCSSHSLA : function(){
187 // get the HSL components
188 var hsl = this.getHSL();
190 // return the CSS HSL Color value
191 return 'hsl(' + hsl.h + ',' + hsl.s + '%,' + hsl.l + '%,' + hsl.a + ')';
196 * Sets the Color of the specified node to this Color. This functions sets
197 * the CSS 'color' property for the node. The parameter is:
199 * @param {DomElement} node - the node whose Color should be set
201 setNodeColor : function(node){
203 // set the Color of the node
204 node.style.color = this.getCSSHexadecimalRGB();
209 * Sets the background Color of the specified node to this Color. This
210 * functions sets the CSS 'background-color' property for the node. The
213 * @param {DomElement} node - the node whose background Color should be set
215 setNodeBackgroundColor : function(node){
217 // set the background Color of the node
218 node.style.backgroundColor = this.getCSSHexadecimalRGB();
221 // convert between formats..
224 var r = this.getIntegerRGB();
225 return new Roo.lib.RGBColor(r.r,r.g,r.b,r.a);
230 var hsl = this.getHSL();
231 // return the CSS HSL Color value
232 return new Roo.lib.HSLColor(hsl.h, hsl.s, hsl.l , hsl.a );
238 var rgb = this.toRGB();
239 var hsv = rgb.getHSV();
240 // return the CSS HSL Color value
241 return new Roo.lib.HSVColor(hsv.h, hsv.s, hsv.v , hsv.a );
245 // modify v = 0 ... 1 (eg. 0.5)
246 saturate : function(v)
248 var rgb = this.toRGB();
249 var hsv = rgb.getHSV();
250 return new Roo.lib.HSVColor(hsv.h, hsv.s * v, hsv.v , hsv.a );
262 * @class Roo.lib.RGBColor
263 * @extends Roo.lib.Color
264 * Creates a Color specified in the RGB Color space, with an optional alpha
265 * component. The parameters are:
267 * r - the red component, clipped to the range [0,255]
268 * g - the green component, clipped to the range [0,255]
269 * b - the blue component, clipped to the range [0,255]
270 * a - the alpha component, clipped to the range [0,1] - this parameter is
271 * optional and defaults to 1
273 Roo.lib.RGBColor = function (r, g, b, a){
275 // store the alpha component after clipping it if necessary
276 var alpha = (a === undefined ? 1 : Math.max(0, Math.min(1, a)));
278 // store the RGB components after clipping them if necessary
281 'r' : Math.max(0, Math.min(255, r)),
282 'g' : Math.max(0, Math.min(255, g)),
283 'b' : Math.max(0, Math.min(255, b))
286 // initialise the HSV and HSL components to null
290 * //private returns the HSV or HSL hue component of this RGBColor. The hue is in the
291 * range [0,360). The parameters are:
293 * maximum - the maximum of the RGB component values
294 * range - the range of the RGB component values
299 // this does an 'exteds'
300 Roo.extend(Roo.lib.RGBColor, Roo.lib.Color, {
303 getHue : function(maximum, range){
305 // check whether the range is zero
308 // set the hue to zero (any hue is acceptable as the Color is grey)
313 // determine which of the components has the highest value and set the hue
316 // red has the highest value
318 var hue = (rgb.g - rgb.b) / range * 60;
319 if (hue < 0) hue += 360;
322 // green has the highest value
324 var hue = (rgb.b - rgb.r) / range * 60 + 120;
327 // blue has the highest value
329 var hue = (rgb.r - rgb.g) / range * 60 + 240;
341 /* //private Calculates and stores the HSV components of this RGBColor so that they can
342 * be returned be the getHSV function.
344 calculateHSV : function(){
346 // get the maximum and range of the RGB component values
347 var maximum = Math.max(rgb.r, rgb.g, rgb.b);
348 var range = maximum - Math.min(rgb.r, rgb.g, rgb.b);
350 // store the HSV components
353 'h' : this.getHue(maximum, range),
354 's' : (maximum == 0 ? 0 : 100 * range / maximum),
360 /* //private Calculates and stores the HSL components of this RGBColor so that they can
361 * be returned be the getHSL function.
363 calculateHSL : function(){
365 // get the maximum and range of the RGB component values
366 var maximum = Math.max(rgb.r, rgb.g, rgb.b);
367 var range = maximum - Math.min(rgb.r, rgb.g, rgb.b);
369 // determine the lightness in the range [0,1]
370 var l = maximum / 255 - range / 510;
372 // store the HSL components
375 'h' : this.getHue(maximum, range),
376 's' : (range == 0 ? 0 : range / 2.55 / (l < 0.5 ? l * 2 : 2 - l * 2)),
383 * @returns the RGB and alpha components of this RGBColor as an object with r,
384 * g, b, and a properties. r, g, and b are in the range [0,255] and a is in
389 // return the RGB components
400 * @returns the HSV and alpha components of this RGBColor as an object with h,
401 * s, v, and a properties. h is in the range [0,360), s and v are in the range
402 * [0,100], and a is in the range [0,1].
407 // calculate the HSV components if necessary
408 if (hsv == null) this.calculateHSV();
410 // return the HSV components
421 * @returns the HSL and alpha components of this RGBColor as an object with h,
422 * s, l, and a properties. h is in the range [0,360), s and l are in the range
423 * [0,100], and a is in the range [0,1].
427 // calculate the HSV components if necessary
428 if (hsl == null) this.calculateHSL();
430 // return the HSL components
442 * @class Roo.lib.HSVColor
443 * @extends Roo.lib.Color
444 * Creates a Color specified in the HSV Color space, with an optional alpha
445 * component. The parameters are:
447 * h - the hue component, wrapped to the range [0,360)
448 * s - the saturation component, clipped to the range [0,100]
449 * v - the value component, clipped to the range [0,100]
450 * a - the alpha component, clipped to the range [0,1] - this parameter is
451 * optional and defaults to 1
453 Roo.lib.HSVColor = function (h, s, v, a){
455 // store the alpha component after clipping it if necessary
456 var alpha = (a === undefined ? 1 : Math.max(0, Math.min(1, a)));
458 // store the HSV components after clipping or wrapping them if necessary
461 'h' : (h % 360 + 360) % 360,
462 's' : Math.max(0, Math.min(100, s)),
463 'v' : Math.max(0, Math.min(100, v))
466 // initialise the RGB and HSL components to null
470 /* Calculates and stores the RGB components of this HSVColor so that they can
471 * be returned be the getRGB function.
473 function calculateRGB(){
475 // check whether the saturation is zero
478 // set the Color to the appropriate shade of grey
485 // set some temporary values
486 var f = hsv.h / 60 - Math.floor(hsv.h / 60);
487 var p = hsv.v * (1 - hsv.s / 100);
488 var q = hsv.v * (1 - hsv.s / 100 * f);
489 var t = hsv.v * (1 - hsv.s / 100 * (1 - f));
491 // set the RGB Color components to their temporary values
492 switch (Math.floor(hsv.h / 60)){
493 case 0: var r = hsv.v; var g = t; var b = p; break;
494 case 1: var r = q; var g = hsv.v; var b = p; break;
495 case 2: var r = p; var g = hsv.v; var b = t; break;
496 case 3: var r = p; var g = q; var b = hsv.v; break;
497 case 4: var r = t; var g = p; var b = hsv.v; break;
498 case 5: var r = hsv.v; var g = p; var b = q; break;
503 // store the RGB components
513 /* Calculates and stores the HSL components of this HSVColor so that they can
514 * be returned be the getHSL function.
516 calculateHSL : function (){
518 // determine the lightness in the range [0,100]
519 var l = (2 - hsv.s / 100) * hsv.v / 2;
521 // store the HSL components
525 's' : hsv.s * hsv.v / (l < 50 ? l * 2 : 200 - l * 2),
529 // correct a division-by-zero error
530 if (isNaN(hsl.s)) hsl.s = 0;
535 * @returns the RGB and alpha components of this HSVColor as an object with r,
536 * g, b, and a properties. r, g, and b are in the range [0,255] and a is in
539 this.getRGB = function(){
541 // calculate the RGB components if necessary
542 if (rgb == null) calculateRGB();
544 // return the RGB components
555 * @returns the HSV and alpha components of this HSVColor as an object with h,
556 * s, v, and a properties. h is in the range [0,360), s and v are in the range
557 * [0,100], and a is in the range [0,1].
559 this.getHSV = function(){
561 // return the HSV components
572 * @returns the HSL and alpha components of this HSVColor as an object with h,
573 * s, l, and a properties. h is in the range [0,360), s and l are in the range
574 * [0,100], and a is in the range [0,1].
576 this.getHSL = function(){
578 // calculate the HSL components if necessary
579 if (hsl == null) this.calculateHSL();
581 // return the HSL components
592 Roo.lib.HSVColor.prototype = new Roo.lib.Color();
596 * @class Roo.lib.HSLColor
597 * @extends Roo.lib.Color
599 * Creates a Color specified in the HSL Color space, with an optional alpha
600 * component. The parameters are:
602 * h - the hue component, wrapped to the range [0,360)
603 * s - the saturation component, clipped to the range [0,100]
604 * l - the lightness component, clipped to the range [0,100]
605 * a - the alpha component, clipped to the range [0,1] - this parameter is
606 * optional and defaults to 1
609 Roo.lib.HSLColor = function(h, s, l, a){
611 // store the alpha component after clipping it if necessary
612 var alpha = (a === undefined ? 1 : Math.max(0, Math.min(1, a)));
614 // store the HSL components after clipping or wrapping them if necessary
617 'h' : (h % 360 + 360) % 360,
618 's' : Math.max(0, Math.min(100, s)),
619 'l' : Math.max(0, Math.min(100, l))
622 // initialise the RGB and HSV components to null
626 /* Calculates and stores the RGB components of this HSLColor so that they can
627 * be returned be the getRGB function.
629 function calculateRGB(){
631 // check whether the saturation is zero
634 // store the RGB components representing the appropriate shade of grey
644 // set some temporary values
646 ? hsl.l * (1 + hsl.s / 100)
647 : hsl.l + hsl.s - hsl.l * hsl.s / 100;
648 var q = 2 * hsl.l - p;
650 // initialise the RGB components
653 'r' : (h + 120) / 60 % 6,
655 'b' : (h + 240) / 60 % 6
658 // loop over the RGB components
659 for (var key in rgb){
661 // ensure that the property is not inherited from the root object
662 if (rgb.hasOwnProperty(key)){
664 // set the component to its value in the range [0,100]
666 rgb[key] = q + (p - q) * rgb[key];
667 }else if (rgb[key] < 3){
669 }else if (rgb[key] < 4){
670 rgb[key] = q + (p - q) * (4 - rgb[key]);
675 // set the component to its value in the range [0,255]
686 /* Calculates and stores the HSV components of this HSLColor so that they can
687 * be returned be the getHSL function.
689 calculateHSV : function(){
691 // set a temporary value
692 var t = hsl.s * (hsl.l < 50 ? hsl.l : 100 - hsl.l) / 100;
694 // store the HSV components
698 's' : 200 * t / (hsl.l + t),
702 // correct a division-by-zero error
703 if (isNaN(hsv.s)) hsv.s = 0;
708 * @returns the RGB and alpha components of this HSLColor as an object with r,
709 * g, b, and a properties. r, g, and b are in the range [0,255] and a is in
712 this.getRGB = function(){
714 // calculate the RGB components if necessary
715 if (rgb == null) calculateRGB();
717 // return the RGB components
728 * @returns the HSV and alpha components of this HSLColor as an object with h,
729 * s, v, and a properties. h is in the range [0,360), s and v are in the range
730 * [0,100], and a is in the range [0,1].
732 this.getHSV = function(){
734 // calculate the HSV components if necessary
735 if (hsv == null) this.calculateHSV();
737 // return the HSV components
748 * @returns the HSL and alpha components of this HSLColor as an object with h,
749 * s, l, and a properties. h is in the range [0,360), s and l are in the range
750 * [0,100], and a is in the range [0,1].
752 this.getHSL = function(){
754 // return the HSL components
765 Roo.lib.HSLColor.prototype = new Roo.lib.Color();