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