| 1 | import argparse |
| 2 | import hashlib |
| 3 | import xml.etree.ElementTree as ET |
| 4 | from pathlib import Path |
| 5 | |
| 6 | from PIL import Image |
| 7 | |
| 8 | parser = argparse.ArgumentParser() |
| 9 | parser.add_argument("--output", type=Path) |
| 10 | group = parser.add_mutually_exclusive_group(required=True) |
| 11 | group.add_argument("--whale-body", action="store_true") |
| 12 | group.add_argument("--pixel-body", action="store_true") |
| 13 | args = parser.parse_args() |
| 14 | root = Path(__file__).resolve().parents[1] |
| 15 | if args.pixel_body: |
| 16 | source = root / "assets/pixel-whale.svg" |
| 17 | if hashlib.sha256(source.read_bytes()).hexdigest() != "9d3742fbf325aedc18435d7b15e89178b8684ccbb40cd55a32e60bed156de700": |
| 18 | raise ValueError("Expected the approved 192-cell Codewhale pixel whale") |
| 19 | step = 512 / 192 |
| 20 | candidates = [] |
| 21 | for rect in ET.fromstring(source.read_text()): |
| 22 | color = rect.attrib["fill"].lstrip("#") |
| 23 | rgb = tuple(int(color[i:i + 2], 16) for i in (0, 2, 4)) |
| 24 | for col in range(max(1, round(float(rect.attrib["width"]) / step))): |
| 25 | candidates.append((float(rect.attrib["x"]) + (col + .5) * step, |
| 26 | float(rect.attrib["y"]) + step / 2, *rgb)) |
| 27 | distances = [float("inf")] * len(candidates) |
| 28 | selected = [] |
| 29 | at = 0 |
| 30 | for _ in range(980): |
| 31 | x, y, *color = candidates[at] |
| 32 | selected.append((x, y, *color)) |
| 33 | distances = [min(d, (x - p[0]) ** 2 + (y - p[1]) ** 2) |
| 34 | for d, p in zip(distances, candidates, strict=True)] |
| 35 | at = max(range(len(distances)), key=distances.__getitem__) |
| 36 | output = args.output or root / "src/core/dot-pixel-body.ts" |
| 37 | lines = ["export const DOT_PIXEL_BODY: readonly (readonly [number, number, number, number, number])[] = ["] |
| 38 | lines.extend(f" [{(x - 256) / 512 * .878:.6f}, {(y - 256) / 512 * .878:.6f}, {r}, {g}, {b}]," for x, y, r, g, b in selected) |
| 39 | lines.append("];") |
| 40 | output.write_text("\n".join(lines) + "\n", encoding="utf-8") |
| 41 | print(f"Generated 980 drones from the approved 192-cell pixel whale: {output}") |
| 42 | raise SystemExit(0) |
| 43 | if args.whale_body: |
| 44 | source = root / "public/whale.png" |
| 45 | if hashlib.sha256(source.read_bytes()).hexdigest() != "dc180fe5bde033f55bed5c7c7c1ab81182f31dd5913df237fb23e2a2da5098df": |
| 46 | raise ValueError("Expected the reviewed filled Codewhale pet mark") |
| 47 | mask = Image.open(source).getchannel("A") |
| 48 | left, top, right, bottom = mask.getbbox() |
| 49 | candidates = [(x, y) for row, y in enumerate(range(top + 4, bottom, 8)) |
| 50 | for x in range(left + 4 + (row % 2) * 4, right, 8) |
| 51 | if mask.getpixel((x, y)) >= 128] |
| 52 | distances = [float("inf")] * len(candidates) |
| 53 | selected = [] |
| 54 | at = 0 |
| 55 | for _ in range(980): |
| 56 | x, y = candidates[at] |
| 57 | selected.append((x, y)) |
| 58 | distances = [min(d, (x - px) ** 2 + (y - py) ** 2) |
| 59 | for d, (px, py) in zip(distances, candidates, strict=True)] |
| 60 | at = max(range(len(distances)), key=distances.__getitem__) |
| 61 | scale = .878 / (right - left) |
| 62 | cx, cy = (left + right) / 2, (top + bottom) / 2 |
| 63 | output = args.output or root / "src/core/dot-body.ts" |
| 64 | lines = ["export const DOT_BODY: readonly (readonly [number, number])[] = ["] |
| 65 | lines.extend(f" [{(x - cx) * scale:.6f}, {(y - cy) * scale:.6f}]," for x, y in selected) |
| 66 | lines.append("]; ") |
| 67 | output.write_text("\n".join(lines).rstrip() + "\n", encoding="utf-8") |
| 68 | print(f"Generated 980 interior dots from the filled pet mark: {output}") |
| 69 | raise SystemExit(0) |
| 70 |