OK Stackoverflowians, Here’s the riddle for the day.
I have a weather sidebar widget on a WordPress site that snags all of its data from the Google Weather API. I would like to set a gradient from RED to BLUE and then, by the magic of the javascript gods, to look at the value of the temperature.
Higher number = closer to RED
Lower Number = closer to BLUE
Is there anything out there that can do this? Or would I have to start from scratch?
You could express the color using two dimensions like this:
Then your CSS color would become:
No idea if it will look nice though 😉
You can do this using jQuery:
CSS
HTML
Hope this helps! FYI, the fiddle. Alternatively, you can use
.keyup()
function too! 🙂Here’s a JSFiddle of an adaptation from a similar implementation I made to generate colours for JavaScript heatmap charts.
You pass in a value, a minimum and a maximum value to define a range, which then interpolates and generates a hex value given the first, last and midpoint colours defined in the script. This goes from red to green, given a range of 0 – 50, but it also handles out-of-range values by providing the most extreme colour. Maybe you can modify this to suit your requirements?