summaryrefslogtreecommitdiff
path: root/color/colormap.py
blob: b3761aeb414a70b94ecaf5e43437a20a2583c061 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import numpy as np

import matplotlib.pyplot as plt

def map_to_color(a, range=None):
    a = np.asarray(a)
    if range is None:
        range = (a.min(), a.max())

    x = np.linspace(0, np.pi, 100)

    yr = np.cos(x)**2
    yr[x > np.pi/2] = 0.0
    yg = np.sin(x)**2
    yb = np.cos(x)**2
    yb[x < np.pi/2] = 0.0

    #plt.plot(x, yr, 'r-', x, yb, 'b-', x, yg, 'g-')
    #plt.show()

    ax = np.pi*(a - range[0])/(range[1]-range[0])

    r = (np.interp(ax, x, yr)*255).astype(np.uint32)
    g = (np.interp(ax, x, yg)*255).astype(np.uint32)
    b = (np.interp(ax, x, yb)*255).astype(np.uint32)

    return r << 16 | g << 8 | b