What is the algorithm to create colors for a heatmap?

后端 未结 6 1524
你的背包
你的背包 2021-01-30 10:31

Assuming values are normalized from 0 to 1, what is the algoritm to get a color to create a heatmap like this?

1 is red, .5 is green, 0 is dark blue.

Working in

6条回答
  •  长情又很酷
    2021-01-30 11:22

    A general approach is to interpolate colors. You decided that

    0: 0 0 255 (or any blue)
    0.5: 0 255 0 (or any green)
    1: 255 0 0 (or any red)
    

    You simply do a linear interpolation of the RGB. Between 2 reference values (eg t between 0 and 0.5), the interpolated color C is like

    C = (1 - t) * c0 + t * c1
    

    You must apply this formula on each color component RGB. Some other hints about color linear interpolation: How to interpolate a color sequence?

    ---- edit ----- I removed the header of my answer, as I realized I misunderstood the question (see comment). I leave a copy for consistent reading, and information, just in case.

    A first possibility is to build a reference heatmap with any software that would: create a image 256X1pixel with pixel values from 0 to 255 and apply the desired heatmap with ImageMagick: then you can read the RGB back and build a map (value:RGB).

提交回复
热议问题