Using a function that can calculate the color component at a point between two values, you can use the rgb(r,g,b) color syntax in CSS to set the background color:
function morph(start, stop, point) { return Math.round(stop - start) * point / 100 + start); } $('td').each(function(){ var value = parseInt($(this).text()); var color; if (value < 0) { color = morph(255,100,-value) + ',' + morph(255,0,-value) + ',' + morph(255,0,-value); } else { color = morph(255,0,value) + ',' + morph(255,50,value) + ',' + morph(255,0,value); } $(this).css('background-color', 'rgb(' + color + ')'); });
Guffa source share