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