#!/usr/bin/env python3 """Brand asset + token generator (code-as-truth). Lives at branding/build.py. Single source of truth for the product's visual language. Holds the palette, mark geometry, and type IN CODE, and emits — for every platform — the app-icon ladders AND the color/design token files. Generated outputs are committed; a CI drift check (re-run + `git diff --exit-code`) keeps them honest. Never hand-edit a generated file. Run from the repo root: python3 branding/build.py Requires: rsvg-convert (icon rasterization). `magick` only for previews. This is a STARTER — replace {{PRODUCT}}, the PALETTE values, the MARK geometry, and the output paths to match the product. See https://standards.thmsn.dev/d/branding. """ import json import math import pathlib import subprocess ROOT = pathlib.Path(__file__).parent # branding/ REPO = ROOT.parent # repo root SRC = ROOT / "src"; SRC.mkdir(exist_ok=True) # ── palette (HSL [h 0-360, s %, l %] per scheme; dark is default) ──────────── # primary/secondary/tertiary are the brand seeds; the rest are the semantic # tokens the UI needs. Edit HERE — values flow to web CSS, SwiftUI, and WinUI. PALETTE = { "background": {"dark": [222, 16, 9], "light": [210, 20, 98]}, "foreground": {"dark": [215, 16, 90], "light": [222, 18, 12]}, "surface": {"dark": [222, 15, 12], "light": [210, 20, 96]}, "surface-foreground": {"dark": [215, 14, 86], "light": [222, 16, 20]}, "primary": {"dark": [217, 91, 60], "light": [217, 91, 52]}, "primary-foreground": {"dark": [0, 0, 100], "light": [0, 0, 100]}, "secondary": {"dark": [220, 13, 18], "light": [215, 16, 93]}, "secondary-foreground": {"dark": [215, 14, 88], "light": [222, 16, 22]}, "tertiary": {"dark": [280, 60, 62], "light": [280, 55, 50]}, "tertiary-foreground": {"dark": [0, 0, 100], "light": [0, 0, 100]}, "muted": {"dark": [220, 13, 16], "light": [214, 16, 95]}, "muted-foreground": {"dark": [218, 11, 62], "light": [218, 12, 40]}, "destructive": {"dark": [0, 72, 55], "light": [0, 72, 48]}, "destructive-foreground": {"dark": [0, 0, 100], "light": [0, 0, 100]}, "border": {"dark": [220, 13, 20], "light": [215, 16, 88]}, "input": {"dark": [220, 13, 24], "light": [215, 16, 85]}, "ring": {"dark": [217, 91, 60], "light": [217, 91, 52]}, } FONTS = { "display": ["Hanken Grotesk", "system-ui", "sans-serif"], "sans": ["Hanken Grotesk", "system-ui", "sans-serif"], "mono": ["JetBrains Mono", "ui-monospace", "monospace"], } RADIUS_REM = 0.5 SCHEMES = ("dark", "light") BANNER = "AUTO-GENERATED by branding/build.py. Do not edit by hand." # ── color-space helpers ────────────────────────────────────────────────────── def hsl_to_rgb(h, s, l): s /= 100; l /= 100 c = (1 - abs(2 * l - 1)) * s hp = ((h % 360) + 360) % 360 / 60 x = c * (1 - abs(hp % 2 - 1)) r, g, b = [(c, x, 0), (x, c, 0), (0, c, x), (0, x, c), (x, 0, c), (c, 0, x)][int(hp) % 6] m = l - c / 2 return (r + m, g + m, b + m) def hsl_to_hex(hsl): return "#" + "".join(f"{max(0, min(255, round(v * 255))):02X}" for v in hsl_to_rgb(*hsl)) # ── mark geometry (REPLACE with the product's artwork) ─────────────────────── def master_svg(): primary = hsl_to_hex(PALETTE["primary"]["dark"]) tertiary = hsl_to_hex(PALETTE["tertiary"]["dark"]) return f''' ''' # ── icon rasterization ─────────────────────────────────────────────────────── def render(svg_path, png_path, size): png_path.parent.mkdir(parents=True, exist_ok=True) subprocess.run(["rsvg-convert", "-w", str(size), "-h", str(size), str(svg_path), "-o", str(png_path)], check=True) def write_json(path, obj): path.parent.mkdir(parents=True, exist_ok=True) path.write_text(json.dumps(obj, indent=2) + "\n") def emit_icons(): master = SRC / "master.svg" master.write_text(master_svg()) # iOS: single 1024 (Xcode 14+). ios = REPO / "apps/apple/iOS/Resources/Assets.xcassets/AppIcon.appiconset" render(master, ios / "Icon-1024.png", 1024) write_json(ios / "Contents.json", { "images": [{"filename": "Icon-1024.png", "idiom": "universal", "platform": "ios", "size": "1024x1024"}], "info": {"author": "xcode", "version": 1}}) # macOS: full ladder 16–512 @1x/@2x. mac = REPO / "apps/apple/macOS/Resources/Assets.xcassets/AppIcon.appiconset" images, seen = [], {} for pt, scale in [(16, 1), (16, 2), (32, 1), (32, 2), (128, 1), (128, 2), (256, 1), (256, 2), (512, 1), (512, 2)]: px = pt * scale fn = f"icon_{pt}x{pt}{'@2x' if scale == 2 else ''}.png" if px not in seen: render(master, mac / fn, px); seen[px] = fn else: (mac / fn).write_bytes((mac / seen[px]).read_bytes()) images.append({"filename": fn, "idiom": "mac", "scale": f"{scale}x", "size": f"{pt}x{pt}"}) write_json(mac / "Contents.json", {"images": images, "info": {"author": "xcode", "version": 1}}) # Web: favicons + PWA icons. web = REPO / "apps/web/public" (web).mkdir(parents=True, exist_ok=True) (web / "favicon.svg").write_text(master_svg()) for name, size in [("favicon-16.png", 16), ("favicon-32.png", 32), ("apple-touch-icon.png", 180), ("pwa-192.png", 192), ("pwa-512.png", 512)]: render(master, web / name, size) # Windows: 256px .ico source (convert with `magick` if available). win = REPO / "apps/windows/Assets" render(master, win / "icon-256.png", 256) print("icons: iOS + macOS + web + windows") # ── token emitters ─────────────────────────────────────────────────────────── def emit_web_css(): def vars_for(scheme): return "\n".join(f" --{n}: {c[scheme][0]} {c[scheme][1]}% {c[scheme][2]}%;" for n, c in PALETTE.items()) fonts = "\n".join( f" --font-{k}: {', '.join(repr(f) if ' ' in f else f for f in v)};" for k, v in FONTS.items()) out = REPO / "apps/web/src/tokens.generated.css" out.parent.mkdir(parents=True, exist_ok=True) out.write_text( f"/* {BANNER} */\n:root {{\n{vars_for('light')}\n --radius: {RADIUS_REM}rem;\n{fonts}\n}}\n" f".dark {{\n{vars_for('dark')}\n}}\n") print(f"tokens: {out.relative_to(REPO)}") def emit_swift(): def palette(scheme): lines = [] for n, c in PALETTE.items(): r, g, b = hsl_to_rgb(*c[scheme]) name = "".join(p.capitalize() for p in n.split("-")) name = name[0].lower() + name[1:] lines.append(f" static let {name} = Color(red: {r:.4f}, green: {g:.4f}, blue: {b:.4f})") return "\n".join(lines) out = REPO / "apps/apple/Packages/{{PRODUCT}}Kit/Sources/{{PRODUCT}}UI/Tokens.generated.swift" out.parent.mkdir(parents=True, exist_ok=True) out.write_text( f"// {BANNER}\nimport SwiftUI\n\npublic enum Palette {{\n" f" public enum Dark {{\n{palette('dark')}\n }}\n" f" public enum Light {{\n{palette('light')}\n }}\n}}\n") print(f"tokens: {out.relative_to(REPO)}") def emit_winui(): def dict_for(scheme, key): rows = "\n".join(f' {hsl_to_hex(c[scheme])}' for n, c in PALETTE.items()) return f' \n{rows}\n ' out = REPO / "apps/windows/src/{{PRODUCT}}App/Tokens.generated.xaml" out.parent.mkdir(parents=True, exist_ok=True) out.write_text( f"\n\n" f" \n" f"{dict_for('dark', 'Default')}\n{dict_for('light', 'Light')}\n" f" \n\n") print(f"tokens: {out.relative_to(REPO)}") if __name__ == "__main__": emit_icons() emit_web_css() emit_swift() emit_winui() print("branding: done")