Merge "Add ss_active_users in SiteStats::isSane"
[mediawiki.git] / resources / jquery / jquery.colorUtil.js
blob9c6f9ecbe1ba6c4c620bf487329b1950295e019b
1 /**
2  * jQuery Color Utilities
3  * Written by Krinkle in 2011
4  * Released under the MIT and GPL licenses.
5  * Mostly based on other plugins and functions (linted and optimized a little).
6  * Sources cited inline.
7  */
8 ( function ( $ ) {
9         $.colorUtil = {
11                 // Color Conversion function from highlightFade
12                 // By Blair Mitchelmore
13                 // http://jquery.offput.ca/highlightFade/
14                 // Parse strings looking for color tuples [255,255,255]
15                 getRGB : function ( color ) {
16                         /*jshint boss:true */
17                         var result;
19                         // Check if we're already dealing with an array of colors
20                         if ( color && $.isArray( color ) && color.length === 3 ) {
21                                 return color;
22                         }
24                         // Look for rgb(num,num,num)
25                         if (result = /rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(color)) {
26                                 return [parseInt(result[1],10), parseInt(result[2],10), parseInt(result[3],10)];
27                         }
29                         // Look for rgb(num%,num%,num%)
30                         if (result = /rgb\(\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*\)/.exec(color)) {
31                                 return [parseFloat(result[1],10)*2.55, parseFloat(result[2],10)*2.55, parseFloat(result[3])*2.55];
32                         }
34                         // Look for #a0b1c2
35                         if (result = /#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/.exec(color)) {
36                                 return [parseInt(result[1],16), parseInt(result[2],16), parseInt(result[3],16)];
37                         }
39                         // Look for #fff
40                         if (result = /#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])/.exec(color)) {
41                                 return [parseInt(result[1]+result[1],16), parseInt(result[2]+result[2],16), parseInt(result[3]+result[3],16)];
42                         }
44                         // Look for rgba(0, 0, 0, 0) == transparent in Safari 3
45                         if (result = /rgba\(0, 0, 0, 0\)/.exec(color)) {
46                                 return $.colorUtil.colors.transparent;
47                         }
49                         // Otherwise, we're most likely dealing with a named color
50                         return $.colorUtil.colors[$.trim(color).toLowerCase()];
51                 },
53                 // Some named colors to work with
54                 // From Interface by Stefan Petre
55                 // http://interface.eyecon.ro/
56                 colors: {
57                         aqua: [0,255,255],
58                         azure: [240,255,255],
59                         beige: [245,245,220],
60                         black: [0,0,0],
61                         blue: [0,0,255],
62                         brown: [165,42,42],
63                         cyan: [0,255,255],
64                         darkblue: [0,0,139],
65                         darkcyan: [0,139,139],
66                         darkgrey: [169,169,169],
67                         darkgreen: [0,100,0],
68                         darkkhaki: [189,183,107],
69                         darkmagenta: [139,0,139],
70                         darkolivegreen: [85,107,47],
71                         darkorange: [255,140,0],
72                         darkorchid: [153,50,204],
73                         darkred: [139,0,0],
74                         darksalmon: [233,150,122],
75                         darkviolet: [148,0,211],
76                         fuchsia: [255,0,255],
77                         gold: [255,215,0],
78                         green: [0,128,0],
79                         indigo: [75,0,130],
80                         khaki: [240,230,140],
81                         lightblue: [173,216,230],
82                         lightcyan: [224,255,255],
83                         lightgreen: [144,238,144],
84                         lightgrey: [211,211,211],
85                         lightpink: [255,182,193],
86                         lightyellow: [255,255,224],
87                         lime: [0,255,0],
88                         magenta: [255,0,255],
89                         maroon: [128,0,0],
90                         navy: [0,0,128],
91                         olive: [128,128,0],
92                         orange: [255,165,0],
93                         pink: [255,192,203],
94                         purple: [128,0,128],
95                         violet: [128,0,128],
96                         red: [255,0,0],
97                         silver: [192,192,192],
98                         white: [255,255,255],
99                         yellow: [255,255,0],
100                         transparent: [255,255,255]
101                 },
103                 /**
104                  * http://mjijackson.com/2008/02/rgb-to-hsl-and-rgb-to-hsv-color-model-conversion-algorithms-in-javascript
105                  * Converts an RGB color value to HSL. Conversion formula
106                  * adapted from http://en.wikipedia.org/wiki/HSL_color_space.
107                  * Assumes r, g, and b are contained in the set [0, 255] and
108                  * returns h, s, and l in the set [0, 1].
109                  *
110                  * @param       Number  R               The red color value
111                  * @param       Number  G               The green color value
112                  * @param       Number  B               The blue color value
113                  * @return      Array                   The HSL representation
114                  */
115                 rgbToHsl: function ( R, G, B ) {
116                         var d,
117                                 r = R / 255,
118                                 g = G / 255,
119                                 b = B / 255,
120                                 max = Math.max( r, g, b ), min = Math.min( r, g, b ),
121                                 h,
122                                 s,
123                                 l = (max + min) / 2;
125                         if ( max === min ) {
126                                 // achromatic
127                                 h = s = 0;
128                         } else {
129                                 d = max - min;
130                                 s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
131                                 switch ( max ) {
132                                         case r:
133                                                 h = (g - b) / d + (g < b ? 6 : 0);
134                                                 break;
135                                         case g:
136                                                 h = (b - r) / d + 2;
137                                                 break;
138                                         case b:
139                                                 h = (r - g) / d + 4;
140                                                 break;
141                                 }
142                                 h /= 6;
143                         }
145                         return [h, s, l];
146                 },
148                 /**
149                  * http://mjijackson.com/2008/02/rgb-to-hsl-and-rgb-to-hsv-color-model-conversion-algorithms-in-javascript
150                  * Converts an HSL color value to RGB. Conversion formula
151                  * adapted from http://en.wikipedia.org/wiki/HSL_color_space.
152                  * Assumes h, s, and l are contained in the set [0, 1] and
153                  * returns r, g, and b in the set [0, 255].
154                  *
155                  * @param       Number  h               The hue
156                  * @param       Number  s               The saturation
157                  * @param       Number  l               The lightness
158                  * @return      Array                   The RGB representation
159                  */
160                 hslToRgb: function ( h, s, l ) {
161                         var r, g, b, hue2rgb, q, p;
163                         if ( s === 0 ) {
164                                 r = g = b = l; // achromatic
165                         } else {
166                                 hue2rgb = function ( p, q, t ) {
167                                         if ( t < 0 ) {
168                                                 t += 1;
169                                         }
170                                         if ( t > 1 ) {
171                                                 t -= 1;
172                                         }
173                                         if ( t < 1/6 ) {
174                                                 return p + (q - p) * 6 * t;
175                                         }
176                                         if ( t < 1/2 ) {
177                                                 return q;
178                                         }
179                                         if ( t < 2/3 ) {
180                                                 return p + (q - p) * (2/3 - t) * 6;
181                                         }
182                                         return p;
183                                 };
185                                 q = l < 0.5 ? l * (1 + s) : l + s - l * s;
186                                 p = 2 * l - q;
187                                 r = hue2rgb( p, q, h + 1/3 );
188                                 g = hue2rgb( p, q, h );
189                                 b = hue2rgb( p, q, h - 1/3 );
190                         }
192                         return [r * 255, g * 255, b * 255];
193                 },
195                 /**
196                  * Get's a brighter or darker rgb() value string.
197                  *
198                  * @author Krinkle
199                  *
200                  * @example     getCSSColorMod( 'red', +0.1 )
201                  * @example     getCSSColorMod( 'rgb(200,50,50)', -0.2 )
202                  *
203                  * @param       Mixed   currentColor current value in css
204                  * @param       Number  mod wanted brightness modification between -1 and 1
205                  * @return      String 'rgb(r,g,b)'
206                  */
207                 getColorBrightness: function ( currentColor, mod ) {
208                         var rgbArr = $.colorUtil.getRGB( currentColor ),
209                                 hslArr = $.colorUtil.rgbToHsl(rgbArr[0], rgbArr[1], rgbArr[2] );
210                         rgbArr = $.colorUtil.hslToRgb(hslArr[0], hslArr[1], hslArr[2]+mod);
212                         return 'rgb(' +
213                                 [parseInt( rgbArr[0], 10), parseInt( rgbArr[1], 10 ), parseInt( rgbArr[2], 10 )].join( ',' ) +
214                                 ')';
215                 }
217         };
219 }( jQuery ) );