What is the algorithm to create colors for a heatmap?

后端 未结 6 1643
你的背包
你的背包 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:19

    Here's a simple 5 color heatmap in python (in pyqt, but easy to generalize)

    def genColorMap(self):
        points = [(255,0,0), (255,255,0), (0,255,0), (0,255,255), (0,0,255)]
        cm = {}
        for i in range(0, 256):
            p0 = int(numpy.floor((i/256.0)/len(points)))
            p1 = int(numpy.ceil((i/256.0)/len(points)))
            rgb = map(lambda x: x[0]*max(0,(i-p0)) + x[1]*max(0,(i-p1)), zip(points[p0], points[p1]))
            cm[i] = QtGui.qRgb(rgb[0], rgb[1], rgb[2])
        return cm
    

提交回复
热议问题