1

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

Ant4res
  • 1,187
  • 1
  • 15
  • 34

3 Answers3

2

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.

Oliver Charlesworth
  • 252,669
  • 29
  • 530
  • 650
2

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

ben author
  • 2,675
  • 2
  • 23
  • 43
1

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.

yokuyuki
  • 98
  • 1
  • 6