Bug Tracker

Ticket #2281: rgba.diff

File rgba.diff, 3.4 KB (added by mdawaffe, 4 years ago)

RGBA support for jquery.color

  • jquery.color.js

     
    1212            if ( fx.state == 0 ) { 
    1313                fx.start = getColor( fx.elem, attr ); 
    1414                fx.end = getRGB( fx.end ); 
     15                if ( fx.start[3] || fx.end[3] ) { // if one is rgba, make sure both are 
     16                    fx.start[3] = fx.start[3] || 1; 
     17                    fx.end[3] = fx.end[3] || 1; 
     18                } 
    1519            } 
    1620 
    17             fx.elem.style[attr] = "rgb(" + [ 
    18                 Math.max(Math.min( parseInt((fx.pos * (fx.end[0] - fx.start[0])) + fx.start[0]), 255), 0), 
    19                 Math.max(Math.min( parseInt((fx.pos * (fx.end[1] - fx.start[1])) + fx.start[1]), 255), 0), 
    20                 Math.max(Math.min( parseInt((fx.pos * (fx.end[2] - fx.start[2])) + fx.start[2]), 255), 0) 
    21             ].join(",") + ")"; 
     21            fx.elem.style[attr] = ( fx.end[3] ? 'rgba(' : 'rgb(' ) + 
     22                Math.max(Math.min( parseInt((fx.pos * (fx.end[0] - fx.start[0])) + fx.start[0]), 255), 0) + ',' + 
     23                Math.max(Math.min( parseInt((fx.pos * (fx.end[1] - fx.start[1])) + fx.start[1]), 255), 0) + ',' + 
     24                Math.max(Math.min( parseInt((fx.pos * (fx.end[2] - fx.start[2])) + fx.start[2]), 255), 0) + 
     25                ( fx.end[3] ? ',' + 
     26                Math.max(Math.min( parseFloat((fx.pos * (fx.end[3] - fx.start[3])) + fx.start[3]), 1), 0) : '') + 
     27                ')'; 
    2228        } 
    2329    }); 
    2430 
     
    5056        if (result = /#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])/.exec(color)) 
    5157            return [parseInt(result[1]+result[1],16), parseInt(result[2]+result[2],16), parseInt(result[3]+result[3],16)]; 
    5258 
    53         // Look for rgba(0, 0, 0, 0) == transparent in Safari 3 
    54         if (result = /rgba\(0, 0, 0, 0\)/.exec(color)) 
    55             return colors['transparent'] 
     59        // Look for rgba(num,num,num,alpha) 
     60        if (result = /rgba\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([01]*(?:\.[0-9]+)?)\s*\)/.exec(color)) 
     61            return [parseInt(result[1]), parseInt(result[2]), parseInt(result[3]), parseFloat(result[4])]; 
    5662 
     63        // Look for rgba(num%,num%,num%,alpha) 
     64        if (result = /rgba\(\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([01]*(?:\.[0-9]+)?)\s*\)/.exec(color)) 
     65            return [parseFloat(result[1])*2.55, parseFloat(result[2])*2.55, parseFloat(result[3])*2.55, parseFloat(result[4])]; 
     66 
    5767        // Otherwise, we're most likely dealing with a named color 
    58         return colors[jQuery.trim(color).toLowerCase()]; 
     68        return colors[jQuery.trim(color).toLowerCase()] || [255,255,255]; 
    5969    } 
    6070 
    6171    function getColor(elem, attr) { 
    6272        var color; 
     73        var rgba = /rgba\(\s*0,\s*0,\s*0,\s*0\s*\)/; // transparent 
    6374 
    6475        do { 
    6576            color = jQuery.curCSS(elem, attr); 
    6677 
    6778            // Keep going until we find an element that has color, or we hit the body 
    68             if ( color != '' && color != 'transparent' || jQuery.nodeName(elem, "body") ) 
     79            if ( color != '' && color != 'transparent' && !color.match(rgba) || jQuery.nodeName(elem, "body") ) 
    6980                break; 
    7081 
    7182            attr = "backgroundColor"; 
     
    121132        red:[255,0,0], 
    122133        silver:[192,192,192], 
    123134        white:[255,255,255], 
    124         yellow:[255,255,0], 
    125         transparent: [255,255,255] 
     135        yellow:[255,255,0] 
    126136    }; 
    127137 
    128138})(jQuery);