Algorithm to convert any positive integer to an RGB value

后端 未结 10 1318
没有蜡笔的小新
没有蜡笔的小新 2020-12-04 13:49

We have a heatmap we want to display. The numbers that will make up the values being displayed are unknown (except that they will be positive integers). The range of numbers

10条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-04 14:19

    This answer is probably a little late to the party. I'm displaying some environmental data, and need to colour the resulting bars from green to red relative to the max and min of the data set (or which ever values are passed as max and min to the function. Anyway, the below accomplishes that. Can be changed for blue to red fairly easily enough, I would think.

    // scale colour temp relatively
    
    function getColourTemp(maxVal, minVal, actual) {
        var midVal = (maxVal - minVal)/2;
        var intR;
        var intG;
        var intB = Math.round(0);
    
        if (actual >= midVal){
             intR = 255;
             intG = Math.round(255 * ((maxVal - actual) / (maxVal - midVal)));
        }
        else{
            intG = 255;
            intR = Math.round(255 * ((actual - minVal) / (midVal - minVal)));
        }
    
        return to_rgb(intR, intG, intB);
    }
    

提交回复
热议问题