-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_previews.py
63 lines (51 loc) · 1.58 KB
/
generate_previews.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
import colorsys
import os
import jinja2
import json
from generate_from_haldclut import (
hex_to_rgb, SCHEMES_DIR, CLUT_THEMES_JSON
)
MONOKAI_THEMES_JSON = './lib/monokai-themes.json'
PREVIEWS_DIR = './previews'
README_TEMPLATE = 'README-template.md'
template_loader = jinja2.FileSystemLoader(searchpath='./')
env = jinja2.Environment(
loader=template_loader,
trim_blocks=True,
lstrip_blocks=True
)
TEMPLATE_FILE = 'preview-template.svg'
def prepare_theme_data(theme_name):
normalized = theme_name.replace(' ', '-').lower()
less_colors = []
with open(os.path.join(SCHEMES_DIR, f'{normalized}.less'), 'r') as fp:
for line in fp.read().splitlines():
less_colors.append(f"#{line.split('#')[1][:6]}")
colors = [{
'hex': color,
'hsv': colorsys.rgb_to_hsv(*list(map(
lambda x: x / 255,
hex_to_rgb(color)
)))
} for color in less_colors]
return {
'name': theme_name,
'slug': normalized,
'colors': colors
}
def render_preview(groups):
template = env.get_template(TEMPLATE_FILE)
with open('preview.svg', 'w') as fp:
fp.write(template.render(groups=groups))
if __name__ == '__main__':
theme_names = []
with open(CLUT_THEMES_JSON, 'r') as fp:
theme_names += json.load(fp)
with open(MONOKAI_THEMES_JSON, 'r') as fp:
theme_names += json.load(fp)
themes = []
for theme_name in theme_names:
data = prepare_theme_data(theme_name)
themes.append(data)
groups = list(zip(*(iter(themes),) * 3))
render_preview(groups)