"""Genera los iconos PWA (PNG) sin dependencias más allá de numpy. Uso: uv run --project backend python scripts/generar_iconos.py """ import struct import zlib from pathlib import Path import numpy as np DIR_ICONS = Path(__file__).resolve().parents[1] / "web" / "icons" def escribir_png(ruta: Path, rgb: np.ndarray) -> None: """Escribe un PNG RGB 8-bit (sin filtros) usando solo stdlib.""" alto, ancho, _ = rgb.shape def chunk(tipo: bytes, datos: bytes) -> bytes: cuerpo = tipo + datos return struct.pack(">I", len(datos)) + cuerpo + struct.pack(">I", zlib.crc32(cuerpo)) filas = b"".join(b"\x00" + rgb[y].tobytes() for y in range(alto)) ruta.write_bytes( b"\x89PNG\r\n\x1a\n" + chunk(b"IHDR", struct.pack(">IIBBBBB", ancho, alto, 8, 2, 0, 0, 0)) + chunk(b"IDAT", zlib.compress(filas, 9)) + chunk(b"IEND", b"") ) def icono(n: int) -> np.ndarray: """Dos anillos entrelazados (L teal, R ámbar) sobre fondo oscuro.""" y, x = np.mgrid[0:n, 0:n] / n img = np.zeros((n, n, 3)) img[:] = np.array([13, 17, 23]) / 255 # --fondo def anillo(cx: float, cy: float, r: float, grosor: float, color: tuple) -> np.ndarray: d = np.hypot(x - cx, y - cy) return np.exp(-(((d - r) / grosor) ** 2))[..., None] * (np.array(color) / 255) img += anillo(0.40, 0.5, 0.24, 0.02, (88, 196, 220)) # oído izquierdo · alpha img += anillo(0.60, 0.5, 0.24, 0.02, (240, 168, 60)) # oído derecho · beta d_centro = np.hypot(x - 0.5, y - 0.5) img += (np.exp(-((d_centro / 0.09) ** 2)) * 0.30)[..., None] # batido al centro return (np.clip(img, 0, 1) * 255).astype(np.uint8) if __name__ == "__main__": DIR_ICONS.mkdir(parents=True, exist_ok=True) for lado in (512, 192, 180): escribir_png(DIR_ICONS / f"icono-{lado}.png", icono(lado)) print(f"icono-{lado}.png")