"""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 re from pathlib import Path 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" # 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 css(colors: dict, accent: str | None, border: list[str] | None = None) -> str: lines = [":root {"] 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" 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 return css(colors, accent, border)