Ant4res
Ant4res

Reputation: 1225

Get colors from double values

I have to create an heap map from a matrix of double values (both positive and negative), but how can I get a color from those values within a range between green and red? Thanks

Upvotes: 1

Views: 931

Answers (3)

yokuyuki
yokuyuki

Reputation: 98

Assuming green is positive and red is negative like in most red-green heatmaps, find the most positive value among the positive values and divide them by it and then find the most negative value among the negative values and divide them by it. Multiply the ratios for the positive values by 255 for green and keeping red as 0 and the ratios for the negative values by 255 for red and keeping green as 0. So when the value is 0, it should be black; when the value is at the most positive, it's fully green; when the value is at the most negative, it's fully red.

Upvotes: 1

ben author
ben author

Reputation: 2955

You'll want to map your value range to a hue range in a hue-saturation-value/lightness colorspace, then translate that to RGB.

Upvotes: 2

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272517

Pick an RGB colour for "full red" (e.g. (255,0,0)) and for "full green" (e.g. (0,255,0)). Then interpolate between them, based on the ratio between your input value and the maximum possible value.

The simplest possible interpolation is linear interpolation in the RGB colour space. However, this might not give a very satisfactory result (in particular, the brightness and saturation of the values will vary). A better approach might be to convert into the HSV colour space, and do the interpolation there.

Upvotes: 2

Related Questions