-
Notifications
You must be signed in to change notification settings - Fork 2
/
generate-themes
executable file
·127 lines (110 loc) · 4.55 KB
/
generate-themes
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
#!/usr/bin/env python3
# material-linux - Auto-generated set of consistent themes for a Linux
# desktop based on the Android Material color palette.
#
# Copyright (C) 2014 Andrzej Pronobis
# Author: Andrzej Pronobis <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
import sys
import re
import shutil
from x256 import x256
from material_colors import material_colors
from theme import theme as theme_common
from palette_dark import palette as palette_dark
# Definition of color themes
theme_infos = [['dark', palette_dark, theme_common]]
# Extensions for which we should just copy without parsing
asset_exts = ['.png']
def generate_theme(theme_name, palette, theme):
"""Generate a single theme."""
# Resolve colors
print("-> Resolving palette colors")
rgb_regexp = re.compile(
"^#[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]$")
for palette_color_name, color_name in palette.items():
color_parts = color_name.split('-')
x = '-'.join(color_parts[:-1])
y = color_parts[-1]
if (len(x) > 0) and (x in material_colors) and (y in material_colors[x]):
palette[palette_color_name] = material_colors[x][y]
elif not rgb_regexp.match(color_name):
print("ERROR: Unknown palette color %s!" % color_name)
sys.exit(1)
print("-> Resolving theme colors")
for theme_color_name, color_name in theme.items():
if color_name not in palette:
print("ERROR: Unknown theme color %s!" % color_name)
sys.exit(1)
theme[theme_color_name] = palette[color_name]
# Compile color tags for palette and theme colors
tags = {}
# Palette
for color, value in palette.items():
tags["[material:" + color + "]"] = value
# Palette 256
for color, value in palette.items():
x = x256.from_hex(value[1:])
tags["[material256:" + color + "]"] = str(x)
# Theme
for color, value in theme.items():
tags["[material:" + color + "]"] = value
# Theme 256
for color, value in theme.items():
x = x256.from_hex(value[1:])
tags["[material256:" + color + "]"] = str(x)
# Compile replacement pattern
pattern = re.compile(r'(' + '|'.join(
re.escape(key) for key in tags.keys()) + r')')
# Get def files
script_dir = os.path.realpath(sys.path[0])
def_dir = os.path.join(script_dir, 'defs', theme_name)
out_dir = os.path.join(script_dir, 'out')
# Process each file
for dir_name, _, files in os.walk(def_dir):
for file_name in files:
rel_dir = os.path.relpath(dir_name, def_dir)
in_file_path = os.path.join(dir_name, file_name)
out_file_dir = os.path.realpath(os.path.join(out_dir, theme_name, rel_dir))
out_file_path = os.path.join(out_file_dir, file_name)
in_file_ext = os.path.splitext(in_file_path)
# Make out dir
os.makedirs(out_file_dir, exist_ok=True)
# Parse or copy
if in_file_ext[1] in asset_exts:
# Copy
print("-> Copying file: %s" %
os.path.relpath(os.path.join(rel_dir, file_name)))
shutil.copy(in_file_path, out_file_path)
else:
# Parse
print("-> Parsing file: %s" %
os.path.relpath(os.path.join(rel_dir, file_name)))
in_file = open(in_file_path, 'r')
out_file = open(out_file_path, 'w')
for line in in_file:
result = pattern.sub(lambda x: tags[x.group()], line)
out_file.write(result)
out_file.close()
in_file.close()
def main():
# Generate all themes
for theme_info in theme_infos:
print("Processing theme '%s'... " % theme_info[0])
generate_theme(theme_info[0], theme_info[1], theme_info[2])
print("Done!\n")
if __name__ == '__main__':
main()