Merge "selenium: invoke jobs to enforce eventual consistency"
[lhc/web/wiklou.git] / resources / src / jquery / jquery.colorUtil.js
1 /*!
2 * jQuery Color Utilities
3 *
4 * Released under the MIT and GPL licenses.
5 *
6 * Mostly based on other plugins and functions (linted and optimized a little).
7 * Sources cited inline.
8 */
9 ( function () {
10 /**
11 * @class jQuery.colorUtil
12 * @singleton
13 */
14 $.colorUtil = {
15
16 /**
17 * Parse CSS color strings looking for color tuples
18 *
19 * Based on highlightFade by Blair Mitchelmore
20 * <http://jquery.offput.ca/highlightFade/>
21 *
22 * @param {Array|string} color
23 * @return {Array}
24 */
25 getRGB: function ( color ) {
26 var result;
27
28 // Check if we're already dealing with an array of colors
29 if ( color && Array.isArray( color ) && color.length === 3 ) {
30 return color;
31 }
32 if ( typeof color !== 'string' ) {
33 return undefined;
34 }
35
36 // Look for rgb(num,num,num)
37 // eslint-disable-next-line no-cond-assign
38 if ( result = /rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec( color ) ) {
39 return [
40 parseInt( result[ 1 ], 10 ),
41 parseInt( result[ 2 ], 10 ),
42 parseInt( result[ 3 ], 10 )
43 ];
44 }
45
46 // Look for rgb(num%,num%,num%)
47 // eslint-disable-next-line no-cond-assign
48 if ( result = /rgb\(\s*([0-9]+(?:\.[0-9]+)?)%\s*,\s*([0-9]+(?:\.[0-9]+)?)%\s*,\s*([0-9]+(?:\.[0-9]+)?)%\s*\)/.exec( color ) ) {
49 return [
50 parseFloat( result[ 1 ] ) * 2.55,
51 parseFloat( result[ 2 ] ) * 2.55,
52 parseFloat( result[ 3 ] ) * 2.55
53 ];
54 }
55
56 // Look for #a0b1c2
57 // eslint-disable-next-line no-cond-assign
58 if ( result = /#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/.exec( color ) ) {
59 return [
60 parseInt( result[ 1 ], 16 ),
61 parseInt( result[ 2 ], 16 ),
62 parseInt( result[ 3 ], 16 )
63 ];
64 }
65
66 // Look for #fff
67 // eslint-disable-next-line no-cond-assign
68 if ( result = /#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])/.exec( color ) ) {
69 return [
70 parseInt( result[ 1 ] + result[ 1 ], 16 ),
71 parseInt( result[ 2 ] + result[ 2 ], 16 ),
72 parseInt( result[ 3 ] + result[ 3 ], 16 )
73 ];
74 }
75
76 // Look for rgba(0, 0, 0, 0) == transparent in Safari 3
77 // eslint-disable-next-line no-cond-assign
78 if ( result = /rgba\(0, 0, 0, 0\)/.exec( color ) ) {
79 return $.colorUtil.colors.transparent;
80 }
81
82 // Otherwise, we're most likely dealing with a named color
83 return $.colorUtil.colors[ color.trim().toLowerCase() ];
84 },
85
86 /**
87 * Named color map
88 *
89 * Based on Interface by Stefan Petre
90 * <http://interface.eyecon.ro/>
91 *
92 * @property {Object}
93 */
94 colors: {
95 aqua: [ 0, 255, 255 ],
96 azure: [ 240, 255, 255 ],
97 beige: [ 245, 245, 220 ],
98 black: [ 0, 0, 0 ],
99 blue: [ 0, 0, 255 ],
100 brown: [ 165, 42, 42 ],
101 cyan: [ 0, 255, 255 ],
102 darkblue: [ 0, 0, 139 ],
103 darkcyan: [ 0, 139, 139 ],
104 darkgrey: [ 169, 169, 169 ],
105 darkgreen: [ 0, 100, 0 ],
106 darkkhaki: [ 189, 183, 107 ],
107 darkmagenta: [ 139, 0, 139 ],
108 darkolivegreen: [ 85, 107, 47 ],
109 darkorange: [ 255, 140, 0 ],
110 darkorchid: [ 153, 50, 204 ],
111 darkred: [ 139, 0, 0 ],
112 darksalmon: [ 233, 150, 122 ],
113 darkviolet: [ 148, 0, 211 ],
114 fuchsia: [ 255, 0, 255 ],
115 gold: [ 255, 215, 0 ],
116 green: [ 0, 128, 0 ],
117 indigo: [ 75, 0, 130 ],
118 khaki: [ 240, 230, 140 ],
119 lightblue: [ 173, 216, 230 ],
120 lightcyan: [ 224, 255, 255 ],
121 lightgreen: [ 144, 238, 144 ],
122 lightgrey: [ 211, 211, 211 ],
123 lightpink: [ 255, 182, 193 ],
124 lightyellow: [ 255, 255, 224 ],
125 lime: [ 0, 255, 0 ],
126 magenta: [ 255, 0, 255 ],
127 maroon: [ 128, 0, 0 ],
128 navy: [ 0, 0, 128 ],
129 olive: [ 128, 128, 0 ],
130 orange: [ 255, 165, 0 ],
131 pink: [ 255, 192, 203 ],
132 purple: [ 128, 0, 128 ],
133 violet: [ 128, 0, 128 ],
134 red: [ 255, 0, 0 ],
135 silver: [ 192, 192, 192 ],
136 white: [ 255, 255, 255 ],
137 yellow: [ 255, 255, 0 ],
138 transparent: [ 255, 255, 255 ]
139 },
140
141 /**
142 * Convert an RGB color value to HSL.
143 *
144 * Conversion formula based on
145 * <http://mjijackson.com/2008/02/rgb-to-hsl-and-rgb-to-hsv-color-model-conversion-algorithms-in-javascript>
146 *
147 * Adapted from <https://en.wikipedia.org/wiki/HSL_color_space>.
148 *
149 * Assumes `r`, `g`, and `b` are contained in the set `[0, 255]` and
150 * returns `h`, `s`, and `l` in the set `[0, 1]`.
151 *
152 * @param {number} r The red color value
153 * @param {number} g The green color value
154 * @param {number} b The blue color value
155 * @return {number[]} The HSL representation
156 */
157 rgbToHsl: function ( r, g, b ) {
158 var d, h, s, l, min, max;
159
160 r = r / 255;
161 g = g / 255;
162 b = b / 255;
163
164 max = Math.max( r, g, b );
165 min = Math.min( r, g, b );
166 l = ( max + min ) / 2;
167
168 if ( max === min ) {
169 // achromatic
170 h = s = 0;
171 } else {
172 d = max - min;
173 s = l > 0.5 ? d / ( 2 - max - min ) : d / ( max + min );
174 switch ( max ) {
175 case r:
176 h = ( g - b ) / d + ( g < b ? 6 : 0 );
177 break;
178 case g:
179 h = ( b - r ) / d + 2;
180 break;
181 case b:
182 h = ( r - g ) / d + 4;
183 break;
184 }
185 h /= 6;
186 }
187
188 return [ h, s, l ];
189 },
190
191 /**
192 * Convert an HSL color value to RGB.
193 *
194 * Conversion formula based on
195 * <http://mjijackson.com/2008/02/rgb-to-hsl-and-rgb-to-hsv-color-model-conversion-algorithms-in-javascript>
196 *
197 * Adapted from <https://en.wikipedia.org/wiki/HSL_color_space>.
198 *
199 * Assumes `h`, `s`, and `l` are contained in the set `[0, 1]` and
200 * returns `r`, `g`, and `b` in the set `[0, 255]`.
201 *
202 * @param {number} h The hue
203 * @param {number} s The saturation
204 * @param {number} l The lightness
205 * @return {number[]} The RGB representation
206 */
207 hslToRgb: function ( h, s, l ) {
208 var r, g, b, hue2rgb, q, p;
209
210 if ( s === 0 ) {
211 r = g = b = l; // achromatic
212 } else {
213 hue2rgb = function ( p, q, t ) {
214 if ( t < 0 ) {
215 t += 1;
216 }
217 if ( t > 1 ) {
218 t -= 1;
219 }
220 if ( t < 1 / 6 ) {
221 return p + ( q - p ) * 6 * t;
222 }
223 if ( t < 1 / 2 ) {
224 return q;
225 }
226 if ( t < 2 / 3 ) {
227 return p + ( q - p ) * ( 2 / 3 - t ) * 6;
228 }
229 return p;
230 };
231
232 q = l < 0.5 ? l * ( 1 + s ) : l + s - l * s;
233 p = 2 * l - q;
234 r = hue2rgb( p, q, h + 1 / 3 );
235 g = hue2rgb( p, q, h );
236 b = hue2rgb( p, q, h - 1 / 3 );
237 }
238
239 return [ r * 255, g * 255, b * 255 ];
240 },
241
242 /**
243 * Get a brighter or darker rgb() value string.
244 *
245 * Usage:
246 *
247 * $.colorUtil.getColorBrightness( 'red', +0.1 );
248 * // > "rgb(255,50,50)"
249 * $.colorUtil.getColorBrightness( 'rgb(200,50,50)', -0.2 );
250 * // > "rgb(118,29,29)"
251 *
252 * @param {Mixed} currentColor Current value in css
253 * @param {number} mod Wanted brightness modification between -1 and 1
254 * @return {string} Like `'rgb(r,g,b)'`
255 */
256 getColorBrightness: function ( currentColor, mod ) {
257 var rgbArr = $.colorUtil.getRGB( currentColor ),
258 hslArr = $.colorUtil.rgbToHsl( rgbArr[ 0 ], rgbArr[ 1 ], rgbArr[ 2 ] );
259 rgbArr = $.colorUtil.hslToRgb( hslArr[ 0 ], hslArr[ 1 ], hslArr[ 2 ] + mod );
260
261 return 'rgb(' +
262 [ parseInt( rgbArr[ 0 ], 10 ), parseInt( rgbArr[ 1 ], 10 ), parseInt( rgbArr[ 2 ], 10 ) ].join( ',' ) +
263 ')';
264 }
265
266 };
267
268 }() );