Set background color based on outdoor temperature

前端 未结 4 479
-上瘾入骨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:58

    I recently had this conundrum with using time data to display the colors of the sky that that time would correspond to. It's tough, Here are three ways I explored:

    1) The bad-ass way: Make a function for your R, G, B channels separately that would accept an x-intercept of your temperature, and spit out a y-intercept for your Red channel, Blue channel and Green channel over the range of temperatures and corresponding colors you have. To make this I would reverse engineer it by sampling along the color range for some major division of the temperatures and plotting as many points as you can and then drawing a 6th degree polynomial through the points for each of the channels. You would get a function which could accept a temperature value and combine 3 outputs for the R, G, and B channels of an RGB color for alpha 1. Should work, haven't tested it though and am not willing to haha

    2) Make a background class for each of the major colors (you decided whether this is 5 or 50 colors) and toggle between them with an alpha blend. This is what I ended up using for my issue.

    if(temp > 0 && temp <= 5)
    {
         greenBackground.alpha == 1
         yellowBakckground.alpha == (temp/5)
    }
    else if(temp > 5 && temp <= 10)
    

    etc...

    So if your temp was 2.5 then it would be 50% mix of yellow and green

    I was able to implement this option in 1 night and the result looks great! It's time consuming, but do-able and not as messy as you might think.

    3) Make and store an array with RGB colors sampled from your gradient against all the possible integers (there aren't that many between -30 and 30) and round the API's data to integer values if needed. That would be the simplest I suppose. Definitely not as cool as Option 1 though :)

    Good luck!

提交回复
热议问题