|
2 | 2 | * jQuery Color Animations |
3 | 3 | * Copyright 2007 John Resig |
4 | 4 | * Released under the MIT and GPL licenses. |
| 5 | + * |
| 6 | + * RGBA support by Mehdi Kabab <http://pioupioum.fr> |
5 | 7 | */ |
6 | 8 |
|
7 | 9 | (function(jQuery){ |
| 10 | + jQuery.extend(jQuery.support, { |
| 11 | + "rgba": supportsRGBA() |
| 12 | + }); |
8 | 13 |
|
9 | 14 | // We override the animation for all of these color styles |
10 | 15 | jQuery.each(['backgroundColor', 'borderBottomColor', 'borderLeftColor', 'borderRightColor', 'borderTopColor', 'color', 'outlineColor'], function(i,attr){ |
11 | 16 | jQuery.fx.step[attr] = function(fx){ |
| 17 | + var tuples = []; |
12 | 18 | if ( !fx.colorInit ) { |
13 | 19 | fx.start = getColor( fx.elem, attr ); |
14 | 20 | fx.end = getRGB( fx.end ); |
| 21 | + fx.alphavalue = { |
| 22 | + start: 4 === fx.start.length, |
| 23 | + end: 4 === fx.end.length |
| 24 | + }; |
| 25 | + if ( !fx.alphavalue.start ) { |
| 26 | + fx.start.push( 1 ); |
| 27 | + } |
| 28 | + if ( !fx.alphavalue.end ) { |
| 29 | + fx.end.push( 1 ); |
| 30 | + } |
| 31 | + if ( jQuery.support.rgba && |
| 32 | + (!fx.alphavalue.start && fx.alphavalue.end) || // RGB => RGBA |
| 33 | + (fx.alphavalue.start && fx.alphavalue.end) || // RGBA => RGBA |
| 34 | + (fx.alphavalue.start && !fx.alphavalue.end) // RGBA => RGB |
| 35 | + ) { |
| 36 | + fx.colorModel = 'rgba'; |
| 37 | + } else { |
| 38 | + fx.colorModel = 'rgb'; |
| 39 | + } |
15 | 40 | fx.colorInit = true; |
16 | 41 | } |
17 | 42 |
|
18 | | - fx.elem.style[attr] = "rgb(" + [ |
19 | | - Math.max(Math.min( parseInt((fx.pos * (fx.end[0] - fx.start[0])) + fx.start[0]), 255), 0), |
20 | | - Math.max(Math.min( parseInt((fx.pos * (fx.end[1] - fx.start[1])) + fx.start[1]), 255), 0), |
21 | | - Math.max(Math.min( parseInt((fx.pos * (fx.end[2] - fx.start[2])) + fx.start[2]), 255), 0) |
22 | | - ].join(",") + ")"; |
23 | | - } |
| 43 | + tuples.push( Math.max(Math.min( parseInt( (fx.pos * (fx.end[0] - fx.start[0])) + fx.start[0]), 255), 0) ); // R |
| 44 | + tuples.push( Math.max(Math.min( parseInt( (fx.pos * (fx.end[1] - fx.start[1])) + fx.start[1]), 255), 0) ); // G |
| 45 | + tuples.push( Math.max(Math.min( parseInt( (fx.pos * (fx.end[2] - fx.start[2])) + fx.start[2]), 255), 0) ); // B |
| 46 | + |
| 47 | + if ( fx.colorModel == 'rgba' ) { |
| 48 | + // Alpha |
| 49 | + tuples.push( Math.max(Math.min( parseFloat((fx.pos * (fx.end[3] - fx.start[3])) + fx.start[3]), 1), 0).toFixed(2) ); |
| 50 | + } |
| 51 | + |
| 52 | + fx.elem.style[attr] = fx.colorModel + "(" + tuples.join(",") + ")"; |
| 53 | + }; |
24 | 54 | }); |
25 | 55 |
|
26 | 56 | // Color Conversion functions from highlightFade |
27 | 57 | // By Blair Mitchelmore |
28 | 58 | // http://jquery.offput.ca/highlightFade/ |
29 | 59 |
|
30 | | - // Parse strings looking for color tuples [255,255,255] |
| 60 | + // Parse strings looking for color tuples [255,255,255[,1]] |
31 | 61 | function getRGB(color) { |
32 | | - var result; |
| 62 | + var result, ret, |
| 63 | + ralpha = '(?:,\\s*((?:1|0)(?:\\.0+)?|(?:0?\\.[0-9]+))\\s*)?\\)', |
| 64 | + rrgbdecimal = new RegExp( 'rgb(a)?\\(\\s*([0-9]{1,3})\\s*,\\s*([0-9]{1,3})\\s*,\\s*([0-9]{1,3})\\s*' + ralpha ), |
| 65 | + rrgbpercent = new RegExp( 'rgb(a)?\\(\\s*([0-9]+(?:\\.[0-9]+)?)\\%\\s*,\\s*([0-9]+(?:\\.[0-9]+)?)\\%\\s*,\\s*([0-9]+(?:\\.[0-9]+)?)\\%\\s*' + ralpha ); |
33 | 66 |
|
34 | 67 | // Check if we're already dealing with an array of colors |
35 | | - if ( color && color.constructor == Array && color.length == 3 ) |
| 68 | + if ( color && color.constructor == Array && color.length >= 3 && color.length <= 4 ) { |
36 | 69 | return color; |
37 | | - |
38 | | - // Look for rgb(num,num,num) |
39 | | - if (result = /rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(color)) |
40 | | - return [parseInt(result[1]), parseInt(result[2]), parseInt(result[3])]; |
41 | | - |
42 | | - // Look for rgb(num%,num%,num%) |
43 | | - if (result = /rgb\(\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*\)/.exec(color)) |
44 | | - return [parseFloat(result[1])*2.55, parseFloat(result[2])*2.55, parseFloat(result[3])*2.55]; |
| 70 | + } |
45 | 71 |
|
46 | 72 | // Look for #a0b1c2 |
47 | | - if (result = /#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/.exec(color)) |
| 73 | + if (result = /#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/.exec(color)) { |
48 | 74 | return [parseInt(result[1],16), parseInt(result[2],16), parseInt(result[3],16)]; |
| 75 | + } |
49 | 76 |
|
50 | 77 | // Look for #fff |
51 | | - if (result = /#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])/.exec(color)) |
| 78 | + if (result = /#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])/.exec(color)) { |
52 | 79 | return [parseInt(result[1]+result[1],16), parseInt(result[2]+result[2],16), parseInt(result[3]+result[3],16)]; |
| 80 | + } |
53 | 81 |
|
54 | | - // Look for rgba(0, 0, 0, 0) == transparent in Safari 3 |
55 | | - if (result = /rgba\(0, 0, 0, 0\)/.exec(color)) |
56 | | - return colors['transparent']; |
| 82 | + // Look for rgb[a](num,num,num[,num]) |
| 83 | + if (result = rrgbdecimal.exec(color)) { |
| 84 | + ret = [parseInt(result[2]), parseInt(result[3]), parseInt(result[4])]; |
| 85 | + // is rgba? |
| 86 | + if (result[1] && result[5]) { |
| 87 | + ret.push(parseFloat(result[5])); |
| 88 | + } |
| 89 | + |
| 90 | + return ret; |
| 91 | + } |
| 92 | + |
| 93 | + // Look for rgb[a](num%,num%,num%[,num]) |
| 94 | + if (result = rrgbpercent.exec(color)) { |
| 95 | + ret = [parseFloat(result[2]) * 2.55, parseFloat(result[3]) * 2.55, parseFloat(result[4]) * 2.55]; |
| 96 | + // is rgba? |
| 97 | + if (result[1] && result[5]) { |
| 98 | + ret.push(parseFloat(result[5])); |
| 99 | + } |
| 100 | + |
| 101 | + return ret; |
| 102 | + } |
57 | 103 |
|
58 | 104 | // Otherwise, we're most likely dealing with a named color |
59 | 105 | return colors[jQuery.trim(color).toLowerCase()]; |
|
75 | 121 | return getRGB(color); |
76 | 122 | }; |
77 | 123 |
|
| 124 | + function supportsRGBA() { |
| 125 | + var $script = jQuery('script:first'), |
| 126 | + color = $script.css('color'), |
| 127 | + result = false; |
| 128 | + if (/^rgba/.test(color)) { |
| 129 | + result = true; |
| 130 | + } else { |
| 131 | + try { |
| 132 | + result = ( color != $script.css('color', 'rgba(0, 0, 0, 0.5)').css('color') ); |
| 133 | + $script.css('color', color); |
| 134 | + } catch (e) {}; |
| 135 | + } |
| 136 | + |
| 137 | + return result; |
| 138 | + }; |
| 139 | + |
78 | 140 | // Some named colors to work with |
79 | 141 | // From Interface by Stefan Petre |
80 | 142 | // http://interface.eyecon.ro/ |
|
123 | 185 | silver:[192,192,192], |
124 | 186 | white:[255,255,255], |
125 | 187 | yellow:[255,255,0], |
126 | | - transparent: [255,255,255] |
| 188 | + transparent: ( jQuery.support.rgba ) ? [0,0,0,0] : [255,255,255] |
127 | 189 | }; |
128 | 190 |
|
129 | 191 | })(jQuery); |
0 commit comments