-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcolor_utilities.py
302 lines (231 loc) · 11.1 KB
/
color_utilities.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# -*- coding: utf-8 -*-
"""
Module containing beautiful color schemes and some color utilities.
Authors:
Giulia Longhi, [email protected] (color schemes)
Marco Raveri, [email protected] (code)
"""
# ******************************************************************************
import math
# ******************************************************************************
# Definition of the color maps:
# ------------------------------------------------------------------------------
the_gold_standard = { 0: (203.0/255.0, 15.0/255.0, 40.0/255.0),
1: (255.0/255.0, 165.0/255.0, 0.0),
2: (42.0/255.0, 46.0/255.0, 139.0/255.0),
3: (0.0/255.0, 153.0/255.0, 204.0/255.0),
4: (0.0/255.0, 221.0/255.0, 52.0/255.0),
5: (0.0, 0.75, 0.75),
6: (0.0, 0.0, 0.0),
}
# ------------------------------------------------------------------------------
spring_and_winter = {
0: (93./255., 50./255., 137./255.),
1: (197./255., 43./255., 135./255.),
2: (237./255., 120./255., 159./255.),
3: (241./255., 147./255., 130./255.),
4: (113./255., 187./255., 220./255.),
5: (24./255., 120./255., 187./255.),
}
# ------------------------------------------------------------------------------
winter_and_spring = { 0: (24./255., 120./255., 187./255.),
1: (113./255., 187./255., 220./255.),
2: (241./255., 147./255., 130./255.),
3: (237./255., 120./255., 159./255.),
4: (197./255., 43./255., 135./255.),
5: (93./255., 50./255., 137./255.),
}
# ------------------------------------------------------------------------------
summer_sun = { 0: (234./255., 185./255., 185./255.),
1: (234./255., 90./255., 103./255.),
2: (255./255., 231./255., 76./255.),
3: (249./255., 179./255., 52./255.),
4: (55./255., 97./255., 140./255.),
5: (82./255., 158./255., 214./255.),
}
# ------------------------------------------------------------------------------
summer_sky = { 0: (82./255., 158./255., 214./255.),
1: (55./255., 97./255., 140./255.),
2: (249./255., 179./255., 52./255.),
3: (255./255., 231./255., 76./255.),
4: (234./255., 90./255., 103./255.),
5: (234./255., 185./255., 185./255.),
}
# ------------------------------------------------------------------------------
autumn_fields = { 0: (50./255., 138./255., 165./255.),
1: (16./255., 135./255., 98./255.),
2: (198./255., 212./255., 60./255.),
3: (255./255., 251./255., 73./255.),
4: (237./255., 118./255., 40./255.),
5: (142./255., 26./255., 26./255.),
}
# ------------------------------------------------------------------------------
autumn_leaves = { 0: (142./255., 26./255., 26./255.),
1: (237./255., 118./255., 40./255.),
2: (255./255., 251./255., 73./255.),
3: (198./255., 212./255., 60./255.),
4: (16./255., 135./255., 98./255.),
5: (50./255., 138./255., 165./255.),
}
# ------------------------------------------------------------------------------
shades_of_gray = { 0: (90./255., 90./255., 90./255.)
}
# ******************************************************************************
# definition of color interpolation utilities:
def color_linear_interpolation( rgb_1, rgb_2, alpha ):
"""
This function performs a linear color interpolation in RGB space.
alpha has to go from zero to one and is the coordinate.
"""
_out_color = []
for _a,_b in zip(rgb_1,rgb_2):
_out_color.append( _a +(_b-_a)*alpha )
return tuple(_out_color)
# ******************************************************************************
# definition of the color helper:
def nice_colors( num, colormap='the_gold_standard', interpolation_method='linear', output_format='RGB' ):
"""
This function returns a color from a colormap defined above, according to the
number entered.
:param num: input number. Can be an integer or float.
If the number is integer the function returns one of the colors in the
colormap. If the number is a float returns the shade combining the two
neighbouring colors.
:type num: :class:`int` or :class:`float`
:param colormap: a string containing the name of the colormap.
:type colormap: :class:`string`
:param interpolation_method: the method to interpolate between colors.
Legal choices are:
interpolation_method='linear', linear interpolation;
Further interpolation methods will be added in the future.
:type interpolation_method: :class:`string`
:param output_format: output format of the color.
Legal choices are:
output_format='HEX'
output_format='RGB' (default)
:type output_format: :class:`string`
:return: string with HEX color or tuple with RGB coordinates
"""
# get the colormap:
try:
_cmap = globals()[str(colormap)]
except:
raise ValueError('Requested color map ('+str(colormap)+') does not exist.')
# get the indexes of the color map:
_idx_low = int( math.floor(num%len(_cmap)) )
_idx_up = int( math.floor((_idx_low+1)%len(_cmap)) )
# perform color interpolation:
if interpolation_method=='linear':
_t = num%len(_cmap)-_idx_low
_out_color = color_linear_interpolation(_cmap[_idx_low],_cmap[_idx_up],_t)
else:
raise ValueError('Requested color interpolation method ('+str(interpolation_method)+') does not exist.')
# choose the output format:
if output_format=='HEX':
_out_color = '#%02x%02x%02x' % tuple( [ _c*255. for _c in _out_color] )
_out_color = str(_out_color)
elif output_format=='RGB':
pass
else:
raise ValueError('Requested output format ('+str(output_format)+') does not exist.')
#
return _out_color
# ******************************************************************************
# Definition of bash colors:
class bash_colors:
"""
This class contains the necessary definitions to print to bash screen with colors.
Sometimes it can be useful and nice!
:ivar HEADER: ANSI color for light purple.
:ivar OKBLUE: ANSI color for blue.
:ivar OKGREEN: ANSI color for green.
:ivar WARNING: ANSI color for yellow.
:ivar FAIL: ANSI color for red.
:ivar BOLD: ANSI code for bold text.
:ivar UNDERLINE: ANSI code for underlined text.
:ivar ENDC: ANSI code to restore the bash default.
"""
# --------------------------------------------------------------------------
HEADER = '\033[95m' #: ANSI color for light purple.
OKBLUE = '\033[94m' #: ANSI color for blue.
OKGREEN = '\033[92m' #: ANSI color for green.
WARNING = '\033[93m' #: ANSI color for yellow.
FAIL = '\033[91m' #: ANSI color for red.
BOLD = '\033[1m' #: ANSI code for bold text.
UNDERLINE = '\033[4m' #: ANSI code for underlined text.
ENDC = '\033[0m' #: ANSI code to restore the bash default.
# --------------------------------------------------------------------------
def __init__(self):
pass
# --------------------------------------------------------------------------
def header(self, string):
"""
Function that returns a string that can be printed to bash in :class:`cosmicfish_pylib.colors.bash_colors.HEADER` color.
:param string: input string.
:type string: string
:return: the input string with the relevant ANSI code at the beginning and at the end.
:rtype: string
"""
return self.HEADER+str(string)+self.ENDC
# --------------------------------------------------------------------------
def blue(self,string):
"""
Function that returns a string that can be printed to bash in :class:`cosmicfish_pylib.colors.bash_colors.OKBLUE` color.
:param string: input string.
:type string: string
:return: the input string with the relevant ANSI code at the beginning and at the end.
:rtype: string
"""
return self.OKBLUE+str(string)+self.ENDC
# --------------------------------------------------------------------------
def green(self,string):
"""
Function that returns a string that can be printed to bash in :class:`cosmicfish_pylib.colors.bash_colors.OKGREEN` color.
:param string: input string.
:type string: string
:return: the input string with the relevant ANSI code at the beginning and at the end.
:rtype: string
"""
return self.OKGREEN+str(string)+self.ENDC
# --------------------------------------------------------------------------
def warning(self,string):
"""
Function that returns a string that can be printed to bash in :class:`cosmicfish_pylib.colors.bash_colors.WARNING` color.
:param string: input string.
:type string: string
:return: the input string with the relevant ANSI code at the beginning and at the end.
:rtype: string
"""
return self.WARNING+str(string)+self.ENDC
# --------------------------------------------------------------------------
def fail(self,string):
"""
Function that returns a string that can be printed to bash in :class:`cosmicfish_pylib.colors.bash_colors.FAIL` color.
:param string: input string.
:type string: string
:return: the input string with the relevant ANSI code at the beginning and at the end.
:rtype: string
"""
return self.FAIL+str(string)+self.ENDC
# --------------------------------------------------------------------------
def bold(self,string):
"""
Function that returns a string that can be printed to bash in :class:`cosmicfish_pylib.colors.bash_colors.BOLD` color.
:param string: input string.
:type string: string
:return: the input string with the relevant ANSI code at the beginning and at the end.
:rtype: string
"""
return self.BOLD+str(string)+self.ENDC
# --------------------------------------------------------------------------
def underline(self,string):
"""
Function that returns a string that can be printed to bash in :class:`cosmicfish_pylib.colors.bash_colors.UNDERLINE` color.
:param string: input string.
:type string: string
:return: the input string with the relevant ANSI code at the beginning and at the end.
:rtype: string
"""
return self.UNDERLINE+str(string)+self.ENDC
# --------------------------------------------------------------------------
# ******************************************************************************