Set background color based on outdoor temperature

前端 未结 4 537
-上瘾入骨i
-上瘾入骨i 2021-01-31 20:04

Heyoh SO,

I have a temperature widget to implement on a project I am working on. Nothing is specially difficult, I\'ve got a free API to retrieve the datas that I need e

4条回答
  •  感动是毒
    2021-01-31 20:51

    This is a special-case, not a generic solution, but by simply doing a linear gradient between hues and scrunching the blend in the middle range (i.e. the green) you can get a reasonable approximation without color stepping:

    Demo: http://jsfiddle.net/bcronin/kGqbR/18/

    //
    // Function to map a -30 to 30 degree temperature to 
    // a color
    //
    var F = function(t)
    {
        // Map the temperature to a 0-1 range
        var a = (t + 30)/60;
        a = (a < 0) ? 0 : ((a > 1) ? 1 : a);
    
        // Scrunch the green/cyan range in the middle
        var sign = (a < .5) ? -1 : 1;
        a = sign * Math.pow(2 * Math.abs(a - .5), .35)/2 + .5;
    
        // Linear interpolation between the cold and hot
        var h0 = 259;
        var h1 = 12;
        var h = (h0) * (1 - a) + (h1) * (a);
    
        return pusher.color("hsv", h, 75, 90).hex6();
    };
    

提交回复
热议问题