-
-
Notifications
You must be signed in to change notification settings - Fork 83
/
create-themes.py
87 lines (66 loc) · 2.67 KB
/
create-themes.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
# Render themes, creates them as separate files and as
# one combined file `ios-themes.yaml`.
# Part of https://github.com/basnijholt/lovelace-ios-themes
from pathlib import Path
import jinja2
import yaml
from PIL import Image, ImageColor
with open("settings-light-dark.yaml") as f:
all_settings = yaml.safe_load(f)
COMMIT = "a37376d918fcfe4785be99910dc9a7200ac37da9"
BASE_URL = (
f"https://cdn.jsdelivr.net/gh/basnijholt/lovelace-ios-themes@{COMMIT}/themes"
)
def parse(x):
return x if "#" not in x else f'"{x}"'
def average_color(fname):
color = Image.open(fname).resize((1, 1)).getpixel((0, 0))
hex_color = "#{:02x}{:02x}{:02x}".format(*color)
rgb_color = ImageColor.getrgb(hex_color)
return "rgba({}, {}, {}, 0.4)".format(*rgb_color)
def fname_to_url(background: Path) -> str:
return f"{BASE_URL}/{background.name}"
def fname_to_local_path(background: Path) -> str:
return f"/local/ios-themes/{background.name}"
BACKGROUND_COLORS = {
# Suggested by @okets in issue #42
"blue-red": "rgba(30, 2, 61, 0.4)",
"dark-blue": "rgba(48, 69, 124, 0.4)",
"dark-green": "rgba(48, 89, 71, 0.4)",
"light-blue": "rgba(1, 195, 220, 0.4)",
"light-green": "rgba(114, 188, 139, 0.4)",
"orange": "rgba(255, 229, 116, 0.4)",
"red": "rgba(234, 88, 63, 0.4)",
}
fname = Path("themes/ios-themes.yaml")
fname.parent.mkdir(parents=True, exist_ok=True)
with fname.open("w") as f:
f.write("---\n# From https://github.com/basnijholt/lovelace-ios-themes")
for background in sorted(Path("themes").glob("homekit-bg-*.jpg")):
color = background.stem.split("homekit-bg-")[-1]
if color in BACKGROUND_COLORS:
app_header_background_color = BACKGROUND_COLORS[color]
else:
app_header_background_color = average_color(background)
for which in ["light", "dark"]:
for standard in [False, True]:
settings = {k: parse(v[which]) for k, v in all_settings.items()}
if standard:
settings["state_icon_active_color"] = "rgba(255, 214, 10, 1)"
suffix = ""
else:
suffix = "-alternative"
with open("template.jinja2") as f:
template = jinja2.Template("".join(f.readlines()))
result = template.render(
**settings,
which=which,
app_header_background_color=app_header_background_color,
background=fname_to_url(background)
if standard
else fname_to_local_path(background),
color=color,
suffix=suffix,
)
with fname.open("a") as f:
f.write("\n" + result + "\n")