forked from CyanideCN/PyCINRAD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathform_colormap.py
135 lines (131 loc) · 5.39 KB
/
form_colormap.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
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import codecs
import re
import numpy as np
import matplotlib.colors as cmx
def form_colormap(filepath, proportion=True, sep=False, spacing='c'):
'''
read text file containing colortable, and then form the colormap according to
the argument given.
sample text file structure: value, R, G, B
Parameters
----------
filepath: string
The path of the colormap txt file.
proportion: boolean, default is True
When proportion is True, LinearSegmentedColormap will be formed
by the input values.
sep: boolean, default is False
When sep is True, the colormap will be a set of color blocks
(no color gradient).
spacing: string, default is 'c', only used when sep is False.
When spacing is 'c', the color blocks will be equally spaced.
A ListedColormap will be returned.
When spacing is 'v', the length of color blocks will be based
on the input values. A LinearSegmentedColormap will be returned.
'''
inidict = {'red':None, 'green':None, 'blue':None}
file_object = codecs.open(filepath, mode='r', encoding='GBK')
all_the_text = file_object.read().strip()
file_object.close()
contents = re.split('[\s]+', all_the_text)
nlin = int(len(contents) / 4)
arr = np.array(contents)
arr = arr.reshape(nlin, 4)
if sep == True:
if spacing == 'c':
ar1 = arr.transpose()
value = []
count = 0
while count < len(ar1[1]):
value.append((int(ar1[1][count]) / 255, int(ar1[2][count]) / 255,
int(ar1[3][count]) / 255))
count = count + 1
return cmx.ListedColormap(value, 256)
elif spacing == 'v':
value = []
r = []
g = []
b = []
for i in arr:
value.append(float(i[0]))
r.append(int(i[1]) / 255)
g.append(int(i[2]) / 255)
b.append(int(i[3]) / 255)
if len(value) > 1:
if value[-1] < value[-2]:
raise ValueError('Values must be in order')
inivalue = value[0]
maxvalue = value[-1]
drange = maxvalue - inivalue
rpart = []
gpart = []
bpart = []
count = 0
try:
while count < len(value):
tupr = ((value[count] - inivalue) / drange, r[count], r[count])
tupg = ((value[count] - inivalue) / drange, g[count], g[count])
tupb = ((value[count] - inivalue) / drange, b[count], b[count])
tupra = ((value[count + 1] - inivalue-1e-5) / drange, r[count], r[count])
tupga = ((value[count + 1] - inivalue-1e-5) / drange, g[count], g[count])
tupba = ((value[count + 1] - inivalue-1e-5) / drange, b[count], b[count])
rpart.append(tupr)
rpart.append(tupra)
gpart.append(tupg)
gpart.append(tupga)
bpart.append(tupb)
bpart.append(tupba)
count=count+1
except IndexError:
rpart.append((1, r[count], r[count]))
gpart.append((1, g[count], g[count]))
bpart.append((1, b[count], b[count]))
inidict['red'] = rpart
inidict['green'] = gpart
inidict['blue'] = bpart
return cmx.LinearSegmentedColormap('my_colormap', inidict, 256)
elif sep == False:
value = []
r = []
g = []
b = []
for i in arr:
value.append(float(i[0]))
r.append(int(i[1]) / 255)
g.append(int(i[2]) / 255)
b.append(int(i[3]) / 255)
if len(value) > 1:
if value[-1] < value[-2]:
raise ValueError('Values must be in order')
inivalue = value[0]
maxvalue = value[-1]
drange = maxvalue-inivalue
rpart = []
gpart = []
bpart = []
if proportion == True:
count = 0
while count < len(value):
tupr = ((value[count] - inivalue) / drange, r[count], r[count])
tupg = ((value[count] - inivalue) / drange, g[count], g[count])
tupb = ((value[count] - inivalue) / drange, b[count], b[count])
rpart.append(tupr)
gpart.append(tupg)
bpart.append(tupb)
count=count + 1
elif proportion == False:
count = 0
while count < len(value):
tupr = (count / len(value), r[count], r[count])
tupg = (count / len(value), g[count], g[count])
tupb = (count / len(value), b[count], b[count])
rpart.append(tupr)
gpart.append(tupg)
bpart.append(tupb)
count = count + 1
inidict['red'] = rpart
inidict['green'] = gpart
inidict['blue'] = bpart
return cmx.LinearSegmentedColormap('my_colormap', inidict, 256)
if __name__ == '__main__':
cdict=form_colormap('C:\\Users\\ww\\Documents\\Visual Studio 2015\\Projects\\colormap\\wxbell cape.txt', sep=True, spacing='v')