jQuery.extend({ dechex: function(dec) { return ((dec < 16) ? "0" : "") + dec.toString(16); }, zebraDecay: function(x) { return parseInt(Math.max(x - 16, 0, x * 0.8)); } }); jQuery.fn.extend({ // highlight alternative table rows applyZebra: function() { if (jQuery(this).hasClass('noZebra')) return; jQuery('tr:nth-child(odd)', this).each(function(){ var row = jQuery(this); if (row.hasClass('odd') || row.hasClass('even')) return true; var rowBg = row.css('background-color'); // TODO: determine which browsers will say if tds are transparent // and allow us skip processing those tds row.children('td').each(function(){ var cell = jQuery(this); if (cell.hasClass('footer') || cell.hasClass('noZebra')) return true; var r=-1, g=-1, b=-1; // get colour from cell var oc = cell.css('background-color'); if (typeof oc=="undefined" || oc == "transparent" || oc == 'rgba(0, 0, 0, 0)') { // try to get colour from row oc = rowBg; } if (typeof oc=="undefined" || oc == "transparent" || oc == 'rgba(0, 0, 0, 0)') { r = g = b = 255; } else { if (oc.length == 4 && oc.substr(0,1) == '#') { // handle short values oc = "#"+oc.charAt(1)+oc.charAt(1)+oc.charAt(2)+ oc.charAt(2)+oc.charAt(3)+oc.charAt(3); } if (oc.length == 7 && oc.substr(0,1) == '#') { r=parseInt(oc.substr(1,2),16); g=parseInt(oc.substr(3,2),16); b=parseInt(oc.substr(5,2),16); } else if (oc.substr(0,4) == 'rgb(') { var rgb = oc.substr(4,oc.length-5).split(","); r = rgb[0]; g = rgb[1]; b = rgb[2]; } else { /* if it's a named colour, we don't know how to interpret it * so will do nothing to it * * Note that Firefox will store named colours as their RGB values * but IE & opera won't */ //alert(oc); } } if (r >= 0 && g >= 0 && b >= 0) { // get darker colour from original r = jQuery.zebraDecay(r); g = jQuery.zebraDecay(g); b = jQuery.zebraDecay(b); cell.css('background-color', "#"+jQuery.dechex(r)+jQuery.dechex(g)+jQuery.dechex(b)); } }); }); } }); $(function() { $('table.data > tbody > tr:nth-child(odd)').addClass('odd'); $('table.data > tr:nth-child(odd)').addClass('odd'); }); // vim: syntax=javascript st=4 ts=4 sw=4 tw=79