5 Functions for colour handling and processing.
7 http://www.safalra.com/web-design/javascript/colour-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/
20 /* An abstract Colour implementation. Concrete Colour implementations should use
21 * an instance of this function as their prototype, and implement the getRGB and
22 * getHSL functions. getRGB should return an object representing the RGB
23 * components of this Colour, with the red, green, and blue components in the
24 * range [0,255] and the alpha component in the range [0,100]. getHSL should
25 * return an object representing the HSL components of this Colour, with the hue
26 * component in the range [0,360), the saturation and lightness components in
27 * the range [0,100], and the alpha component in the range [0,1].
29 Roo.lib.Colour = function(){
31 /* Returns an object representing the RGBA components of this Colour. The red,
32 * green, and blue components are converted to integers in the range [0,255].
33 * The alpha is a value in the range [0,1].
35 this.getIntegerRGB = function(){
37 // get the RGB components of this colour
38 var rgb = this.getRGB();
40 // return the integer components
42 'r' : Math.round(rgb.r),
43 'g' : Math.round(rgb.g),
44 'b' : Math.round(rgb.b),
50 /* Returns an object representing the RGBA components of this Colour. The red,
51 * green, and blue components are converted to numbers in the range [0,100].
52 * The alpha is a value in the range [0,1].
54 this.getPercentageRGB = function(){
56 // get the RGB components of this colour
57 var rgb = this.getRGB();
59 // return the percentage components
61 'r' : 100 * rgb.r / 255,
62 'g' : 100 * rgb.g / 255,
63 'b' : 100 * rgb.b / 255,
69 /* Returns a string representing this Colour as a CSS hexadecimal RGB colour
70 * value - that is, a string of the form #RRGGBB where each of RR, GG, and BB
71 * are two-digit hexadecimal numbers.
73 this.getCSSHexadecimalRGB = function(){
75 // get the integer RGB components
76 var rgb = this.getIntegerRGB();
78 // determine the hexadecimal equivalents
79 var r16 = rgb.r.toString(16);
80 var g16 = rgb.g.toString(16);
81 var b16 = rgb.b.toString(16);
83 // return the CSS RGB colour value
85 + (r16.length == 2 ? r16 : '0' + r16)
86 + (g16.length == 2 ? g16 : '0' + g16)
87 + (b16.length == 2 ? b16 : '0' + b16);
91 /* Returns a string representing this Colour as a CSS integer RGB colour
92 * value - that is, a string of the form rgb(r,g,b) where each of r, g, and b
93 * are integers in the range [0,255].
95 this.getCSSIntegerRGB = function(){
97 // get the integer RGB components
98 var rgb = this.getIntegerRGB();
100 // return the CSS RGB colour value
101 return 'rgb(' + rgb.r + ',' + rgb.g + ',' + rgb.b + ')';
105 /* Returns a string representing this Colour as a CSS integer RGBA colour
106 * value - that is, a string of the form rgba(r,g,b,a) where each of r, g, and
107 * b are integers in the range [0,255] and a is in the range [0,1].
109 this.getCSSIntegerRGBA = function(){
111 // get the integer RGB components
112 var rgb = this.getIntegerRGB();
114 // return the CSS integer RGBA colour value
115 return 'rgb(' + rgb.r + ',' + rgb.g + ',' + rgb.b + ',' + rgb.a + ')';
119 /* Returns a string representing this Colour as a CSS percentage RGB colour
120 * value - that is, a string of the form rgb(r%,g%,b%) where each of r, g, and
121 * b are in the range [0,100].
123 this.getCSSPercentageRGB = function(){
125 // get the percentage RGB components
126 var rgb = this.getPercentageRGB();
128 // return the CSS RGB colour value
129 return 'rgb(' + rgb.r + '%,' + rgb.g + '%,' + rgb.b + '%)';
133 /* Returns a string representing this Colour as a CSS percentage RGBA colour
134 * value - that is, a string of the form rgba(r%,g%,b%,a) where each of r, g,
135 * and b are in the range [0,100] and a is in the range [0,1].
137 this.getCSSPercentageRGBA = function(){
139 // get the percentage RGB components
140 var rgb = this.getPercentageRGB();
142 // return the CSS percentage RGBA colour value
143 return 'rgb(' + rgb.r + '%,' + rgb.g + '%,' + rgb.b + '%,' + rgb.a + ')';
147 /* Returns a string representing this Colour as a CSS HSL colour value - that
148 * is, a string of the form hsl(h,s%,l%) where h is in the range [0,100] and
149 * s and l are in the range [0,100].
151 this.getCSSHSL = function(){
153 // get the HSL components
154 var hsl = this.getHSL();
156 // return the CSS HSL colour value
157 return 'hsl(' + hsl.h + ',' + hsl.s + '%,' + hsl.l + '%)';
161 /* Returns a string representing this Colour as a CSS HSLA colour value - that
162 * is, a string of the form hsla(h,s%,l%,a) where h is in the range [0,100],
163 * s and l are in the range [0,100], and a is in the range [0,1].
165 this.getCSSHSLA = function(){
167 // get the HSL components
168 var hsl = this.getHSL();
170 // return the CSS HSL colour value
171 return 'hsl(' + hsl.h + ',' + hsl.s + '%,' + hsl.l + '%,' + hsl.a + ')';
175 /* Sets the colour of the specified node to this Colour. This functions sets
176 * the CSS 'color' property for the node. The parameter is:
178 * node - the node whose colour should be set
180 this.setNodeColour = function(node){
182 // set the colour of the node
183 node.style.color = this.getCSSHexadecimalRGB();
187 /* Sets the background colour of the specified node to this Colour. This
188 * functions sets the CSS 'background-color' property for the node. The
191 * node - the node whose background colour should be set
193 this.setNodeBackgroundColour = function(node){
195 // set the background colour of the node
196 node.style.backgroundColor = this.getCSSHexadecimalRGB();
203 /* Creates a colour specified in the RGB colour space, with an optional alpha
204 * component. The parameters are:
206 * r - the red component, clipped to the range [0,255]
207 * g - the green component, clipped to the range [0,255]
208 * b - the blue component, clipped to the range [0,255]
209 * a - the alpha component, clipped to the range [0,1] - this parameter is
210 * optional and defaults to 1
212 Roo.lib.RGBColour = function (r, g, b, a){
214 // store the alpha component after clipping it if necessary
215 var alpha = (a === undefined ? 1 : Math.max(0, Math.min(1, a)));
217 // store the RGB components after clipping them if necessary
220 'r' : Math.max(0, Math.min(255, r)),
221 'g' : Math.max(0, Math.min(255, g)),
222 'b' : Math.max(0, Math.min(255, b))
225 // initialise the HSV and HSL components to null
229 /* Returns the HSV or HSL hue component of this RGBColour. The hue is in the
230 * range [0,360). The parameters are:
232 * maximum - the maximum of the RGB component values
233 * range - the range of the RGB component values
235 function getHue(maximum, range){
237 // check whether the range is zero
240 // set the hue to zero (any hue is acceptable as the colour is grey)
245 // determine which of the components has the highest value and set the hue
248 // red has the highest value
250 var hue = (rgb.g - rgb.b) / range * 60;
251 if (hue < 0) hue += 360;
254 // green has the highest value
256 var hue = (rgb.b - rgb.r) / range * 60 + 120;
259 // blue has the highest value
261 var hue = (rgb.r - rgb.g) / range * 60 + 240;
273 /* Calculates and stores the HSV components of this RGBColour so that they can
274 * be returned be the getHSV function.
276 function calculateHSV(){
278 // get the maximum and range of the RGB component values
279 var maximum = Math.max(rgb.r, rgb.g, rgb.b);
280 var range = maximum - Math.min(rgb.r, rgb.g, rgb.b);
282 // store the HSV components
285 'h' : getHue(maximum, range),
286 's' : (maximum == 0 ? 0 : 100 * range / maximum),
292 /* Calculates and stores the HSL components of this RGBColour so that they can
293 * be returned be the getHSL function.
295 function calculateHSL(){
297 // get the maximum and range of the RGB component values
298 var maximum = Math.max(rgb.r, rgb.g, rgb.b);
299 var range = maximum - Math.min(rgb.r, rgb.g, rgb.b);
301 // determine the lightness in the range [0,1]
302 var l = maximum / 255 - range / 510;
304 // store the HSL components
307 'h' : getHue(maximum, range),
308 's' : (range == 0 ? 0 : range / 2.55 / (l < 0.5 ? l * 2 : 2 - l * 2)),
314 /* Returns the RGB and alpha components of this RGBColour as an object with r,
315 * g, b, and a properties. r, g, and b are in the range [0,255] and a is in
318 this.getRGB = function(){
320 // return the RGB components
330 /* Returns the HSV and alpha components of this RGBColour as an object with h,
331 * s, v, and a properties. h is in the range [0,360), s and v are in the range
332 * [0,100], and a is in the range [0,1].
334 this.getHSV = function(){
336 // calculate the HSV components if necessary
337 if (hsv == null) calculateHSV();
339 // return the HSV components
349 /* Returns the HSL and alpha components of this RGBColour as an object with h,
350 * s, l, and a properties. h is in the range [0,360), s and l are in the range
351 * [0,100], and a is in the range [0,1].
353 this.getHSL = function(){
355 // calculate the HSV components if necessary
356 if (hsl == null) calculateHSL();
358 // return the HSL components
369 Roo.lib.RGBColour.prototype = new Roo.lib.Colour();
372 /* Creates a colour specified in the HSV colour space, with an optional alpha
373 * component. The parameters are:
375 * h - the hue component, wrapped to the range [0,360)
376 * s - the saturation component, clipped to the range [0,100]
377 * v - the value component, clipped to the range [0,100]
378 * a - the alpha component, clipped to the range [0,1] - this parameter is
379 * optional and defaults to 1
381 Roo.lib.HSVColour = function (h, s, v, a){
383 // store the alpha component after clipping it if necessary
384 var alpha = (a === undefined ? 1 : Math.max(0, Math.min(1, a)));
386 // store the HSV components after clipping or wrapping them if necessary
389 'h' : (h % 360 + 360) % 360,
390 's' : Math.max(0, Math.min(100, s)),
391 'v' : Math.max(0, Math.min(100, v))
394 // initialise the RGB and HSL components to null
398 /* Calculates and stores the RGB components of this HSVColour so that they can
399 * be returned be the getRGB function.
401 function calculateRGB(){
403 // check whether the saturation is zero
406 // set the colour to the appropriate shade of grey
413 // set some temporary values
414 var f = hsv.h / 60 - Math.floor(hsv.h / 60);
415 var p = hsv.v * (1 - hsv.s / 100);
416 var q = hsv.v * (1 - hsv.s / 100 * f);
417 var t = hsv.v * (1 - hsv.s / 100 * (1 - f));
419 // set the RGB colour components to their temporary values
420 switch (Math.floor(hsv.h / 60)){
421 case 0: var r = hsv.v; var g = t; var b = p; break;
422 case 1: var r = q; var g = hsv.v; var b = p; break;
423 case 2: var r = p; var g = hsv.v; var b = t; break;
424 case 3: var r = p; var g = q; var b = hsv.v; break;
425 case 4: var r = t; var g = p; var b = hsv.v; break;
426 case 5: var r = hsv.v; var g = p; var b = q; break;
431 // store the RGB components
441 /* Calculates and stores the HSL components of this HSVColour so that they can
442 * be returned be the getHSL function.
444 function calculateHSL(){
446 // determine the lightness in the range [0,100]
447 var l = (2 - hsv.s / 100) * hsv.v / 2;
449 // store the HSL components
453 's' : hsv.s * hsv.v / (l < 50 ? l * 2 : 200 - l * 2),
457 // correct a division-by-zero error
458 if (isNaN(hsl.s)) hsl.s = 0;
462 /* Returns the RGB and alpha components of this HSVColour as an object with r,
463 * g, b, and a properties. r, g, and b are in the range [0,255] and a is in
466 this.getRGB = function(){
468 // calculate the RGB components if necessary
469 if (rgb == null) calculateRGB();
471 // return the RGB components
481 /* Returns the HSV and alpha components of this HSVColour as an object with h,
482 * s, v, and a properties. h is in the range [0,360), s and v are in the range
483 * [0,100], and a is in the range [0,1].
485 this.getHSV = function(){
487 // return the HSV components
497 /* Returns the HSL and alpha components of this HSVColour as an object with h,
498 * s, l, and a properties. h is in the range [0,360), s and l are in the range
499 * [0,100], and a is in the range [0,1].
501 this.getHSL = function(){
503 // calculate the HSL components if necessary
504 if (hsl == null) calculateHSL();
506 // return the HSL components
517 Roo.lib.HSVColour.prototype = new Roo.lib.Colour();
520 /* Creates a colour specified in the HSL colour space, with an optional alpha
521 * component. The parameters are:
523 * h - the hue component, wrapped to the range [0,360)
524 * s - the saturation component, clipped to the range [0,100]
525 * l - the lightness component, clipped to the range [0,100]
526 * a - the alpha component, clipped to the range [0,1] - this parameter is
527 * optional and defaults to 1
530 Roo.lib.HSLColour = function(h, s, l, a){
532 // store the alpha component after clipping it if necessary
533 var alpha = (a === undefined ? 1 : Math.max(0, Math.min(1, a)));
535 // store the HSL components after clipping or wrapping them if necessary
538 'h' : (h % 360 + 360) % 360,
539 's' : Math.max(0, Math.min(100, s)),
540 'l' : Math.max(0, Math.min(100, l))
543 // initialise the RGB and HSV components to null
547 /* Calculates and stores the RGB components of this HSLColour so that they can
548 * be returned be the getRGB function.
550 function calculateRGB(){
552 // check whether the saturation is zero
555 // store the RGB components representing the appropriate shade of grey
565 // set some temporary values
567 ? hsl.l * (1 + hsl.s / 100)
568 : hsl.l + hsl.s - hsl.l * hsl.s / 100;
569 var q = 2 * hsl.l - p;
571 // initialise the RGB components
574 'r' : (h + 120) / 60 % 6,
576 'b' : (h + 240) / 60 % 6
579 // loop over the RGB components
580 for (var key in rgb){
582 // ensure that the property is not inherited from the root object
583 if (rgb.hasOwnProperty(key)){
585 // set the component to its value in the range [0,100]
587 rgb[key] = q + (p - q) * rgb[key];
588 }else if (rgb[key] < 3){
590 }else if (rgb[key] < 4){
591 rgb[key] = q + (p - q) * (4 - rgb[key]);
596 // set the component to its value in the range [0,255]
607 /* Calculates and stores the HSV components of this HSLColour so that they can
608 * be returned be the getHSL function.
610 function calculateHSV(){
612 // set a temporary value
613 var t = hsl.s * (hsl.l < 50 ? hsl.l : 100 - hsl.l) / 100;
615 // store the HSV components
619 's' : 200 * t / (hsl.l + t),
623 // correct a division-by-zero error
624 if (isNaN(hsv.s)) hsv.s = 0;
628 /* Returns the RGB and alpha components of this HSLColour as an object with r,
629 * g, b, and a properties. r, g, and b are in the range [0,255] and a is in
632 this.getRGB = function(){
634 // calculate the RGB components if necessary
635 if (rgb == null) calculateRGB();
637 // return the RGB components
647 /* Returns the HSV and alpha components of this HSLColour as an object with h,
648 * s, v, and a properties. h is in the range [0,360), s and v are in the range
649 * [0,100], and a is in the range [0,1].
651 this.getHSV = function(){
653 // calculate the HSV components if necessary
654 if (hsv == null) calculateHSV();
656 // return the HSV components
666 /* Returns the HSL and alpha components of this HSLColour as an object with h,
667 * s, l, and a properties. h is in the range [0,360), s and l are in the range
668 * [0,100], and a is in the range [0,1].
670 this.getHSL = function(){
672 // return the HSL components
683 Roo.lib.HSLColour.prototype = new Roo.lib.Colour();