-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathcolormap_creator_matplotlib.py
More file actions
76 lines (68 loc) · 2.33 KB
/
colormap_creator_matplotlib.py
File metadata and controls
76 lines (68 loc) · 2.33 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#import cmocean
import matplotlib.pyplot as plt
import numpy as np
import tecplot as tp
from tecplot.constant import *
colormaps = [
plt.get_cmap('viridis')
,plt.get_cmap('turbo')
,plt.get_cmap('plasma')
,plt.get_cmap('inferno')
,plt.get_cmap('magma')
#,cmocean.cm.thermal
#,cmocean.cm.haline
#,cmocean.cm.solar
#,cmocean.cm.ice
#,cmocean.cm.gray
#,cmocean.cm.oxy
#,cmocean.cm.deep
#,cmocean.cm.dense
#,cmocean.cm.algae
#,cmocean.cm.matter
#,cmocean.cm.turbid
#,cmocean.cm.speed
#,cmocean.cm.amp
#,cmocean.cm.tempo
#,cmocean.cm.phase
#,cmocean.cm.balance
#,cmocean.cm.delta
#,cmocean.cm.curl
]
def create_tecplot_colormap_command(cm, fractions, suffix=""):
#print("NumPoints ", cm.N)
cmd = "$!CREATECOLORMAP\n"
colormapname = "{}{}".format(cm.name,suffix)
cmd += " NAME = '{}'\n".format(colormapname)
cmd += " NUMCONTROLPOINTS = {}\n".format(len(fractions))
fractions = list(fractions)
fractions.sort()
for i,frac in enumerate(fractions):
point = i+1
value = int(frac*cm.N)
#print("Point: ", point)
#print("Value: ", value)
# Handle sharp changes in the colormap
lead = cm(value)
trail = cm(value-1)
if np.allclose(trail, lead, 0.2):
trail = lead
max_rgb = 255
lead_r = lead[0]*max_rgb
lead_g = lead[1]*max_rgb
lead_b = lead[2]*max_rgb
trail_r = trail[0]*max_rgb
trail_g = trail[1]*max_rgb
trail_b = trail[2]*max_rgb
cmd += " CONTROLPOINT %d {COLORMAPFRACTION = %f LEADRGB {R=%d G=%d B=%d} TRAILRGB {R=%d G=%d B=%d}}\n" %(point,frac,lead_r,lead_g,lead_b,trail_r,trail_g,trail_b)
return colormapname, cmd
with open("colormaps.map", "w") as cmap_file:
cmap_file.write("#!MC 1410\n")
for cmap in colormaps:
# 11 control points is a pretty good approximation. In some cases you may need more or may need
# very specific control point fractions (like for the cmocean.cm.oxy colormap)
num_control_points = 11
fractions = set(np.linspace(0.0,1.0,num_control_points))
ret = create_tecplot_colormap_command(cmap, fractions, suffix=" - matplotlib")
print(ret[0])
cmap_file.write(ret[1])
print("Wrote colormaps to: colormaps.map")