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
|
# deck-it - reads the UDT palette and renders it as CSS
# Copyright (C) 2026 Danilo M. <danix@danix.xyz>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# 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, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
"""Read UDT's palette output and render it as CSS custom properties.
UDT writes the structural palette to ~/.config/rofi/udt/palette.rasi and the
wallpaper accent to ~/.cache/udt/udt-accent.rasi. deck-it reads both rather
than restating the colours, so a scheme or wallpaper change reaches the tablet.
"""
import json
import re
from pathlib import Path
from urllib.parse import quote
PALETTE_LINE = re.compile(r"^\s*([a-z0-9]+)\s*:\s*(#[0-9a-fA-F]{6,8});", re.M)
ACCENT = re.compile(r"accent:\s*(#[0-9a-fA-F]{6,8})")
BORDER = re.compile(r"rgb\(\s*([0-9a-fA-F]{6})\s*\)")
PALETTE_PATH = Path.home() / ".config" / "rofi" / "udt" / "palette.rasi"
ACCENT_PATH = Path.home() / ".cache" / "udt" / "udt-accent.rasi"
BORDER_PATH = Path.home() / ".cache" / "udt" / "udt-border.lua"
# Keystone's fonts page owns text sizes (deck-it's own override, else the base)
# and the clock's font families.
FONTS_PATH = Path.home() / ".config" / "keystone" / "fonts.json"
FONT_SIZE = 14 # px, the size deck.css was designed at
# Catppuccin Macchiato, used before UDT has ever generated its files.
FALLBACK = {
"base": "#24273a", "mantle": "#1e2030", "crust": "#181926",
"surface0": "#363a4f", "surface1": "#494d64", "surface2": "#5b6078",
"text": "#cad3f5", "subtext0": "#a5adcb", "subtext1": "#b8c0e0",
"overlay0": "#6e738d", "red": "#ed8796", "green": "#a6da95",
"yellow": "#eed49f", "teal": "#8bd5ca", "blue": "#8aadf4",
"lavender": "#b7bdf8", "peach": "#f5a97f", "mauve": "#c6a0f6",
"accent": "#b7bdf8",
}
def parse_palette(text: str) -> dict[str, str]:
return dict(PALETTE_LINE.findall(text))
def parse_accent(text: str) -> str | None:
m = ACCENT.search(text)
return m[1] if m else None
def parse_border(text: str) -> list[str]:
"""The three hues Hyprland rotates in the window border, as hex."""
return ["#" + value.lower() for value in BORDER.findall(text)]
def parse_font_size(text: str) -> int | None:
"""deck-it's px size from keystone's fonts.json, range-checked as quickshell does."""
try:
data = json.loads(text)
px = (data.get("override") or {}).get("deckit", data.get("base"))
except (ValueError, AttributeError):
return None
if isinstance(px, (int, float)) and not isinstance(px, bool) and 8 <= px <= 40:
return round(px)
return None
# fonts.json's "family" keys for the dim cover's clock, and the selector each sets.
CLOCK_FONTS = {"deckit-clock": ".dim-time", "deckit-date": ".dim-date"}
# Its "override" keys for the clock's px sizes; unset keeps deck.css's rem.
CLOCK_SIZES = {"deckit:clock": ".dim-time", "deckit:date": ".dim-date"}
def parse_clock_sizes(text: str) -> dict[str, int]:
"""The clock's own px sizes from keystone's fonts.json, by selector, range-checked."""
try:
override = json.loads(text).get("override") or {}
except (ValueError, AttributeError):
return {}
if not isinstance(override, dict):
return {}
return {sel: round(px) for key, sel in CLOCK_SIZES.items()
if isinstance(px := override.get(key), (int, float)) and not isinstance(px, bool) and 8 <= px <= 400}
def parse_families(text: str) -> dict[str, str]:
"""The clock font families from keystone's fonts.json, by key; empty ones left out."""
try:
family = json.loads(text).get("family") or {}
except (ValueError, AttributeError):
return {}
if not isinstance(family, dict):
return {}
return {k: v.strip() for k, v in family.items()
if k in CLOCK_FONTS and isinstance(v, str) and v.strip()}
def families() -> dict[str, str]:
try:
return parse_families(FONTS_PATH.read_text())
except OSError:
return {}
def font_rules(fams: dict[str, str], sizes: dict[str, int] | None = None) -> str:
"""@font-face and selector rules for each set clock font. The family is in
the URL only, quoted, so a change reloads the file and no name reaches the CSS."""
out = []
for key, sel in CLOCK_FONTS.items():
if key in fams:
out.append(f'@font-face {{ font-family: "{key}"; src: url("/fonts/{key}?f={quote(fams[key])}"); }}')
out.append(f'#dimmer {sel} {{ font-family: "{key}", "Inconsolata Nerd Font Mono", '
'ui-monospace, monospace; font-weight: normal; }')
out += [f"#dimmer {sel} {{ font-size: {px}px; }}" for sel, px in (sizes or {}).items()]
return "".join(line + "\n" for line in out)
def css(colors: dict, accent: str | None, border: list[str] | None = None,
font_size: int | None = None, fams: dict[str, str] | None = None,
clock_sizes: dict[str, int] | None = None) -> str:
lines = [":root {"]
lines.append(f" --udt-font-size: {font_size or FONT_SIZE}px;")
lines += [f" --udt-{name}: {value};" for name, value in colors.items()]
lines.append(f" --udt-accent: {accent or FALLBACK['accent']};")
stops = border if border and len(border) >= 3 else [
FALLBACK["mauve"], FALLBACK["lavender"], FALLBACK["blue"]]
for index, value in enumerate(stops[:3], 1):
lines.append(f" --udt-border-{index}: {value};")
lines.append("}")
return "\n".join(lines) + "\n" + font_rules(fams or {}, clock_sizes)
def load() -> str:
"""Render the live UDT palette as CSS, falling back if it is not installed."""
try:
colors = parse_palette(PALETTE_PATH.read_text())
except OSError:
colors = {}
if not colors:
colors = dict(FALLBACK)
try:
accent = parse_accent(ACCENT_PATH.read_text())
except OSError:
accent = None
try:
border = parse_border(BORDER_PATH.read_text())
except OSError:
border = None
try:
fonts = FONTS_PATH.read_text()
except OSError:
fonts = ""
return css(colors, accent, border, parse_font_size(fonts), parse_families(fonts),
parse_clock_sizes(fonts))
|