]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - resources/src/jquery/jquery.colorUtil.js
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.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
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 ) ) {
36                                 return [
37                                         parseInt( result[ 1 ], 10 ),
38                                         parseInt( result[ 2 ], 10 ),
39                                         parseInt( result[ 3 ], 10 )
40                                 ];
41                         }
42
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 ) ) {
46                                 return [
47                                         parseFloat( result[ 1 ] ) * 2.55,
48                                         parseFloat( result[ 2 ] ) * 2.55,
49                                         parseFloat( result[ 3 ] ) * 2.55
50                                 ];
51                         }
52
53                         // Look for #a0b1c2
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 ) ) {
56                                 return [
57                                         parseInt( result[ 1 ], 16 ),
58                                         parseInt( result[ 2 ], 16 ),
59                                         parseInt( result[ 3 ], 16 )
60                                 ];
61                         }
62
63                         // Look for #fff
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 ) ) {
66                                 return [
67                                         parseInt( result[ 1 ] + result[ 1 ], 16 ),
68                                         parseInt( result[ 2 ] + result[ 2 ], 16 ),
69                                         parseInt( result[ 3 ] + result[ 3 ], 16 )
70                                 ];
71                         }
72
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;
77                         }
78
79                         // Otherwise, we're most likely dealing with a named color
80                         return $.colorUtil.colors[ $.trim( color ).toLowerCase() ];
81                 },
82
83                 /**
84                  * Named color map
85                  *
86                  * Based on Interface by Stefan Petre
87                  * <http://interface.eyecon.ro/>
88                  *
89                  * @property {Object}
90                  */
91                 colors: {
92                         aqua: [ 0, 255, 255 ],
93                         azure: [ 240, 255, 255 ],
94                         beige: [ 245, 245, 220 ],
95                         black: [ 0, 0, 0 ],
96                         blue: [ 0, 0, 255 ],
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 ],
122                         lime: [ 0, 255, 0 ],
123                         magenta: [ 255, 0, 255 ],
124                         maroon: [ 128, 0, 0 ],
125                         navy: [ 0, 0, 128 ],
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 ],
131                         red: [ 255, 0, 0 ],
132                         silver: [ 192, 192, 192 ],
133                         white: [ 255, 255, 255 ],
134                         yellow: [ 255, 255, 0 ],
135                         transparent: [ 255, 255, 255 ]
136                 },
137
138                 /**
139                  * Convert an RGB color value to HSL.
140                  *
141                  * Conversion formula based on
142                  * <http://mjijackson.com/2008/02/rgb-to-hsl-and-rgb-to-hsv-color-model-conversion-algorithms-in-javascript>
143                  *
144                  * Adapted from <https://en.wikipedia.org/wiki/HSL_color_space>.
145                  *
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]`.
148                  *
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
153                  */
154                 rgbToHsl: function ( r, g, b ) {
155                         var d, h, s, l, min, max;
156
157                         r = r / 255;
158                         g = g / 255;
159                         b = b / 255;
160
161                         max = Math.max( r, g, b );
162                         min = Math.min( r, g, b );
163                         l = ( max + min ) / 2;
164
165                         if ( max === min ) {
166                                 // achromatic
167                                 h = s = 0;
168                         } else {
169                                 d = max - min;
170                                 s = l > 0.5 ? d / ( 2 - max - min ) : d / ( max + min );
171                                 switch ( max ) {
172                                         case r:
173                                                 h = ( g - b ) / d + ( g < b ? 6 : 0 );
174                                                 break;
175                                         case g:
176                                                 h = ( b - r ) / d + 2;
177                                                 break;
178                                         case b:
179                                                 h = ( r - g ) / d + 4;
180                                                 break;
181                                 }
182                                 h /= 6;
183                         }
184
185                         return [ h, s, l ];
186                 },
187
188                 /**
189                  * Convert an HSL color value to RGB.
190                  *
191                  * Conversion formula based on
192                  * <http://mjijackson.com/2008/02/rgb-to-hsl-and-rgb-to-hsv-color-model-conversion-algorithms-in-javascript>
193                  *
194                  * Adapted from <https://en.wikipedia.org/wiki/HSL_color_space>.
195                  *
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]`.
198                  *
199                  * @param {number} h The hue
200                  * @param {number} s The saturation
201                  * @param {number} l The lightness
202                  * @return {number[]} The RGB representation
203                  */
204                 hslToRgb: function ( h, s, l ) {
205                         var r, g, b, hue2rgb, q, p;
206
207                         if ( s === 0 ) {
208                                 r = g = b = l; // achromatic
209                         } else {
210                                 hue2rgb = function ( p, q, t ) {
211                                         if ( t < 0 ) {
212                                                 t += 1;
213                                         }
214                                         if ( t > 1 ) {
215                                                 t -= 1;
216                                         }
217                                         if ( t < 1 / 6 ) {
218                                                 return p + ( q - p ) * 6 * t;
219                                         }
220                                         if ( t < 1 / 2 ) {
221                                                 return q;
222                                         }
223                                         if ( t < 2 / 3 ) {
224                                                 return p + ( q - p ) * ( 2 / 3 - t ) * 6;
225                                         }
226                                         return p;
227                                 };
228
229                                 q = l < 0.5 ? l * ( 1 + s ) : l + s - l * s;
230                                 p = 2 * l - q;
231                                 r = hue2rgb( p, q, h + 1 / 3 );
232                                 g = hue2rgb( p, q, h );
233                                 b = hue2rgb( p, q, h - 1 / 3 );
234                         }
235
236                         return [ r * 255, g * 255, b * 255 ];
237                 },
238
239                 /**
240                  * Get a brighter or darker rgb() value string.
241                  *
242                  * Usage:
243                  *
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)"
248                  *
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)'`
252                  */
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 );
257
258                         return 'rgb(' +
259                                 [ parseInt( rgbArr[ 0 ], 10 ), parseInt( rgbArr[ 1 ], 10 ), parseInt( rgbArr[ 2 ], 10 ) ].join( ',' ) +
260                                 ')';
261                 }
262
263         };
264
265 }( jQuery ) );