-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmap2rgb.py
36 lines (26 loc) · 900 Bytes
/
cmap2rgb.py
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
28
29
30
31
32
33
34
35
36
import argparse
import matplotlib as mpl
from matplotlib.colors import LinearSegmentedColormap
import numpy as np
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-c', '--cmap', type=str, default='YlGn')
args = parser.parse_args()
print(f"const {args.cmap} = new Colormap('{args.cmap}',\n[")
cmap = mpl.colormaps[args.cmap]
if isinstance(cmap, LinearSegmentedColormap):
for x in np.linspace(0.0, 1.0, 255):
r, g, b, _ = cmap(x)
r255 = int(r * 255)
g255 = int(g * 255)
b255 = int(b * 255)
print(f' "rgb({r255} {g255} {b255})",')
else:
for r, g, b in cmap.colors:
r255 = int(r * 255)
g255 = int(g * 255)
b255 = int(b * 255)
print(f' "rgb({r255} {g255} {b255})",')
print("]);")
if __name__ == "__main__":
main()