I am trying to create a heat map with python. For this I have to assign an RGB value to every value in the range of possible values. I thought of changing the color from blue (minimal value) over green to red (maximal value).
The picture example below explains how I thought of the color composition: We have a range from 1 (pure blue) to 3 (pure red), 2 is in between resembled by green.
I read about linear interpolation and wrote a function that (more or less) handles the calculation for a certain value in the range between a minimum and a maximum and returns an RGB tuple. It uses if
and elif
conditions (which does not make me completely happy):
def convert_to_rgb(minimum, maximum, value):
minimum, maximum = float(minimum), float(maximum)
halfmax = (minimum + maximum) / 2
if minimum <= value <= halfmax:
r = 0
g = int( 255./(halfmax - minimum) * (value - minimum))
b = int( 255. + -255./(halfmax - minimum) * (value - minimum))
return (r,g,b)
elif halfmax < value <= maximum:
r = int( 255./(maximum - halfmax) * (value - halfmax))
g = int( 255. + -255./(maximum - halfmax) * (value - halfmax))
b = 0
return (r,g,b)
However I wonder if one could write a function for each color value without using if
conditions. Does anybody have an idea? Thank you a lot!
"We sense light intensity on a logarithmic scale – an exponential intensity ramp will be seen as a linear ramp" https://courses.cs.washington.edu/courses/cse455/09wi/Lects/lect11.pdf
From the https://en.wikipedia.org/wiki/RGB_color_model: "an input intensity RGB value of (0.5, 0.5, 0.5) only outputs about 22% of full brightness (1.0, 1.0, 1.0), instead of 50%"
This leads to the brownish smudge at 2.5 in @martineau example, where it should be yellow, and cyan at 1.5 in order to get a proper hue gradient.
So the formula you should use to get the gradient is not necessarily what you will want. (sorry for not answering your question directly)
But it might be handy to convert to the HSV or HLS color space model, and use H (for hue) and use that as input, and convert back to RGB for display purposes. ie:
https://docs.python.org/2/library/colorsys.html
Here's another way to do it that, while not as absolutely short as possible, is much more general since it hasn't been hardcoded for your specific set of colors. This means it can also be used to linearly interpolate a specified range of values over a variably-sized palette of arbitrary colors.
Also note that colors could have been interpolated in other colorspaces giving results that may be more pleasing than in others. This is illustrated in the different results obtained from the two separate answers I submitted to a related question titled Range values to pseudocolor.
Numeric output:
Here's the output visualized as a horizontal gradient:
You can often eliminate an
if
with an index into an array of two values. Python lacks a ternary conditional operator, but this works:Replace the
*_curve_1
and*_curve_2
expressions with the constants or slopes or curves either left or right of the midpoint, respectively.I'll leave those substitutions to you, but for example:
red_curve_1
andblue_curve_2
are simply0
green_curve_1
is255*(value-minimum)/(halfmax-minimum)