-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgenerate-colors.py
52 lines (37 loc) · 1.18 KB
/
generate-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
import io
from pathlib import Path
import configparser
from matplotlib import pyplot as plt
TOTAL_COUNT = 32
FIXED_COUNT = 8
FIXED_COLORMAP = 'binary_r'
COLORMAPS = [
'CMRmap',
'cubehelix',
'gist_earth',
'gist_ncar',
'gist_rainbow',
'jet',
'nipy_spectral',
'ocean',
'turbo',
'twilight',
]
def main():
path = Path.cwd()
for colormap in COLORMAPS:
colors = generate_colormap_colors(FIXED_COLORMAP, FIXED_COUNT) + generate_colormap_colors(colormap, TOTAL_COUNT - FIXED_COUNT)
filename = f'{TOTAL_COUNT}-{colormap}.ini'
data = generate_colors_file_contents(colors)
(path / filename).write_text(data, newline='')
def generate_colors_file_contents(colors: list[tuple]) -> str:
config = configparser.ConfigParser()
config['Colors'] = {i + 1: f'#{bytes(color[:-1]).hex()}' for i, color in enumerate(colors)}
data = io.StringIO()
config.write(data)
return data.getvalue().strip()
def generate_colormap_colors(colormap: str, count: int) -> list[tuple]:
colormap = plt.get_cmap(colormap)
return [colormap(i / (count - 1), bytes=True) for i in range(count)]
if __name__ == '__main__':
main()