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