TableTower/tools/theme_art/svg_common.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

84 lines
3.2 KiB
Python

"""Shared helpers for the theme art generator: SVG text, numbers, colours, seeded random numbers."""
import os
REPO_ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
THEMES_DIR = os.path.join(REPO_ROOT, "themes")
# Shapes copied from the wardrobe mockup (one source for every module).
LEAF_PATH = "M0,-10 C6,-6 6,6 0,10 C-6,6 -6,-6 0,-10 Z"
MAPLE_LEAF_PATH = ("M0,-12 L3,-5 L9,-7 L6,-1 L11,2 L4,3 L5,10 L0,6 L-5,10 L-4,3 "
"L-11,2 L-6,-1 L-9,-7 L-3,-5 Z")
def num(value):
"""Short, stable text for a number (no trailing zeros)."""
text = f"{value:.2f}".rstrip("0").rstrip(".")
return "0" if text in ("-0", "") else text
def grey(lightness):
"""A grey hex colour (r = g = b) for a lightness from 0 to 1."""
level = max(0, min(255, round(lightness * 255)))
return f"#{level:02x}{level:02x}{level:02x}"
def svg_document(width, height, body, defs=""):
"""Wrap SVG body text in a root element with explicit size and viewBox."""
defs_text = f"<defs>{defs}</defs>" if defs else ""
return (f'<svg xmlns="http://www.w3.org/2000/svg" width="{num(width)}" height="{num(height)}" '
f'viewBox="0 0 {num(width)} {num(height)}">{defs_text}{body}</svg>\n')
def write_theme_file(theme_id, file_name, text):
"""Write one art file into themes/<id>/ and return its path."""
folder = os.path.join(THEMES_DIR, theme_id)
os.makedirs(folder, exist_ok=True)
path = os.path.join(folder, file_name)
with open(path, "w", encoding="utf-8") as handle:
handle.write(text)
return path
def remove_theme_file(theme_id, file_name):
"""Delete a stale art file so a re-run matches the current plan."""
path = os.path.join(THEMES_DIR, theme_id, file_name)
if os.path.exists(path):
os.remove(path)
def css_colour(text):
"""Split a mockup colour ('#hex' or 'rgba(r,g,b,a)') into (hex, opacity)."""
text = text.strip()
if text.startswith("rgba"):
parts = [part.strip() for part in text[text.index("(") + 1:text.index(")")].split(",")]
red, green, blue = (int(float(part)) for part in parts[:3])
return f"#{red:02x}{green:02x}{blue:02x}", float(parts[3])
return text, 1.0
def wrapped_copies(inner, tile_width, tile_height):
"""Draw tile content nine times (shifted by the tile size) so shapes cut at an edge reappear opposite."""
copies = []
for shift_x in (-tile_width, 0, tile_width):
for shift_y in (-tile_height, 0, tile_height):
copies.append(f'<g transform="translate({num(shift_x)} {num(shift_y)})">{inner}</g>')
return "".join(copies)
class MockupRandom:
"""The mockup's seeded random numbers (mulberry32), so shapes land where the mockup put them."""
def __init__(self, seed):
self.state = seed & 0xFFFFFFFF
@staticmethod
def _multiply(first, second):
return (first * second) & 0xFFFFFFFF
def next(self):
self.state = (self.state + 0x6D2B79F5) & 0xFFFFFFFF
state = self.state
mixed = self._multiply(state ^ (state >> 15), 1 | state)
mixed = ((mixed + self._multiply(mixed ^ (mixed >> 7), 61 | mixed)) & 0xFFFFFFFF) ^ mixed
return ((mixed ^ (mixed >> 14)) & 0xFFFFFFFF) / 4294967296