Temperature Color Tint Function
First should mention that in an attempt to post more here and get more out, future things will be less like proper write-ups on subjects and more about what I’m working on in concise chunks of digestible info - in this case some math to output RGB after taking in temperature in Fahrenheit.
I’d previously located and used a function written by others (and rewritten by me) to calculate the RGB color emitted by a black body based on its temperature in Kelvin. This is good for things like what color a star should glow in a space game, which is what I was using it for.
I now have started a very different game, built on the same engine (of course, code re-use is good and they will cross-pollinate and enhance each other) and needed to color tiles based on their temperature in Fahrenheit. Here’s what it turned out like (it will look nicer later, this is early engine work, test texture, more fine tuning of colors to do later, etc.).
I went into that by hand instead of researching how to accurately calculate the color, because on this scale it’s much more dependent on what the material is and how much light is hitting it of what color. As such, I’m just worrying about a vague tint rather than accuracy.
I ended up with this:
TL;DR below? Just read this indented paragraph.
The colors are the actual RGB graphs for each color, X is temperature, Y is amount of color, it’s centered on 60 as the average middle temperature, and any incoming X values are clamped to fit in roughly -10 to 130 to keep to the desired outputs (plans for changing over to another equation for each color outside of this range are in my head).
Here’s the long, rambling version of the explanation/thought process..
When working on the Fahrenheit temperature color function I used the graphing tool that I like to use online (www.desmos.com/calculator) and ended up with a set of elegant-looking equations, colored RGB to match what they were.
R: tan((x-60)/50) G: cos((x-60)/30) B: -tan((x-60)/50)
Cosine is used for G because we needed a soft curve centered on the X origin, and tangent was used for the others for its pattern of exponential increase in one direction and decrease in the other (B is sign flipped because we want more blue for cold and more red for hot, so they are opposite).
The "-60" bit of these equations shifts the X origin (not literally, but for purposes of discussion/thought) to 60, which in this case means 60 degrees Fahrenheit, roughly the average temperature here on Earth and a moderate livable temperature.
Finally there's a division piece of the puzzle. When working with graphs visually, adding a division operation on the X value stretches the graph's progression horizontally - it essentially modifies the frequency of a continuous function. By dividing R and B by 50, the curves in the X value range we're interested in stretch from -20 to 140. By dividing G by 30, its curve nearly matches the curve of the two tangent curves from 10 to 110, amusingly.
Values fed in are clamped so that the repeating nature of cos/tan functions doesn't cause cycling of colors over a wider temperature range. Anything under a certain point can only get so blue, and anything over a certain point can only get so red. As well, we don't want the green to come back at random, so this is pretty important. If we want the colors to cover a wider range we'd need to add six more functions, three which take over for RGB under a certain point (R would be just always 0) and over a certain point (where B would just always be 0). I'd say we would probably widen the green curve and then these linear lines would come from R and G, the R linearly rising slowly and G lowering slowly in the temperature up direction, with the same happening with B and G in the other direction.








