| 1 | #!/usr/bin/env python3 |
| 2 | """Unique placeholder block images for each planet color. |
| 3 | |
| 4 | Tim can replace any of these PNGs in build/images/. Filenames stay stable. |
| 5 | Run from the repo root: python3 scripts/gen_planet_blocks.py |
| 6 | """ |
| 7 | from __future__ import annotations |
| 8 | |
| 9 | import json |
| 10 | import math |
| 11 | from pathlib import Path |
| 12 | |
| 13 | from PIL import Image, ImageDraw, ImageFilter, ImageChops |
| 14 | |
| 15 | ROOT = Path(__file__).resolve().parents[1] |
| 16 | CATALOG = ROOT / 'server' / 'lib' / 'planets.json' |
| 17 | OUT = ROOT / 'build' / 'images' |
| 18 | SIZE = 64 |
| 19 | SCALE = 4 # supersample |
| 20 | SS = SIZE * SCALE |
| 21 | |
| 22 | |
| 23 | def hex_rgb(h): |
| 24 | h = h.lstrip('#') |
| 25 | return tuple(int(h[i:i + 2], 16) for i in (0, 2, 4)) |
| 26 | |
| 27 | |
| 28 | def lighten(rgb, f=0.35): |
| 29 | return tuple(min(255, int(c + (255 - c) * f)) for c in rgb) |
| 30 | |
| 31 | |
| 32 | def darken(rgb, f=0.28): |
| 33 | return tuple(max(0, int(c * (1 - f))) for c in rgb) |
| 34 | |
| 35 | |
| 36 | def regular_poly(cx, cy, r, n, rot=-math.pi / 2): |
| 37 | pts = [] |
| 38 | for i in range(n): |
| 39 | a = rot + i * 2 * math.pi / n |
| 40 | pts.append((cx + r * math.cos(a), cy + r * math.sin(a))) |
| 41 | return pts |
| 42 | |
| 43 | |
| 44 | def star_pts(cx, cy, r, n=5, inner=0.42, rot=-math.pi / 2): |
| 45 | pts = [] |
| 46 | for i in range(n * 2): |
| 47 | a = rot + i * math.pi / n |
| 48 | rr = r if i % 2 == 0 else r * inner |
| 49 | pts.append((cx + rr * math.cos(a), cy + rr * math.sin(a))) |
| 50 | return pts |
| 51 | |
| 52 | |
| 53 | def draw_shape(draw, shape, cx, cy, r, fill, outline, width): |
| 54 | shape = shape.lower() |
| 55 | if shape == 'circle': |
| 56 | box = [cx - r, cy - r, cx + r, cy + r] |
| 57 | draw.ellipse(box, fill=fill, outline=outline, width=width) |
| 58 | elif shape == 'square': |
| 59 | s = r * 0.86 |
| 60 | box = [cx - s, cy - s, cx + s, cy + s] |
| 61 | draw.rounded_rectangle(box, radius=r * 0.12, fill=fill, outline=outline, width=width) |
| 62 | elif shape == 'triangle': |
| 63 | pts = regular_poly(cx, cy + r * 0.08, r * 1.08, 3, -math.pi / 2) |
| 64 | draw.polygon(pts, fill=fill, outline=outline, width=width) |
| 65 | elif shape == 'diamond': |
| 66 | pts = regular_poly(cx, cy, r * 1.05, 4, 0) |
| 67 | draw.polygon(pts, fill=fill, outline=outline, width=width) |
| 68 | elif shape == 'star': |
| 69 | draw.polygon(star_pts(cx, cy, r * 1.12), fill=fill, outline=outline, width=width) |
| 70 | elif shape == 'hexagon': |
| 71 | draw.polygon(regular_poly(cx, cy, r * 1.05, 6, 0), fill=fill, outline=outline, width=width) |
| 72 | elif shape == 'pentagon': |
| 73 | draw.polygon(regular_poly(cx, cy, r * 1.08, 5), fill=fill, outline=outline, width=width) |
| 74 | elif shape == 'teardrop': |
| 75 | # circle + top point |
| 76 | pts = [] |
| 77 | for i in range(24): |
| 78 | a = math.pi * 0.22 + i * (2 * math.pi * 0.78) / 23 |
| 79 | pts.append((cx + r * 0.85 * math.cos(a), cy + r * 0.25 + r * 0.85 * math.sin(a))) |
| 80 | pts.insert(0, (cx, cy - r * 1.1)) |
| 81 | draw.polygon(pts, fill=fill, outline=outline, width=width) |
| 82 | elif shape == 'crescent': |
| 83 | # Draw a moon by punching a circle out of another. |
| 84 | # ImageDraw can't punch; caller uses a mask. Fallback: thick arc-like poly. |
| 85 | outer = [] |
| 86 | inner = [] |
| 87 | for i in range(22): |
| 88 | a = -math.pi * 0.7 + i * (1.4 * math.pi) / 21 |
| 89 | outer.append((cx + r * math.cos(a) - r * 0.08, cy + r * math.sin(a))) |
| 90 | ox, oy = cx + r * 0.42, cy - r * 0.05 |
| 91 | for i in range(18, -1, -1): |
| 92 | a = -math.pi * 0.65 + i * (1.3 * math.pi) / 18 |
| 93 | inner.append((ox + r * 0.72 * math.cos(a), oy + r * 0.72 * math.sin(a))) |
| 94 | draw.polygon(outer + inner, fill=fill, outline=outline, width=width) |
| 95 | elif shape == 'plus': |
| 96 | t = r * 0.38 |
| 97 | pts = [ |
| 98 | (cx - t, cy - r), (cx + t, cy - r), (cx + t, cy - t), |
| 99 | (cx + r, cy - t), (cx + r, cy + t), (cx + t, cy + t), |
| 100 | (cx + t, cy + r), (cx - t, cy + r), (cx - t, cy + t), |
| 101 | (cx - r, cy + t), (cx - r, cy - t), (cx - t, cy - t), |
| 102 | ] |
| 103 | draw.polygon(pts, fill=fill, outline=outline, width=width) |
| 104 | elif shape == 'gem': |
| 105 | pts = [ |
| 106 | (cx, cy - r), (cx + r * 0.72, cy - r * 0.15), |
| 107 | (cx + r * 0.5, cy + r * 0.95), (cx - r * 0.5, cy + r * 0.95), |
| 108 | (cx - r * 0.72, cy - r * 0.15), |
| 109 | ] |
| 110 | draw.polygon(pts, fill=fill, outline=outline, width=width) |
| 111 | elif shape == 'arrow': |
| 112 | pts = [ |
| 113 | (cx, cy - r * 1.1), (cx + r * 0.85, cy + r * 0.05), |
| 114 | (cx + r * 0.28, cy + r * 0.05), (cx + r * 0.28, cy + r), |
| 115 | (cx - r * 0.28, cy + r), (cx - r * 0.28, cy + r * 0.05), |
| 116 | (cx - r * 0.85, cy + r * 0.05), |
| 117 | ] |
| 118 | draw.polygon(pts, fill=fill, outline=outline, width=width) |
| 119 | else: |
| 120 | draw.ellipse([cx - r, cy - r, cx + r, cy + r], fill=fill, outline=outline, width=width) |
| 121 | |
| 122 | |
| 123 | def render_block(hex_color, shape): |
| 124 | rgb = hex_rgb(hex_color) |
| 125 | hi = lighten(rgb, 0.42) |
| 126 | lo = darken(rgb, 0.32) |
| 127 | img = Image.new('RGBA', (SS, SS), (0, 0, 0, 0)) |
| 128 | draw = ImageDraw.Draw(img) |
| 129 | cx = cy = SS / 2 |
| 130 | r = SS * 0.36 |
| 131 | # drop shadow |
| 132 | shadow = Image.new('RGBA', (SS, SS), (0, 0, 0, 0)) |
| 133 | sd = ImageDraw.Draw(shadow) |
| 134 | draw_shape(sd, shape, cx + SCALE * 1.2, cy + SCALE * 1.6, r, (0, 0, 0, 90), None, 0) |
| 135 | shadow = shadow.filter(ImageFilter.GaussianBlur(radius=SCALE * 1.1)) |
| 136 | img = Image.alpha_composite(img, shadow) |
| 137 | draw = ImageDraw.Draw(img) |
| 138 | # body |
| 139 | draw_shape(draw, shape, cx, cy, r, rgb + (255,), lo + (255,), max(2, SCALE)) |
| 140 | # highlight (smaller, offset, semi) |
| 141 | hi_layer = Image.new('RGBA', (SS, SS), (0, 0, 0, 0)) |
| 142 | hd = ImageDraw.Draw(hi_layer) |
| 143 | draw_shape(hd, shape, cx - r * 0.06, cy - r * 0.10, r * 0.55, hi + (110,), None, 0) |
| 144 | # clip highlight to the body roughly by multiplying alpha later is hard; |
| 145 | # a soft blob is fine for a placeholder. |
| 146 | hi_layer = hi_layer.filter(ImageFilter.GaussianBlur(radius=SCALE * 0.6)) |
| 147 | img = Image.alpha_composite(img, hi_layer) |
| 148 | out = img.resize((SIZE, SIZE), Image.Resampling.LANCZOS) |
| 149 | return out |
| 150 | |
| 151 | |
| 152 | def main(): |
| 153 | data = json.loads(CATALOG.read_text()) |
| 154 | OUT.mkdir(parents=True, exist_ok=True) |
| 155 | written = [] |
| 156 | seen = set() |
| 157 | for planet in data['planets']: |
| 158 | shapes = [c['shape'] for c in planet['colors']] |
| 159 | if len(set(shapes)) != 5: |
| 160 | raise SystemExit(f'{planet["id"]} does not have 5 unique shapes') |
| 161 | for color in planet['colors']: |
| 162 | path = ROOT / 'build' / color['image'] |
| 163 | if path.name in seen: |
| 164 | raise SystemExit(f'duplicate filename {path}') |
| 165 | seen.add(path.name) |
| 166 | img = render_block(color['hex'], color['shape']) |
| 167 | img.save(path, 'PNG', optimize=True) |
| 168 | written.append(path.relative_to(ROOT)) |
| 169 | print(f'wrote {len(written)} placeholders to {OUT}') |
| 170 | for p in written: |
| 171 | print(' ', p) |
| 172 | |
| 173 | |
| 174 | if __name__ == '__main__': |
| 175 | main() |
projects-timfalken-com meteors