#!/usr/bin/env python3 """The app's icons, from the server's pixel leaf (`web/static/img/logo-mark.svg`). scripts/gen-icons.py [] (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 = "" def rects(svg: str) -> list[tuple[int, int, int, int, str]]: found = [] for attrs in re.findall(r"]*)/>", 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'\n{HEADER}\n' '\n' f"{body}\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' \n' for colour, group in sorted(launcher_by_colour.items()) ) placed = ' \n' group = f"{placed}{coloured} \n" mono = f'{placed} \n \n' silhouette = f' \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()