CI / check (push) Canceled after 0s
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
122 lines
4.5 KiB
Python
Executable File
122 lines
4.5 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""The app's icons, from the server's pixel leaf (`web/static/img/logo-mark.svg`).
|
|
|
|
scripts/gen-icons.py [<server-ref>] (default HEAD of ../LLeMbas, or LEMBAS_SERVER)
|
|
|
|
The mark is a 16x16 grid of rects, so it converts exactly into Android vector drawables -- one
|
|
path per colour -- and needs no PNG at any density:
|
|
|
|
drawable/ic_launcher_foreground.xml the leaf and the app's own sign, in the adaptive icon's 66dp safe zone
|
|
drawable/ic_launcher_monochrome.xml their silhouette, for themed icons
|
|
drawable/ic_notification.xml the leaf's silhouette alone: a status-bar icon is alpha only
|
|
|
|
Each LLeMbas product carries the server's leaf with one small pixel sign of its own in the free
|
|
corner; this app's is a phone. The launcher's art is 24 pixels across: the leaf at (2, 2), the sign
|
|
at (13, 14).
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import re
|
|
import subprocess
|
|
import sys
|
|
from collections import defaultdict
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parent.parent
|
|
SERVER = Path(os.environ.get("LEMBAS_SERVER", ROOT.parent / "LLeMbas"))
|
|
SOURCE = "src/lembas/web/static/img/logo-mark.svg"
|
|
RES = ROOT / "app/src/main/res/drawable"
|
|
HEADER = "<!-- Generated by scripts/gen-icons.py from the LLeMbas server's logo-mark.svg. Do not edit. -->"
|
|
|
|
|
|
def rects(svg: str) -> list[tuple[int, int, int, int, str]]:
|
|
found = []
|
|
for attrs in re.findall(r"<rect\s+([^>]*)/>", svg):
|
|
a = dict(re.findall(r'(\w+)="([^"]*)"', attrs))
|
|
found.append((int(a["x"]), int(a["y"]), int(a["width"]), int(a["height"]), a["fill"].upper()))
|
|
if not found:
|
|
sys.exit("gen-icons: no rects in the mark")
|
|
return found
|
|
|
|
|
|
# The app's sign: a phone, in the launcher only.
|
|
SIGN_COLOUR = "#C792EA"
|
|
SIGN = (
|
|
"..#####..",
|
|
"..#...#..",
|
|
"..#...#..",
|
|
"..#...#..",
|
|
"..#...#..",
|
|
"..#.#.#..",
|
|
"..#####..",
|
|
)
|
|
LEAF_AT = (2, 2)
|
|
SIGN_AT = (13, 14)
|
|
|
|
|
|
def sign() -> list[tuple[int, int, int, int, str]]:
|
|
cells = []
|
|
for y, row in enumerate(SIGN):
|
|
x = 0
|
|
while x < len(row):
|
|
if row[x] != "#":
|
|
x += 1
|
|
continue
|
|
w = 1
|
|
while x + w < len(row) and row[x + w] == "#":
|
|
w += 1
|
|
cells.append((x + SIGN_AT[0], y + SIGN_AT[1], w, 1, SIGN_COLOUR))
|
|
x += w
|
|
return cells
|
|
|
|
|
|
def path(cells) -> str:
|
|
return " ".join(f"M{x},{y}h{w}v{h}h-{w}z" for x, y, w, h, _ in cells)
|
|
|
|
|
|
def vector(size: int, viewport: int, body: str) -> str:
|
|
return (
|
|
f'<?xml version="1.0" encoding="utf-8"?>\n{HEADER}\n'
|
|
'<vector xmlns:android="http://schemas.android.com/apk/res/android"\n'
|
|
f' android:width="{size}dp" android:height="{size}dp"\n'
|
|
f' android:viewportWidth="{viewport}" android:viewportHeight="{viewport}">\n'
|
|
f"{body}</vector>\n"
|
|
)
|
|
|
|
|
|
def main() -> None:
|
|
ref = sys.argv[1] if len(sys.argv) > 1 else "HEAD"
|
|
svg = subprocess.run(
|
|
["git", "-C", str(SERVER), "show", f"{ref}:{SOURCE}"], capture_output=True, text=True, check=True
|
|
).stdout
|
|
cells = rects(svg)
|
|
by_colour: dict[str, list] = defaultdict(list)
|
|
for cell in cells:
|
|
by_colour[cell[4]].append(cell)
|
|
|
|
# 108dp adaptive canvas; the art is 24 pixels across, scaled x2.75 (66dp) and centred: the safe zone.
|
|
launcher = [(x + LEAF_AT[0], y + LEAF_AT[1], w, h, c) for x, y, w, h, c in cells] + sign()
|
|
launcher_by_colour: dict[str, list] = defaultdict(list)
|
|
for cell in launcher:
|
|
launcher_by_colour[cell[4]].append(cell)
|
|
coloured = "".join(
|
|
f' <path android:fillColor="{colour}" android:pathData="{path(group)}"/>\n'
|
|
for colour, group in sorted(launcher_by_colour.items())
|
|
)
|
|
placed = ' <group android:translateX="21" android:translateY="21" android:scaleX="2.75" android:scaleY="2.75">\n'
|
|
group = f"{placed}{coloured} </group>\n"
|
|
mono = f'{placed} <path android:fillColor="#FFFFFFFF" android:pathData="{path(launcher)}"/>\n </group>\n'
|
|
silhouette = f' <path android:fillColor="#FFFFFFFF" android:pathData="{path(cells)}"/>\n'
|
|
|
|
RES.mkdir(parents=True, exist_ok=True)
|
|
(RES / "ic_launcher_foreground.xml").write_text(vector(108, 108, group))
|
|
(RES / "ic_launcher_monochrome.xml").write_text(vector(108, 108, mono))
|
|
(RES / "ic_notification.xml").write_text(vector(24, 16, silhouette.replace(" ", " ")))
|
|
print(f"gen-icons: {len(cells)} cells in {len(by_colour)} colours, from {ref}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|