2 * jQuery Color Utilities
3 * Written by Krinkle in 2011
4 * Released under the MIT and GPL licenses.
5 * Mostly based on other plugins and functions (taken through JSLint and optimized a little).
6 * Sources cited locally.
12 // Color Conversion function from highlightFade
13 // By Blair Mitchelmore
14 // http://jquery.offput.ca/highlightFade/
15 // Parse strings looking for color tuples [255,255,255]
16 getRGB : function( color
) {
19 // Check if we're already dealing with an array of colors
20 if ( color
&& color
.constructor == Array
&& color
.length
== 3 ){
24 // Look for rgb(num,num,num)
25 if (result
= /rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(color
)) {
26 return [parseInt(result
[1],10), parseInt(result
[2],10), parseInt(result
[3],10)];
29 // Look for rgb(num%,num%,num%)
30 if (result
= /rgb\(\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*\)/.exec(color
)) {
31 return [parseFloat(result
[1],10)*2.55, parseFloat(result
[2],10)*2.55, parseFloat(result
[3])*2.55];
35 if (result
= /#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/.exec(color
)) {
36 return [parseInt(result
[1],16), parseInt(result
[2],16), parseInt(result
[3],16)];
40 if (result
= /#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])/.exec(color
)) {
41 return [parseInt(result
[1]+result
[1],16), parseInt(result
[2]+result
[2],16), parseInt(result
[3]+result
[3],16)];
44 // Look for rgba(0, 0, 0, 0) == transparent in Safari 3
45 if (result
= /rgba\(0, 0, 0, 0\)/.exec(color
)) {
46 return $.colorUtil
.colors
.transparent
;
49 // Otherwise, we're most likely dealing with a named color
50 return $.colorUtil
.colors
[jQuery
.trim(color
).toLowerCase()];
53 // Some named colors to work with
54 // From Interface by Stefan Petre
55 // http://interface.eyecon.ro/
66 darkgrey
:[169,169,169],
68 darkkhaki
:[189,183,107],
69 darkmagenta
:[139,0,139],
70 darkolivegreen
:[85,107,47],
71 darkorange
:[255,140,0],
72 darkorchid
:[153,50,204],
74 darksalmon
:[233,150,122],
75 darkviolet
:[148,0,211],
81 lightblue
:[173,216,230],
82 lightcyan
:[224,255,255],
83 lightgreen
:[144,238,144],
84 lightgrey
:[211,211,211],
85 lightpink
:[255,182,193],
86 lightyellow
:[255,255,224],
100 transparent
: [255,255,255]
103 * http://mjijackson.com/2008/02/rgb-to-hsl-and-rgb-to-hsv-color-model-conversion-algorithms-in-javascript
104 * Converts an RGB color value to HSL. Conversion formula
105 * adapted from http://en.wikipedia.org/wiki/HSL_color_space.
106 * Assumes r, g, and b are contained in the set [0, 255] and
107 * returns h, s, and l in the set [0, 1].
109 * @param Number R The red color value
110 * @param Number G The green color value
111 * @param Number B The blue color value
112 * @return Array The HSL representation
114 rgbToHsl: function( R
, G
, B
) {
118 var max
= Math
.max(r
, g
, b
), min
= Math
.min(r
, g
, b
);
119 var h
, s
, l
= (max
+ min
) / 2;
122 h
= s
= 0; // achromatic
125 s
= l
> 0.5 ? d
/ (2 - max
- min
) : d
/ (max
+ min
);
127 case r
: h
= (g
- b
) / d
+ (g
< b
? 6 : 0); break;
128 case g
: h
= (b
- r
) / d
+ 2; break;
129 case b
: h
= (r
- g
) / d
+ 4; break;
137 * http://mjijackson.com/2008/02/rgb-to-hsl-and-rgb-to-hsv-color-model-conversion-algorithms-in-javascript
138 * Converts an HSL color value to RGB. Conversion formula
139 * adapted from http://en.wikipedia.org/wiki/HSL_color_space.
140 * Assumes h, s, and l are contained in the set [0, 1] and
141 * returns r, g, and b in the set [0, 255].
143 * @param Number h The hue
144 * @param Number s The saturation
145 * @param Number l The lightness
146 * @return Array The RGB representation
148 hslToRgb: function( h
, s
, l
) {
152 r
= g
= b
= l
; // achromatic
154 var hue2rgb = function(p
, q
, t
){
157 if(t
< 1/6){ return p
+ (q
- p
) * 6 * t
; }
158 if(t
< 1/2){ return q
; }
159 if(t
< 2/3){ return p + (q - p) * (2/3 - t
) * 6; }
163 var q
= l
< 0.5 ? l
* (1 + s
) : l
+ s
- l
* s
;
165 r
= hue2rgb(p
, q
, h
+ 1/3);
166 g
= hue2rgb(p
, q
, h
);
167 b
= hue2rgb(p
, q
, h
- 1/3);
170 return [r
* 255, g
* 255, b
* 255];
173 * Get's a brighter or darker rgb() value string.
177 * @example getCSSColorMod( 'red', +0.1 )
178 * @example getCSSColorMod( 'rgb(200,50,50)', -0.2 )
180 * @param Mixed currentColor current value in css
181 * @param Number mod wanted brightness modification between -1 and 1
182 * @return String 'rgb(r,g,b)'
184 getColorBrightness: function( currentColor
, mod
) {
185 var rgbArr
= $.colorUtil
.getRGB( currentColor
),
186 hslArr
= $.colorUtil
.rgbToHsl(rgbArr
[0], rgbArr
[1], rgbArr
[2] );
187 rgbArr
= $.colorUtil
.hslToRgb(hslArr
[0], hslArr
[1], hslArr
[2]+mod
);
189 [parseInt( rgbArr
[0], 10), parseInt( rgbArr
[1], 10 ), parseInt( rgbArr
[2], 10 )].join( ',' ) +