#!/usr/bin/env python3 """Rasterise the app icon (same geometry as public/icons/icon.svg) to PNGs, stdlib only. Usage: python3 -I scripts/make_icons.py Writes public/icons/icon-192.png, icon-512.png (rounded, transparent corners) and apple-touch-icon.png (180 px, full-bleed: iOS applies its own mask). """ import os import struct import zlib ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) BG, TEAL, ORANGE, LIGHT = (0x14, 0x20, 0x2B), (0x16, 0xA3, 0x94), (0xE8, 0x89, 0x3A), (0xF6, 0xF4, 0xEF) C1, C2, R = (24.3, 32.0), (39.7, 32.0), 15.4 # in a 64x64 design grid def colour_at(x, y, rounded): if rounded: # rounded rect, radius 14 cx, cy = min(max(x, 14), 50), min(max(y, 14), 50) if (x - cx) ** 2 + (y - cy) ** 2 > 14 ** 2: return None in1 = (x - C1[0]) ** 2 + (y - C1[1]) ** 2 <= R * R in2 = (x - C2[0]) ** 2 + (y - C2[1]) ** 2 <= R * R return LIGHT if in1 and in2 else ORANGE if in2 else TEAL if in1 else BG def render(size, rounded, ss=4): rows = [] for py in range(size): row = bytearray([0]) # PNG filter type 0 for px in range(size): acc, alpha = [0, 0, 0], 0 for sy in range(ss): for sx in range(ss): c = colour_at((px + (sx + .5) / ss) * 64 / size, (py + (sy + .5) / ss) * 64 / size, rounded) if c: alpha += 1 for i in range(3): acc[i] += c[i] n = ss * ss row += bytes([round(acc[i] / alpha) if alpha else 0 for i in range(3)] + [round(255 * alpha / n)]) rows.append(bytes(row)) def chunk(tag, data): return struct.pack(">I", len(data)) + tag + data + struct.pack(">I", zlib.crc32(tag + data) & 0xFFFFFFFF) ihdr = struct.pack(">IIBBBBB", size, size, 8, 6, 0, 0, 0) # 8-bit RGBA return b"\x89PNG\r\n\x1a\n" + chunk(b"IHDR", ihdr) + chunk(b"IDAT", zlib.compress(b"".join(rows), 9)) + chunk(b"IEND", b"") for name, size, rounded in (("icon-192.png", 192, True), ("icon-512.png", 512, True), ("apple-touch-icon.png", 180, False)): path = os.path.join(ROOT, "public", "icons", name) with open(path, "wb") as f: f.write(render(size, rounded)) print(path, os.path.getsize(path), "bytes")