2 * jQuery Color Utilities
4 * Released under the MIT and GPL licenses.
6 * Mostly based on other plugins and functions (linted and optimized a little).
7 * Sources cited inline.
11 * @class jQuery.colorUtil
17 * Parse CSS color strings looking for color tuples
19 * Based on highlightFade by Blair Mitchelmore
20 * <http://jquery.offput.ca/highlightFade/>
22 * @param {Array|string} color
25 getRGB: function ( color
) {
28 // Check if we're already dealing with an array of colors
29 if ( color
&& $.isArray( color
) && color
.length
=== 3 ) {
33 // Look for rgb(num,num,num)
34 // eslint-disable-next-line no-cond-assign
35 if ( result
= /rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec( color
) ) {
37 parseInt( result
[ 1 ], 10 ),
38 parseInt( result
[ 2 ], 10 ),
39 parseInt( result
[ 3 ], 10 )
43 // Look for rgb(num%,num%,num%)
44 // eslint-disable-next-line no-cond-assign
45 if ( result
= /rgb\(\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*\)/.exec( color
) ) {
47 parseFloat( result
[ 1 ] ) * 2.55,
48 parseFloat( result
[ 2 ] ) * 2.55,
49 parseFloat( result
[ 3 ] ) * 2.55
54 // eslint-disable-next-line no-cond-assign
55 if ( result
= /#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/.exec( color
) ) {
57 parseInt( result
[ 1 ], 16 ),
58 parseInt( result
[ 2 ], 16 ),
59 parseInt( result
[ 3 ], 16 )
64 // eslint-disable-next-line no-cond-assign
65 if ( result
= /#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])/.exec( color
) ) {
67 parseInt( result
[ 1 ] + result
[ 1 ], 16 ),
68 parseInt( result
[ 2 ] + result
[ 2 ], 16 ),
69 parseInt( result
[ 3 ] + result
[ 3 ], 16 )
73 // Look for rgba(0, 0, 0, 0) == transparent in Safari 3
74 // eslint-disable-next-line no-cond-assign
75 if ( result
= /rgba\(0, 0, 0, 0\)/.exec( color
) ) {
76 return $.colorUtil
.colors
.transparent
;
79 // Otherwise, we're most likely dealing with a named color
80 return $.colorUtil
.colors
[ $.trim( color
).toLowerCase() ];
86 * Based on Interface by Stefan Petre
87 * <http://interface.eyecon.ro/>
92 aqua
: [ 0, 255, 255 ],
93 azure
: [ 240, 255, 255 ],
94 beige
: [ 245, 245, 220 ],
97 brown
: [ 165, 42, 42 ],
98 cyan
: [ 0, 255, 255 ],
99 darkblue
: [ 0, 0, 139 ],
100 darkcyan
: [ 0, 139, 139 ],
101 darkgrey
: [ 169, 169, 169 ],
102 darkgreen
: [ 0, 100, 0 ],
103 darkkhaki
: [ 189, 183, 107 ],
104 darkmagenta
: [ 139, 0, 139 ],
105 darkolivegreen
: [ 85, 107, 47 ],
106 darkorange
: [ 255, 140, 0 ],
107 darkorchid
: [ 153, 50, 204 ],
108 darkred
: [ 139, 0, 0 ],
109 darksalmon
: [ 233, 150, 122 ],
110 darkviolet
: [ 148, 0, 211 ],
111 fuchsia
: [ 255, 0, 255 ],
112 gold
: [ 255, 215, 0 ],
113 green
: [ 0, 128, 0 ],
114 indigo
: [ 75, 0, 130 ],
115 khaki
: [ 240, 230, 140 ],
116 lightblue
: [ 173, 216, 230 ],
117 lightcyan
: [ 224, 255, 255 ],
118 lightgreen
: [ 144, 238, 144 ],
119 lightgrey
: [ 211, 211, 211 ],
120 lightpink
: [ 255, 182, 193 ],
121 lightyellow
: [ 255, 255, 224 ],
123 magenta
: [ 255, 0, 255 ],
124 maroon
: [ 128, 0, 0 ],
126 olive
: [ 128, 128, 0 ],
127 orange
: [ 255, 165, 0 ],
128 pink
: [ 255, 192, 203 ],
129 purple
: [ 128, 0, 128 ],
130 violet
: [ 128, 0, 128 ],
132 silver
: [ 192, 192, 192 ],
133 white
: [ 255, 255, 255 ],
134 yellow
: [ 255, 255, 0 ],
135 transparent
: [ 255, 255, 255 ]
139 * Convert an RGB color value to HSL.
141 * Conversion formula based on
142 * <http://mjijackson.com/2008/02/rgb-to-hsl-and-rgb-to-hsv-color-model-conversion-algorithms-in-javascript>
144 * Adapted from <https://en.wikipedia.org/wiki/HSL_color_space>.
146 * Assumes `r`, `g`, and `b` are contained in the set `[0, 255]` and
147 * returns `h`, `s`, and `l` in the set `[0, 1]`.
149 * @param {number} r The red color value
150 * @param {number} g The green color value
151 * @param {number} b The blue color value
152 * @return {number[]} The HSL representation
154 rgbToHsl: function ( r
, g
, b
) {
155 var d
, h
, s
, l
, min
, max
;
161 max
= Math
.max( r
, g
, b
);
162 min
= Math
.min( r
, g
, b
);
163 l
= ( max
+ min
) / 2;
170 s
= l
> 0.5 ? d
/ ( 2 - max
- min
) : d
/ ( max
+ min
);
173 h
= ( g
- b
) / d
+ ( g
< b
? 6 : 0 );
176 h
= ( b
- r
) / d
+ 2;
179 h
= ( r
- g
) / d
+ 4;
189 * Convert an HSL color value to RGB.
191 * Conversion formula based on
192 * <http://mjijackson.com/2008/02/rgb-to-hsl-and-rgb-to-hsv-color-model-conversion-algorithms-in-javascript>
194 * Adapted from <https://en.wikipedia.org/wiki/HSL_color_space>.
196 * Assumes `h`, `s`, and `l` are contained in the set `[0, 1]` and
197 * returns `r`, `g`, and `b` in the set `[0, 255]`.
199 * @param {number} h The hue
200 * @param {number} s The saturation
201 * @param {number} l The lightness
202 * @return {number[]} The RGB representation
204 hslToRgb: function ( h
, s
, l
) {
205 var r
, g
, b
, hue2rgb
, q
, p
;
208 r
= g
= b
= l
; // achromatic
210 hue2rgb = function ( p
, q
, t
) {
218 return p
+ ( q
- p
) * 6 * t
;
224 return p
+ ( q
- p
) * ( 2 / 3 - t
) * 6;
229 q
= l
< 0.5 ? l
* ( 1 + s
) : l
+ s
- l
* s
;
231 r
= hue2rgb( p
, q
, h
+ 1 / 3 );
232 g
= hue2rgb( p
, q
, h
);
233 b
= hue2rgb( p
, q
, h
- 1 / 3 );
236 return [ r
* 255, g
* 255, b
* 255 ];
240 * Get a brighter or darker rgb() value string.
244 * $.colorUtil.getColorBrightness( 'red', +0.1 );
245 * // > "rgb(255,50,50)"
246 * $.colorUtil.getColorBrightness( 'rgb(200,50,50)', -0.2 );
247 * // > "rgb(118,29,29)"
249 * @param {Mixed} currentColor Current value in css
250 * @param {number} mod Wanted brightness modification between -1 and 1
251 * @return {string} Like `'rgb(r,g,b)'`
253 getColorBrightness: function ( currentColor
, mod
) {
254 var rgbArr
= $.colorUtil
.getRGB( currentColor
),
255 hslArr
= $.colorUtil
.rgbToHsl( rgbArr
[ 0 ], rgbArr
[ 1 ], rgbArr
[ 2 ] );
256 rgbArr
= $.colorUtil
.hslToRgb( hslArr
[ 0 ], hslArr
[ 1 ], hslArr
[ 2 ] + mod
);
259 [ parseInt( rgbArr
[ 0 ], 10 ), parseInt( rgbArr
[ 1 ], 10 ), parseInt( rgbArr
[ 2 ], 10 ) ].join( ',' ) +