-
Notifications
You must be signed in to change notification settings - Fork 0
/
colors.py
64 lines (54 loc) · 2.95 KB
/
colors.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
import matplotlib._color_data as mcd
import matplotlib.colors as mcolors
import matplotlib.cm as cm
import numpy as np
cssColors = mcd.CSS4_COLORS
xkcdColors = list(mcd.XKCD_COLORS.values())
# colors = ['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#9467bd', '#8c564b', '#e377c2', '#7f7f7f', '#bcbd22', '#17becf', cssColors['royalblue'], '#a569bd', cssColors['mediumslateblue'], cssColors['salmon'], cssColors['limegreen'], cssColors['hotpink'], cssColors['cadetblue'], cssColors['darkorange'], cssColors['rebeccapurple'], cssColors['firebrick'], cssColors['teal'], cssColors['blueviolet']] # original matplotlib colors with some addition
colors = [cssColors['mediumblue'], cssColors['orange'], cssColors['limegreen'], cssColors['tomato'], cssColors['blueviolet'], cssColors['sienna'], cssColors['hotpink'], cssColors['slategray'], '#bcbd22', cssColors['dodgerblue'], cssColors['mediumslateblue'], cssColors['darkseagreen'], cssColors['salmon'], cssColors['pink'], cssColors['lightcoral'], cssColors['cadetblue'], cssColors['rebeccapurple'], cssColors['teal']] # prettier colors + addition
def getColorByIndex(n):
if n>=len(colors):
return xkcdColors[n]
else:
return colors[n]
def getColorForAge(n):
return cm.get_cmap('spring')(n/8)
# Creating custom cmap:
def hex_to_rgb(value):
'''
Converts hex to rgb colours
value: string of 6 characters representing a hex colour.
Returns: list length 3 of RGB values'''
value = value.strip("#") # removes hash symbol if present
lv = len(value)
return tuple(int(value[i:i + lv // 3], 16) for i in range(0, lv, lv // 3))
def rgb_to_dec(value):
'''
Converts rgb to decimal colours (i.e. divides each value by 256)
value: list (length 3) of RGB values
Returns: list (length 3) of decimal values'''
return [v/256 for v in value]
def get_continuous_cmap(hex_list, float_list=None):
''' creates and returns a color map that can be used in heat map figures.
If float_list is not provided, colour map graduates linearly between each color in hex_list.
If float_list is provided, each color in hex_list is mapped to the respective location in float_list.
Parameters
----------
hex_list: list of hex code strings
float_list: list of floats between 0 and 1, same length as hex_list. Must start with 0 and end with 1.
Returns
----------
colour map'''
rgb_list = [rgb_to_dec(hex_to_rgb(i)) for i in hex_list]
if float_list:
pass
else:
float_list = list(np.linspace(0, 1, len(rgb_list)))
cdict = dict()
for num, col in enumerate(['red', 'green', 'blue']):
col_list = [[float_list[i], rgb_list[i][num], rgb_list[i][num]] for i in range(len(float_list))]
cdict[col] = col_list
cmp = mcolors.LinearSegmentedColormap('my_cmp', segmentdata=cdict, N=256)
return cmp
def getTrafficLightCmap():
return get_continuous_cmap(['#bcffa1', '#fdff54', '#ff3131', '#c40c0c', '#820000'])