-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.py
147 lines (114 loc) · 3.37 KB
/
generate.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
import json
import os
import sys
from PIL import Image
import numpy
import jinja2
jinja_env = jinja2.Environment(loader = jinja2.FileSystemLoader(os.path.abspath('./templates/')))
from pycolor import get_color_from_json
def file_save_log(filepath):
size = os.path.getsize(filepath)
kB = round(size / 1000, 1)
print(f"saved: {filepath} [{kB}kB]")
def generate_readme(colors):
readme_md_template = jinja_env.get_template("readme_md.md")
firebrick = [c for c in colors if c.lowername == "firebrick"][0]
readme_text = readme_md_template.render(
colors=colors,
firebrick=firebrick
)
with open("README.md", "w") as f:
f.write(readme_text)
print("saved: README.md")
def generate_css(colors):
color_css_template = jinja_env.get_template("color_css.css")
for color in colors:
css_text = color_css_template.render(
name=color.lowername,
palette=color.get_palette()
)
with open(color.cssPath, "w") as f:
f.write(css_text)
file_save_log(color.cssPath)
def generate_json(colors):
for color in colors:
with open(color.jsonPath, "w") as f:
json.dump(color.get_palette_json(), f, indent="\t")
file_save_log(color.jsonPath)
def generate_docs(colors):
colorpage_html_template = jinja_env.get_template("colorpage.html")
for color in colors:
colorpage_text = colorpage_html_template.render(
color=color
)
with open(color.htmlPath, "w") as f:
f.write(colorpage_text)
file_save_log(color.htmlPath)
homepage_html_template = jinja_env.get_template("homepage.html")
homepage_html_text = homepage_html_template.render(
colors=colors
)
homepage_path = "docs/index.html"
with open(homepage_path, "w") as f:
f.write(homepage_html_text)
file_save_log(homepage_path)
def generate_master_json(colors):
jo = {}
jo["colors"] = []
for color in colors:
colorJson = {}
colorJson = color.as_object()
colorJson["palette"] = color.get_palette_json()[color.lowername]
jo["colors"].append(colorJson)
master_json_path = "master.json"
with open(master_json_path, "w") as f:
json.dump(jo, f, indent="\t")
file_save_log(master_json_path)
def generate_master_css(colors):
master_css_template = jinja_env.get_template("master_css.css")
csspath = "master.css"
css_text = master_css_template.render(
colors=colors
)
with open(csspath, "w") as f:
f.write(css_text)
file_save_log(csspath)
def generate_pngs(colors):
SIZE = 50
startY = 0
endY = SIZE
for color in colors:
palette = color.get_palette()
data = numpy.zeros((SIZE, SIZE*len(palette), 3), dtype=numpy.uint8)
for idx, shade in enumerate(palette):
startX = idx * SIZE
endX = startX + SIZE
for x in range(startX, endX):
for y in range(startY, endY):
data[y][x] = shade["rgb"]
im = Image.fromarray(data)
im.save(color.pngpath)
file_save_log(color.pngpath)
def main():
colorsJson = json.load(open("colors.json"))
colors = [get_color_from_json(jo) for jo in colorsJson["colors"]]
args = sys.argv[1:]
command = None if len(args) == 0 else args[0].lower()
if command == "readme":
generate_readme(colors)
elif command == "css":
generate_css(colors)
elif command == "json":
generate_json(colors)
elif command == "docs":
generate_docs(colors)
elif command == "masterjson":
generate_master_json(colors)
elif command == "mastercss":
generate_master_css(colors)
elif command == "png":
generate_pngs(colors)
else:
generate_readme(colors)
if __name__ == '__main__':
main()