Merge "Remove not used private member variable mParserWarnings from OutputPage"
[mediawiki.git] / resources / src / jquery / jquery.colorUtil.js
blobc14f2c861c99d8e6691b09bca9b4322deb777404
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 = {
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                         /*jshint boss:true */
27                         var result;
29                         // Check if we're already dealing with an array of colors
30                         if ( color && $.isArray( color ) && color.length === 3 ) {
31                                 return color;
32                         }
34                         // Look for rgb(num,num,num)
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                         }
43                         // Look for rgb(num%,num%,num%)
44                         if ( result = /rgb\(\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*\)/.exec( color ) ) {
45                                 return [
46                                         parseFloat( result[ 1 ] ) * 2.55,
47                                         parseFloat( result[ 2 ] ) * 2.55,
48                                         parseFloat( result[ 3 ] ) * 2.55
49                                 ];
50                         }
52                         // Look for #a0b1c2
53                         if ( result = /#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/.exec( color ) ) {
54                                 return [
55                                         parseInt( result[ 1 ], 16 ),
56                                         parseInt( result[ 2 ], 16 ),
57                                         parseInt( result[ 3 ], 16 )
58                                 ];
59                         }
61                         // Look for #fff
62                         if ( result = /#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])/.exec( color ) ) {
63                                 return [
64                                         parseInt( result[ 1 ] + result[ 1 ], 16 ),
65                                         parseInt( result[ 2 ] + result[ 2 ], 16 ),
66                                         parseInt( result[ 3 ] + result[ 3 ], 16 )
67                                 ];
68                         }
70                         // Look for rgba(0, 0, 0, 0) == transparent in Safari 3
71                         if ( result = /rgba\(0, 0, 0, 0\)/.exec( color ) ) {
72                                 return $.colorUtil.colors.transparent;
73                         }
75                         // Otherwise, we're most likely dealing with a named color
76                         return $.colorUtil.colors[ $.trim( color ).toLowerCase() ];
77                 },
79                 /**
80                  * Named color map
81                  *
82                  * Based on Interface by Stefan Petre
83                  * <http://interface.eyecon.ro/>
84                  *
85                  * @property {Object}
86                  */
87                 colors: {
88                         aqua: [ 0, 255, 255 ],
89                         azure: [ 240, 255, 255 ],
90                         beige: [ 245, 245, 220 ],
91                         black: [ 0, 0, 0 ],
92                         blue: [ 0, 0, 255 ],
93                         brown: [ 165, 42, 42 ],
94                         cyan: [ 0, 255, 255 ],
95                         darkblue: [ 0, 0, 139 ],
96                         darkcyan: [ 0, 139, 139 ],
97                         darkgrey: [ 169, 169, 169 ],
98                         darkgreen: [ 0, 100, 0 ],
99                         darkkhaki: [ 189, 183, 107 ],
100                         darkmagenta: [ 139, 0, 139 ],
101                         darkolivegreen: [ 85, 107, 47 ],
102                         darkorange: [ 255, 140, 0 ],
103                         darkorchid: [ 153, 50, 204 ],
104                         darkred: [ 139, 0, 0 ],
105                         darksalmon: [ 233, 150, 122 ],
106                         darkviolet: [ 148, 0, 211 ],
107                         fuchsia: [ 255, 0, 255 ],
108                         gold: [ 255, 215, 0 ],
109                         green: [ 0, 128, 0 ],
110                         indigo: [ 75, 0, 130 ],
111                         khaki: [ 240, 230, 140 ],
112                         lightblue: [ 173, 216, 230 ],
113                         lightcyan: [ 224, 255, 255 ],
114                         lightgreen: [ 144, 238, 144 ],
115                         lightgrey: [ 211, 211, 211 ],
116                         lightpink: [ 255, 182, 193 ],
117                         lightyellow: [ 255, 255, 224 ],
118                         lime: [ 0, 255, 0 ],
119                         magenta: [ 255, 0, 255 ],
120                         maroon: [ 128, 0, 0 ],
121                         navy: [ 0, 0, 128 ],
122                         olive: [ 128, 128, 0 ],
123                         orange: [ 255, 165, 0 ],
124                         pink: [ 255, 192, 203 ],
125                         purple: [ 128, 0, 128 ],
126                         violet: [ 128, 0, 128 ],
127                         red: [ 255, 0, 0 ],
128                         silver: [ 192, 192, 192 ],
129                         white: [ 255, 255, 255 ],
130                         yellow: [ 255, 255, 0 ],
131                         transparent: [ 255, 255, 255 ]
132                 },
134                 /**
135                  * Convert an RGB color value to HSL.
136                  *
137                  * Conversion formula based on
138                  * <http://mjijackson.com/2008/02/rgb-to-hsl-and-rgb-to-hsv-color-model-conversion-algorithms-in-javascript>
139                  *
140                  * Adapted from <https://en.wikipedia.org/wiki/HSL_color_space>.
141                  *
142                  * Assumes `r`, `g`, and `b` are contained in the set `[0, 255]` and
143                  * returns `h`, `s`, and `l` in the set `[0, 1]`.
144                  *
145                  * @param {number} r The red color value
146                  * @param {number} g The green color value
147                  * @param {number} b The blue color value
148                  * @return {number[]} The HSL representation
149                  */
150                 rgbToHsl: function ( r, g, b ) {
151                         r = r / 255;
152                         g = g / 255;
153                         b = b / 255;
155                         var d,
156                                 max = Math.max( r, g, b ),
157                                 min = Math.min( r, g, b ),
158                                 h,
159                                 s,
160                                 l = ( max + min ) / 2;
162                         if ( max === min ) {
163                                 // achromatic
164                                 h = s = 0;
165                         } else {
166                                 d = max - min;
167                                 s = l > 0.5 ? d / ( 2 - max - min ) : d / ( max + min );
168                                 switch ( max ) {
169                                         case r:
170                                                 h = ( g - b ) / d + ( g < b ? 6 : 0 );
171                                                 break;
172                                         case g:
173                                                 h = ( b - r ) / d + 2;
174                                                 break;
175                                         case b:
176                                                 h = ( r - g ) / d + 4;
177                                                 break;
178                                 }
179                                 h /= 6;
180                         }
182                         return [ h, s, l ];
183                 },
185                 /**
186                  * Convert an HSL color value to RGB.
187                  *
188                  * Conversion formula based on
189                  * <http://mjijackson.com/2008/02/rgb-to-hsl-and-rgb-to-hsv-color-model-conversion-algorithms-in-javascript>
190                  *
191                  * Adapted from <https://en.wikipedia.org/wiki/HSL_color_space>.
192                  *
193                  * Assumes `h`, `s`, and `l` are contained in the set `[0, 1]` and
194                  * returns `r`, `g`, and `b` in the set `[0, 255]`.
195                  *
196                  * @param {number} h The hue
197                  * @param {number} s The saturation
198                  * @param {number} l The lightness
199                  * @return {number[]} The RGB representation
200                  */
201                 hslToRgb: function ( h, s, l ) {
202                         var r, g, b, hue2rgb, q, p;
204                         if ( s === 0 ) {
205                                 r = g = b = l; // achromatic
206                         } else {
207                                 hue2rgb = function ( p, q, t ) {
208                                         if ( t < 0 ) {
209                                                 t += 1;
210                                         }
211                                         if ( t > 1 ) {
212                                                 t -= 1;
213                                         }
214                                         if ( t < 1 / 6 ) {
215                                                 return p + ( q - p ) * 6 * t;
216                                         }
217                                         if ( t < 1 / 2 ) {
218                                                 return q;
219                                         }
220                                         if ( t < 2 / 3 ) {
221                                                 return p + ( q - p ) * ( 2 / 3 - t ) * 6;
222                                         }
223                                         return p;
224                                 };
226                                 q = l < 0.5 ? l * ( 1 + s ) : l + s - l * s;
227                                 p = 2 * l - q;
228                                 r = hue2rgb( p, q, h + 1 / 3 );
229                                 g = hue2rgb( p, q, h );
230                                 b = hue2rgb( p, q, h - 1 / 3 );
231                         }
233                         return [ r * 255, g * 255, b * 255 ];
234                 },
236                 /**
237                  * Get a brighter or darker rgb() value string.
238                  *
239                  * Usage:
240                  *
241                  *     $.colorUtil.getColorBrightness( 'red', +0.1 );
242                  *     // > "rgb(255,50,50)"
243                  *     $.colorUtil.getColorBrightness( 'rgb(200,50,50)', -0.2 );
244                  *     // > "rgb(118,29,29)"
245                  *
246                  * @param {Mixed} currentColor Current value in css
247                  * @param {number} mod Wanted brightness modification between -1 and 1
248                  * @return {string} Like `'rgb(r,g,b)'`
249                  */
250                 getColorBrightness: function ( currentColor, mod ) {
251                         var rgbArr = $.colorUtil.getRGB( currentColor ),
252                                 hslArr = $.colorUtil.rgbToHsl( rgbArr[ 0 ], rgbArr[ 1 ], rgbArr[ 2 ] );
253                         rgbArr = $.colorUtil.hslToRgb( hslArr[ 0 ], hslArr[ 1 ], hslArr[ 2 ] + mod );
255                         return 'rgb(' +
256                                 [ parseInt( rgbArr[ 0 ], 10 ), parseInt( rgbArr[ 1 ], 10 ), parseInt( rgbArr[ 2 ], 10 ) ].join( ',' ) +
257                                 ')';
258                 }
260         };
262 }( jQuery ) );