-
Notifications
You must be signed in to change notification settings - Fork 2
/
labwc-gtktheme.py
executable file
·195 lines (164 loc) · 6.15 KB
/
labwc-gtktheme.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/env python3
"""
Create labwc theme based on the current Gtk theme
SPDX-License-Identifier: GPL-2.0-only
Copyright (C) @Misko_2083 2019
Copyright (C) Johan Malm 2019-2022
"""
import os
import errno
import argparse
from tokenize import tokenize, NUMBER, NAME, OP
from io import BytesIO
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
def parse(tokens):
"""
Parse css color expression token list and return red/green/blue values
Valid name-tokens include 'rgb' and 'rgba', whereas 'alpha', 'shade' and
'mix' are ignored. @other-color references are still to be implemented.
"""
nr_colors_to_parse = 0
in_label = False
color = []
for toknum, tokval, _, _, _ in tokens:
if '@' in tokval:
in_label = True
continue
if toknum == NAME and in_label:
color.clear()
color.append(f"@{tokval}")
return color
if nr_colors_to_parse > 0:
if toknum == OP and tokval in ')':
print("warn: still parsing numbers; did not expect ')'")
if toknum == NUMBER:
color.append(tokval)
nr_colors_to_parse -= 1
continue
if toknum == NAME and tokval in 'rgb':
nr_colors_to_parse = 3
elif toknum == NAME and tokval in 'rgba':
nr_colors_to_parse = 4
return color
def color_hex(color):
""" return rrggbb color hex from list [r, g, b,...] """
if not color:
return "None"
elif len(color) < 3:
return f"{color[0]}"
return '{:02x}{:02x}{:02x}'.format(*(int(x) for x in color[:3]))
def hex_from_expr(line):
""" parse color expression to return hex style rrggbb """
tokens = tokenize(BytesIO(line.encode('utf-8')).readline)
color = parse(tokens)
return color_hex(color)
def mkdir_p(path):
try:
os.makedirs(path)
except OSError as exc:
if exc.errno == errno.EEXIST and os.path.isdir(path):
pass
else:
raise
def add(file, key, color):
if color is None:
print(f"warn: no color for {key}")
return
file.write(f"{key}: #{color}\n")
def parse_section(lines, name):
theme = {}
inside = False
for line in lines:
if f"{name} {{" in line:
inside = True
continue
if inside:
if "}" in line or "{" in line:
inside = False
continue
if 'color' not in line:
continue
key, value = line.strip().split(":", maxsplit=1)
theme[f'{name}.{key.replace(" ", "")}'] = hex_from_expr(value)
return theme
def remove_self_referencing_entries(theme):
for key, label in theme.items():
if f'@{key}' == f'{label}':
# A self-referencing line like @define-color foo @foo is bad
print(f'warn: ignore bad line @define-color {key} {label}')
theme.pop(key)
return remove_self_referencing_entries(theme)
return theme
def resolve_labels(theme):
for key, label in theme.items():
if '@' in label:
for tmp, value in theme.items():
if tmp == label[1:]:
theme[key] = value
return resolve_labels(theme)
return theme
def main():
""" main """
parser = argparse.ArgumentParser(prog="labwc-gtktheme")
parser.add_argument("--css", help="dump css and exit", action='store_true')
parser.add_argument("--colors", help="dump colors and exit", action='store_true')
args = parser.parse_args()
gset = Gtk.Settings.get_default()
themename = gset.get_property("gtk-theme-name")
css = Gtk.CssProvider.get_named(themename).to_string()
if args.css:
print(css)
return
lines = css.split("\n")
theme = {}
# Parse @define-color lines using syntax "@define-color <key> <value>"
for line in lines:
if "@define-color" not in line:
continue
x = line.split(" ", maxsplit=2)
theme[x[1]] = hex_from_expr(x[2])
# Add the color definitions in the headerbar{} and menu{} sections
theme.update(parse_section(lines, "headerbar"))
theme.update(parse_section(lines, "menu"))
theme = remove_self_referencing_entries(theme)
theme = resolve_labels(theme)
# Set fallbacks
# Most themes contain headerbar.border-top-color, but Materia does not
if not 'headerbar.border-top-color' in theme.keys():
theme[f'headerbar.border-top-color'] = theme["theme_bg_color"]
if args.colors:
for key, value in theme.items():
print(f"{key}: {value}")
return
themename = 'GTK'
themedir = os.getenv("HOME") + "/.local/share/themes/" + themename + "/openbox-3"
mkdir_p(themedir)
themefile = themedir + "/themerc"
print(f"Create theme {themename} at {themedir}")
with open(themefile, "w") as f:
add(f, "window.active.title.bg.color", theme["theme_bg_color"])
add(f, "window.inactive.title.bg.color", theme["theme_bg_color"])
add(f, "window.active.label.text.color", theme["theme_text_color"])
add(f, "window.inactive.label.text.color", theme["theme_text_color"])
add(f, "window.active.border.color", theme["headerbar.border-top-color"])
add(f, "window.inactive.border.color", theme["headerbar.border-top-color"])
add(f, "window.active.button.unpressed.image.color", theme["theme_fg_color"])
add(f, "window.inactive.button.unpressed.image.color", theme["theme_fg_color"])
add(f, "menu.items.bg.color", theme["menu.background-color"])
add(f, "menu.items.text.color", theme["theme_fg_color"])
add(f, "menu.items.active.bg.color", theme["theme_fg_color"])
add(f, "menu.items.active.text.color", theme["theme_bg_color"])
add(f, "osd.bg.color", theme["theme_bg_color"])
add(f, "osd.border.color", theme["theme_fg_color"])
add(f, "osd.label.text.color", theme["theme_fg_color"])
# TODO:
# border.width: 1
# padding.height: 3
# menu.overlap.x: 0
# menu.overlap.y: 0
# window.label.text.justify: center
# osd.border.width: 1
if __name__ == '__main__':
main()