-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathhtml_colors.py
287 lines (245 loc) · 8.58 KB
/
html_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
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
from paths import path_to_scripts
import os.path
cb12_set3_file = '{}/tmp.colorbrewer_12class_set3.txt' #JCC
cb12_paired_file = '{}/tmp.colorbrewer_12class_paired.txt' #JCC
if os.path.isfile(cb12_set3_file) and os.path.isfile(cb12_paired_file): #JCC
cb_n12_set3 = [x.split()[0] for x in open(cb12_set3_file.format(path_to_scripts),'r') ] #JCC
cb_n12_paired = [x.split()[0] for x in open(cb12_paired_file.format(path_to_scripts),'r') ] #JCC
#cb_n12_set3 = [x.split()[0] for x in open('{}/tmp.colorbrewer_12class_set3.txt'.format(path_to_scripts),'r') ]
#cb_n12_paired = [x.split()[0] for x in open('{}/tmp.colorbrewer_12class_paired.txt'.format(path_to_scripts),'r') ]
COLOR_BREWER = False
if COLOR_BREWER:
CB_RED = cb_n12_paired[0]
CB_GREEN = cb_n12_paired[1]
CB_BLUE = cb_n12_paired[2]
CB_ORANGE = cb_n12_paired[3]
CB_PURPLE = cb_n12_paired[4]
CB_BROWN = cb_n12_paired[5]
else:
CB_RED = 'red'
CB_GREEN = 'green'
CB_BLUE = 'blue'
CB_ORANGE = 'orange'
CB_PURPLE = 'purple'
CB_BROWN = 'brown'
colors_string = """AliceBlue #F0F8FF Shades Mix
AntiqueWhite #FAEBD7 Shades Mix
Aqua #00FFFF Shades Mix
Aquamarine #7FFFD4 Shades Mix
Azure #F0FFFF Shades Mix
Beige #F5F5DC Shades Mix
Bisque #FFE4C4 Shades Mix
Black #000000 Shades Mix
BlanchedAlmond #FFEBCD Shades Mix
Blue #0000FF Shades Mix
BlueViolet #8A2BE2 Shades Mix
Brown #A52A2A Shades Mix
BurlyWood #DEB887 Shades Mix
CadetBlue #5F9EA0 Shades Mix
Chartreuse #7FFF00 Shades Mix
Chocolate #D2691E Shades Mix
Coral #FF7F50 Shades Mix
CornflowerBlue #6495ED Shades Mix
Cornsilk #FFF8DC Shades Mix
Crimson #DC143C Shades Mix
Cyan #00FFFF Shades Mix
DarkBlue #00008B Shades Mix
DarkCyan #008B8B Shades Mix
DarkGoldenRod #B8860B Shades Mix
DarkGray #A9A9A9 Shades Mix
DarkGreen #006400 Shades Mix
DarkKhaki #BDB76B Shades Mix
DarkMagenta #8B008B Shades Mix
DarkOliveGreen #556B2F Shades Mix
DarkOrange #FF8C00 Shades Mix
DarkOrchid #9932CC Shades Mix
DarkRed #8B0000 Shades Mix
DarkSalmon #E9967A Shades Mix
DarkSeaGreen #8FBC8F Shades Mix
DarkSlateBlue #483D8B Shades Mix
DarkSlateGray #2F4F4F Shades Mix
DarkTurquoise #00CED1 Shades Mix
DarkViolet #9400D3 Shades Mix
DeepPink #FF1493 Shades Mix
DeepSkyBlue #00BFFF Shades Mix
DimGray #696969 Shades Mix
DodgerBlue #1E90FF Shades Mix
FireBrick #B22222 Shades Mix
FloralWhite #FFFAF0 Shades Mix
ForestGreen #228B22 Shades Mix
Fuchsia #FF00FF Shades Mix
Gainsboro #DCDCDC Shades Mix
GhostWhite #F8F8FF Shades Mix
Gold #FFD700 Shades Mix
GoldenRod #DAA520 Shades Mix
Gray #808080 Shades Mix
Green #008000 Shades Mix
GreenYellow #ADFF2F Shades Mix
HoneyDew #F0FFF0 Shades Mix
HotPink #FF69B4 Shades Mix
IndianRed #CD5C5C Shades Mix
Indigo #4B0082 Shades Mix
Ivory #FFFFF0 Shades Mix
Khaki #F0E68C Shades Mix
Lavender #E6E6FA Shades Mix
LavenderBlush #FFF0F5 Shades Mix
LawnGreen #7CFC00 Shades Mix
LemonChiffon #FFFACD Shades Mix
LightBlue #ADD8E6 Shades Mix
LightCoral #F08080 Shades Mix
LightCyan #E0FFFF Shades Mix
LightGoldenRodYellow #FAFAD2 Shades Mix
LightGray #D3D3D3 Shades Mix
LightGreen #90EE90 Shades Mix
LightPink #FFB6C1 Shades Mix
LightSalmon #FFA07A Shades Mix
LightSeaGreen #20B2AA Shades Mix
LightSkyBlue #87CEFA Shades Mix
LightSlateGray #778899 Shades Mix
LightSteelBlue #B0C4DE Shades Mix
LightYellow #FFFFE0 Shades Mix
Lime #00FF00 Shades Mix
LimeGreen #32CD32 Shades Mix
Linen #FAF0E6 Shades Mix
Magenta #FF00FF Shades Mix
Maroon #800000 Shades Mix
MediumAquaMarine #66CDAA Shades Mix
MediumBlue #0000CD Shades Mix
MediumOrchid #BA55D3 Shades Mix
MediumPurple #9370DB Shades Mix
MediumSeaGreen #3CB371 Shades Mix
MediumSlateBlue #7B68EE Shades Mix
MediumSpringGreen #00FA9A Shades Mix
MediumTurquoise #48D1CC Shades Mix
MediumVioletRed #C71585 Shades Mix
MidnightBlue #191970 Shades Mix
MintCream #F5FFFA Shades Mix
MistyRose #FFE4E1 Shades Mix
Moccasin #FFE4B5 Shades Mix
NavajoWhite #FFDEAD Shades Mix
Navy #000080 Shades Mix
OldLace #FDF5E6 Shades Mix
Olive #808000 Shades Mix
OliveDrab #6B8E23 Shades Mix
Orange #FFA500 Shades Mix
OrangeRed #FF4500 Shades Mix
Orchid #DA70D6 Shades Mix
PaleGoldenRod #EEE8AA Shades Mix
PaleGreen #98FB98 Shades Mix
PaleTurquoise #AFEEEE Shades Mix
PaleVioletRed #DB7093 Shades Mix
PapayaWhip #FFEFD5 Shades Mix
PeachPuff #FFDAB9 Shades Mix
Peru #CD853F Shades Mix
Pink #FFC0CB Shades Mix
Plum #DDA0DD Shades Mix
PowderBlue #B0E0E6 Shades Mix
Purple #800080 Shades Mix
RebeccaPurple #663399 Shades Mix
Red #FF0000 Shades Mix
RosyBrown #BC8F8F Shades Mix
RoyalBlue #4169E1 Shades Mix
SaddleBrown #8B4513 Shades Mix
Salmon #FA8072 Shades Mix
SandyBrown #F4A460 Shades Mix
SeaGreen #2E8B57 Shades Mix
SeaShell #FFF5EE Shades Mix
Sienna #A0522D Shades Mix
Silver #C0C0C0 Shades Mix
SkyBlue #87CEEB Shades Mix
SlateBlue #6A5ACD Shades Mix
SlateGray #708090 Shades Mix
Snow #FFFAFA Shades Mix
SpringGreen #00FF7F Shades Mix
SteelBlue #4682B4 Shades Mix
Tan #D2B48C Shades Mix
Teal #008080 Shades Mix
Thistle #D8BFD8 Shades Mix
Tomato #FF6347 Shades Mix
Turquoise #40E0D0 Shades Mix
Violet #EE82EE Shades Mix
Wheat #F5DEB3 Shades Mix
White #FFFFFF Shades Mix
WhiteSmoke #F5F5F5 Shades Mix
Yellow #FFFF00 Shades Mix
YellowGreen #9ACD32 Shades Mix"""
html_colors = []
html_colors_no_whites = []
html_colors_no_lights = []
all_rgb = {}
def rgb_from_hex( hex ):
assert hex[0] == '#' and len(hex)==7
return ( int(hex[1:3],16),
int(hex[3:5],16),
int(hex[5:7],16) )
for line in colors_string.split('\n'):
l = line.split()
colorname = l[0].lower()
if colorname == 'rebeccapurple': continue ## matplotlib doesnt like this one...
hex = l[1]
r,g,b = rgb_from_hex( hex )
lowername = colorname.lower()
html_colors.append( lowername )
all_rgb[ lowername ] = (r,g,b)
avg_rgb= sum((r,g,b))/3.0
if 'white' not in lowername:
html_colors_no_whites.append( lowername )
if 'white' not in lowername and avg_rgb<210:
html_colors_no_lights.append( lowername )
## get some colors for objects sorted by frequency
## fiddle with random seed/state to get consistent colors; legacy silliness...
##
import random
initial_random_state = random.getstate()
random.seed(2)
if COLOR_BREWER:
starter_colors = cb_n12_paired
starter_colors_rgbs = [ rgb_from_hex(x) for x in starter_colors ]
min_rgb_sum = min( ( sum(x) for x in starter_colors_rgbs ) )
for c,rgb in zip( starter_colors, starter_colors_rgbs ):
#print c, rgb in all_rgb.values()
all_rgb[c] = rgb
other_colors = [ x for x in html_colors_no_whites if all_rgb[x] not in starter_colors_rgbs and sum(all_rgb[x]) >= min_rgb_sum ]
other_colors_no_lights = [ x for x in html_colors_no_lights if all_rgb[x] not in starter_colors_rgbs and sum(all_rgb[x]) >= min_rgb_sum ]
print 'min_rgb_sum:',min_rgb_sum,'other_colors:',len(other_colors),'other_colors_no_lights:',len(other_colors_no_lights)
else:
starter_colors = ['red',
'green',
'blue',
'cyan',
'magenta',
'black',
'lime',
'purple',
'gold',
'olive',
'maroon',
]
for c in starter_colors:
assert c in html_colors_no_whites
other_colors = [ x for x in html_colors_no_whites if x not in starter_colors ]
other_colors_no_lights = [ x for x in html_colors_no_lights if x not in starter_colors ]
random.shuffle( other_colors )
rank_colors = starter_colors + other_colors
random.shuffle( other_colors_no_lights )
rank_colors_no_lights = starter_colors + other_colors_no_lights
random.setstate(initial_random_state)
def get_rank_colors( num ):
n_rank_colors = len(rank_colors)
colors = []
for i in range( num ):
colors.append( rank_colors[ i%n_rank_colors ] )
return colors
def get_rank_colors_no_lights( num ):
n_rank_colors = len(rank_colors_no_lights)
colors = []
for i in range( num ):
colors.append( rank_colors_no_lights[ i%n_rank_colors ] )
return colors
if __name__=='__main__':
colors = get_rank_colors(20)
for i,col in enumerate(colors):
print i,col,sum(all_rgb[col])
colors = get_rank_colors_no_lights(20)
for i,col in enumerate(colors):
print i,col,sum(all_rgb[col])