Return to Snippet

Revision: 17701
at September 11, 2009 18:09 by asterfr


Initial Code
function interpolateColor(minColor,maxColor,maxDepth,depth){

    function d2h(d) {return d.toString(16);}
    function h2d(h) {return parseInt(h,16);}
   
    if(depth == 0){
        return minColor;
    }
    if(depth == maxDepth){
        return maxColor;
    }
   
    var color = "#";
   
    for(var i=1; i <= 6; i+=2){
        var minVal = new Number(h2d(minColor.substr(i,2)));
        var maxVal = new Number(h2d(maxColor.substr(i,2)));
        var nVal = minVal + (maxVal-minVal) * (depth/maxDepth);
        var val = d2h(Math.floor(nVal));
        while(val.length < 2){
            val = "0"+val;
        }
        color += val;
    }
    return color;
}

Initial URL


Initial Description
Use case : interpolateColor("00EEDD","3ADECE",2,1) : interpolate the color level 1 between #00EEDD et #3ADECE on a 3 level scales (from 0 to 2).

Initial Title
Color Interpolation

Initial Tags


Initial Language
JavaScript