Have an account? Sign in
Login  Register  Facebook
awesome way to move a tree and keep its life DIY | 3 Amazing Diet Coke Can Hacks Animation Shows 550 Million Years Of Human Evolution In Seconds
This Page is Under Construction! - If You Want To Help Please Send your CV - Advanced Web Core (BETA)
[Edit] From red to green color map depend on percentage
Example
document.write("<span style='background-color: "+getColorForPercentage(0)+";'></span>");
document.write("<span style='background-color: "+getColorForPercentage(0.20)+";'></span>");
document.write("<span style='background-color: "+getColorForPercentage(0.40)+";'></span>");
document.write("<span style='background-color: "+getColorForPercentage(0.60)+";'></span>");
document.write("<span style='background-color: "+getColorForPercentage(0.80)+";'></span>");
document.write("<span style='background-color: "+getColorForPercentage(1)+";'></span>");

Try it yourself »Click on the "Try it yourself" button to see how it works

var percentColors = [
    { pct: 0.0, color: { r: 0xff, g: 0x00, b: 0 } },
    { pct: 0.5, color: { r: 0xff, g: 0xff, b: 0 } },
    { pct: 1.0, color: { r: 0x00, g: 0xff, b: 0 } } ];

var getColorForPercentage = function(pct) {
    for (var i = 0; i < percentColors.length; i++) {
        if (pct <= percentColors[i].pct) {
            var lower = percentColors[i - 1] || { pct: 0.1, color: { r: 0x0, g: 0x00, b: 0 } };
            var upper = percentColors[i];
            var range = upper.pct - lower.pct;
            var rangePct = (pct - lower.pct) / range;
            var pctLower = 1 - rangePct;
            var pctUpper = rangePct;
            var color = {
                r: Math.floor(lower.color.r * pctLower + upper.color.r * pctUpper),
                g: Math.floor(lower.color.g * pctLower + upper.color.g * pctUpper),
                b: Math.floor(lower.color.b * pctLower + upper.color.b * pctUpper)
            };
            return 'rgb(' + [color.r, color.g, color.b].join(',') + ')';
        }
    }
}

Webfairy Mediat - Every Thing About Apple