Skip to content

Commit 92ee455

Browse files
committed
Added RGBA support.
See demo: http://pioupioum.fr/sandbox/jquery-color/
1 parent a17222c commit 92ee455

File tree

1 file changed

+85
-23
lines changed

1 file changed

+85
-23
lines changed

jquery.color.js

Lines changed: 85 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,58 +2,104 @@
22
* jQuery Color Animations
33
* Copyright 2007 John Resig
44
* Released under the MIT and GPL licenses.
5+
*
6+
* RGBA support by Mehdi Kabab <http://pioupioum.fr>
57
*/
68

79
(function(jQuery){
10+
jQuery.extend(jQuery.support, {
11+
"rgba": supportsRGBA()
12+
});
813

914
// We override the animation for all of these color styles
1015
jQuery.each(['backgroundColor', 'borderBottomColor', 'borderLeftColor', 'borderRightColor', 'borderTopColor', 'color', 'outlineColor'], function(i,attr){
1116
jQuery.fx.step[attr] = function(fx){
17+
var tuples = [];
1218
if ( !fx.colorInit ) {
1319
fx.start = getColor( fx.elem, attr );
1420
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+
}
1540
fx.colorInit = true;
1641
}
1742

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+
};
2454
});
2555

2656
// Color Conversion functions from highlightFade
2757
// By Blair Mitchelmore
2858
// http://jquery.offput.ca/highlightFade/
2959

30-
// Parse strings looking for color tuples [255,255,255]
60+
// Parse strings looking for color tuples [255,255,255[,1]]
3161
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 );
3366

3467
// 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 ) {
3669
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+
}
4571

4672
// 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)) {
4874
return [parseInt(result[1],16), parseInt(result[2],16), parseInt(result[3],16)];
75+
}
4976

5077
// 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)) {
5279
return [parseInt(result[1]+result[1],16), parseInt(result[2]+result[2],16), parseInt(result[3]+result[3],16)];
80+
}
5381

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+
}
57103

58104
// Otherwise, we're most likely dealing with a named color
59105
return colors[jQuery.trim(color).toLowerCase()];
@@ -75,6 +121,22 @@
75121
return getRGB(color);
76122
};
77123

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+
78140
// Some named colors to work with
79141
// From Interface by Stefan Petre
80142
// http://interface.eyecon.ro/
@@ -123,7 +185,7 @@
123185
silver:[192,192,192],
124186
white:[255,255,255],
125187
yellow:[255,255,0],
126-
transparent: [255,255,255]
188+
transparent: ( jQuery.support.rgba ) ? [0,0,0,0] : [255,255,255]
127189
};
128190

129191
})(jQuery);

0 commit comments

Comments
 (0)