TableTower/tools/theme_defs/make_theme_defs.py
RobinFiredrake 61a548902f Theme tiers 1-2: theme folders, mascot recolour, background scene and tile, frame sets, picture threads, sounds; nine new built-in themes
- ThemeAssets loads a theme's files; FrameSet + GradientMap (3-stop) generalise the card frame;
  ThreadStrip draws threads from a picture; BoardBackdrop draws scene and tile; MascotArt
  recolours Morgan by role (logo SVG now uses the Keep File importer); SoundBoard plays
  roll / card turn / thread / click / ambient with View > Sound levels.
- Vaporwave, Synthwave, Cozy Autumn, Rustic, Woodsy, Sky Blue, Frutiger Aero, Gothic, Terminal:
  generated art (tools/theme_art), sounds (tools/theme_sounds), settings (tools/theme_defs).
- Ten OFL fonts; --theme=<id> flag; docs/THEMES.md format spec; test_theme_assets.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 15:05:40 -06:00

156 lines
6.3 KiB
Python

"""Writes themes/<id>/theme.gd for each theme in palettes.py.
Run: python3 -I tools/theme_defs/make_theme_defs.py
It turns each theme's small mockup colour set into the app's full colour
token list, adds the art and sound files that exist in themes/<id>/, and the
frame / thread numbers from tools/theme_art/meta.json. See docs/THEMES.md.
"""
import json
import os
import sys
HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, HERE)
from palettes import THEMES # noqa: E402
REPO = os.path.dirname(os.path.dirname(HERE))
# Mockup threads are drawn at mockup scale (Tarot: 4 px); the app draws Tarot's at 11 px.
THREAD_SCALE = 2.5
def to_rgb(hex_text):
hex_text = hex_text.lstrip("#")[:6]
return [int(hex_text[i:i + 2], 16) / 255 for i in (0, 2, 4)]
def to_hex(rgb):
return "#" + "".join("%02x" % round(max(0, min(1, part)) * 255) for part in rgb)
def mix(first, second, amount):
"""`amount` of the way from `first` to `second`."""
a, b = to_rgb(first), to_rgb(second)
return to_hex([a[i] + (b[i] - a[i]) * amount for i in range(3)])
def lighten(color, amount):
return mix(color, "#ffffff", amount)
def darken(color, amount):
return mix(color, "#000000", amount)
def luminance(color):
red, green, blue = to_rgb(color)
return 0.299 * red + 0.587 * green + 0.114 * blue
def tokens(theme):
"""The app's colour tokens (ThemeStore.TOKENS) from the mockup's set."""
mock = theme["colors"]
dark_card = luminance(mock["card"]) < 0.4
out = {
"BG": mock["bg"], "BG_DEEP": darken(mock["bg"], 0.45), "BG_HOVER": lighten(mock["bg"], 0.15),
"PANEL": mock["panel"], "ON_BG": mock["onbg"], "ON_BG_MUTED": mix(mock["onbg"], mock["bg"], 0.35),
"CARD": mock["card"], "CARD_SOFT": mock["card2"], "CARD_HOVER": mix(mock["card"], mock["rule"], 0.6),
"CARD_SHADE": mix(mock["card"], mock["muted"], 0.25),
"INK": mock["ink"], "MUTED": mock["muted"], "FAINT": mix(mock["muted"], mock["card"], 0.4),
"RULE": mock["rule"], "DASH": mix(mock["rule"], mock["muted"], 0.45),
"HOVER_ROW": mix(mock["card"], mock["lit"], 0.35), "STRIPE": mock["ink"] + "0e",
"TRIM": mock["trim"], "TRIM_DARK": mock["trimD"],
"THREAD": mock["thread"], "THREAD_DARK": mock["threadD"],
"ACCENT": mock["accent"], "ACCENT_DARK": darken(mock["accent"], 0.55),
"GROUP": mock["accent"], "LIT": mock["lit"], "FLICK": mock["accent"] + "59",
"PULSE": lighten(mock["accent"], 0.7), "GLOW": mock["glow"],
"VALUE_BG": mix(mock["card"], mock["accent"], 0.18),
"VALUE_BORDER": mix(mock["card"], mock["accent"], 0.55),
"VALUE_INK": lighten(mock["accent"], 0.2) if dark_card else darken(mock["accent"], 0.5),
"VALUE_INK_ON_BG": mock["accent"],
"REF_BG": mix(mock["card"], mock["glow"], 0.2),
"REF_INK": lighten(mock["glow"], 0.3) if dark_card else darken(mock["glow"], 0.5),
"REF_INK_ON_BG": lighten(mock["glow"], 0.3),
}
if dark_card:
out["WARN_BG"] = mix(mock["card"], "#e0574a", 0.3)
out["WARN_INK"] = "#ffb1a3"
out["DANGER_INK"] = "#ff8a7a"
out["OK_INK"] = "#7fe0a0"
out["NO_INK"] = "#ff8a7a"
out.update(theme.get("overrides", {}))
return out
def mascot_colors(theme):
colors = dict(theme["mascot"])
colors.setdefault("inner_line", mix(colors["outline"], colors["cloth"], 0.55))
return colors
def file_keys(theme_id, meta):
"""Keys for the files that exist in themes/<id>/, with meta.json numbers."""
folder = os.path.join(REPO, "themes", theme_id)
has = lambda name: os.path.exists(os.path.join(folder, name)) # noqa: E731
keys = {}
if has("background_scene.svg"):
keys["background_scene"] = {"file": "background_scene.svg"}
keys["background_scene"].update(meta.get("background_scene") or {})
if has("background_tile.svg"):
keys["background_tile"] = {"file": "background_tile.svg"}
keys["background_tile"].update(meta.get("background_tile") or {})
if has("frame_corner.svg") and has("frame_edge.svg"):
frame = {"corner": "frame_corner.svg", "edge": "frame_edge.svg"}
if has("frame_corner_fixed.svg"):
frame["corner_fixed"] = "frame_corner_fixed.svg"
frame.update({key: value for key, value in (meta.get("frame") or {}).items() if key != "has_corner_fixed"})
keys["frame"] = frame
keys["card_frame"] = "art"
if has("thread_strip.svg"):
thread = {"strip": "thread_strip.svg"}
if has("thread_pin.svg"):
thread["pin"] = "thread_pin.svg"
thread.update(meta.get("thread") or {})
if "width" in thread:
thread["width"] = round(float(thread["width"]) * THREAD_SCALE, 1)
keys["thread"] = thread
sounds = {}
for event in ("roll", "card_turn", "thread", "click", "ambient"):
if has("sounds/%s.ogg" % event):
sounds[event] = "sounds/%s.ogg" % event
if sounds:
keys["sounds"] = sounds
return keys
def build(theme_id, theme, meta):
data = {"id": theme_id, "name": theme["name"], "builtin": True,
"pattern": theme.get("pattern", "none"), "logo": "fortune_teller", "card_frame": "plain",
"font_body": theme["fonts"][0], "font_display": theme["fonts"][1],
"background": theme["background"], "panel": theme["panel"],
"mascot_colors": mascot_colors(theme)}
data.update(file_keys(theme_id, meta))
data["colors"] = tokens(theme)
return data
def write(theme_id, data):
body = json.dumps(data, indent="\t", ensure_ascii=False)
text = ("extends RefCounted\n## Built-in theme: %s. Written by tools/theme_defs/make_theme_defs.py:\n"
"## change tools/theme_defs/palettes.py and re-run it rather than editing this file.\n\n"
"const DATA := %s\n") % (data["name"], body)
path = os.path.join(REPO, "themes", theme_id, "theme.gd")
os.makedirs(os.path.dirname(path), exist_ok=True)
with open(path, "w", encoding="utf-8") as handle:
handle.write(text)
return path
def main():
meta_path = os.path.join(REPO, "tools", "theme_art", "meta.json")
all_meta = json.load(open(meta_path)) if os.path.exists(meta_path) else {}
for theme_id, theme in THEMES.items():
print(write(theme_id, build(theme_id, theme, all_meta.get(theme_id, {}))))
if __name__ == "__main__":
main()