BlueGit

projects-timfalken-com meteors

Files

meteors / scripts / make-placeholders.py

make-placeholders.py

make-placeholders.py 6.2 KiB Download Raw
1#!/usr/bin/env python3
2"""Generate unique placeholder block sprites Tim can later replace."""
3from __future__ import annotations
4
5import math
6from pathlib import Path
7
8from PIL import Image, ImageDraw, ImageFilter
9
10ROOT = Path(__file__).resolve().parents[1]
11OUT = ROOT / "build" / "images"
12SIZE = 64
13
14
15def canvas(color=(0, 0, 0, 0)) -> Image.Image:
16 return Image.new("RGBA", (SIZE, SIZE), color)
17
18
19def shade(hex_color: str, factor: float) -> tuple[int, int, int, int]:
20 h = hex_color.lstrip("#")
21 r, g, b = int(h[0:2], 16), int(h[2:4], 16), int(h[4:6], 16)
22 r = max(0, min(255, int(r * factor)))
23 g = max(0, min(255, int(g * factor)))
24 b = max(0, min(255, int(b * factor)))
25 return (r, g, b, 255)
26
27
28def disc(draw: ImageDraw.ImageDraw, cx, cy, r, fill, outline=None, width=2):
29 box = [cx - r, cy - r, cx + r, cy + r]
30 draw.ellipse(box, fill=fill, outline=outline, width=width if outline else 0)
31
32
33def regular_polygon(cx, cy, r, n, rotation_deg=0):
34 pts = []
35 rot = math.radians(rotation_deg - 90)
36 for i in range(n):
37 a = rot + 2 * math.pi * i / n
38 pts.append((cx + r * math.cos(a), cy + r * math.sin(a)))
39 return pts
40
41
42def star_points(cx, cy, r_out, r_in, n=5, rotation_deg=0):
43 pts = []
44 rot = math.radians(rotation_deg - 90)
45 for i in range(n * 2):
46 r = r_out if i % 2 == 0 else r_in
47 a = rot + math.pi * i / n
48 pts.append((cx + r * math.cos(a), cy + r * math.sin(a)))
49 return pts
50
51
52def bevel_shape(base: Image.Image) -> Image.Image:
53 """Soft inner highlight and drop shadow, keeping alpha."""
54 alpha = base.getchannel("A")
55 shadow = Image.new("RGBA", base.size, (0, 0, 0, 0))
56 sh = Image.new("L", base.size, 0)
57 sh.paste(alpha, (2, 3))
58 sh = sh.filter(ImageFilter.GaussianBlur(2))
59 shadow.putalpha(sh.point(lambda v: int(v * 0.45)))
60 out = Image.alpha_composite(shadow, base)
61 # top-left highlight
62 hi = Image.new("RGBA", base.size, (0, 0, 0, 0))
63 hid = ImageDraw.Draw(hi)
64 hid.ellipse((8, 6, 30, 24), fill=(255, 255, 255, 70))
65 hi.putalpha(ImageChops.multiply(hi.getchannel("A"), alpha))
66 out = Image.alpha_composite(out, hi)
67 return out
68
69
70from PIL import ImageChops # after use in bevel — move to top conceptually, fine at runtime
71
72
73def draw_block(shape: str, color: str) -> Image.Image:
74 img = canvas()
75 body = canvas()
76 d = ImageDraw.Draw(body)
77 cx = cy = SIZE / 2
78 fill = shade(color, 1.0)
79 dark = shade(color, 0.55)
80 light = shade(color, 1.25)
81 if shape == "triangle":
82 pts = regular_polygon(cx, cy + 3, 26, 3, 0)
83 d.polygon(pts, fill=fill, outline=dark)
84 elif shape == "circle":
85 disc(d, cx, cy, 25, fill, dark, 3)
86 disc(d, cx - 6, cy - 7, 8, (255, 255, 255, 70), None)
87 elif shape == "square":
88 r = 12
89 d.rounded_rectangle((10, 10, 54, 54), radius=r, fill=fill, outline=dark, width=3)
90 elif shape == "diamond":
91 pts = regular_polygon(cx, cy, 27, 4, 0)
92 d.polygon(pts, fill=fill, outline=dark)
93 elif shape == "star":
94 pts = star_points(cx, cy, 28, 13, 5, 0)
95 d.polygon(pts, fill=fill, outline=dark)
96 else:
97 d.rounded_rectangle((8, 8, 56, 56), 10, fill=fill, outline=dark, width=3)
98 # inner edge
99 if shape != "circle":
100 inner = body.filter(ImageFilter.SMOOTH_MORE)
101 body = inner
102 return bevel_shape(body)
103
104
105def draw_gray(locked=False) -> Image.Image:
106 img = canvas()
107 d = ImageDraw.Draw(img)
108 fill = (48, 50, 56, 255) if not locked else (28, 28, 32, 255)
109 dark = (20, 20, 24, 255)
110 d.rounded_rectangle((8, 8, 56, 56), 10, fill=fill, outline=dark, width=3)
111 # rivets
112 for x, y in ((16, 16), (48, 16), (16, 48), (48, 48)):
113 disc(d, x, y, 3, (70, 72, 78, 255) if not locked else (16, 16, 18, 255))
114 if locked:
115 # a muted cross
116 d.line((20, 20, 44, 44), fill=(90, 40, 40, 220), width=5)
117 d.line((44, 20, 20, 44), fill=(90, 40, 40, 220), width=5)
118 else:
119 # engine nozzle hint at the bottom
120 d.rounded_rectangle((24, 50, 40, 58), 3, fill=(24, 24, 28, 255))
121 return bevel_shape(img)
122
123
124def draw_flame_strip() -> Image.Image:
125 frames = 4
126 strip = Image.new("RGBA", (SIZE * frames, SIZE), (0, 0, 0, 0))
127 for i in range(frames):
128 fr = canvas()
129 d = ImageDraw.Draw(fr)
130 # Flicker: length and lean change per frame
131 lean = (-1, 1, 0, -0.5)[i] * 4
132 length = (26, 30, 22, 28)[i]
133 cx = SIZE / 2 + lean
134 top = 8
135 bot = 8 + length
136 # outer, mid, core
137 pts_outer = [(cx - 14, top), (cx + 14, top), (cx + lean * 0.3, bot)]
138 pts_mid = [(cx - 9, top + 2), (cx + 9, top + 2), (cx + lean * 0.2, bot - 4)]
139 pts_core = [(cx - 4, top + 3), (cx + 4, top + 3), (cx, bot - 10)]
140 d.polygon(pts_outer, fill=(255, 110, 20, 230))
141 d.polygon(pts_mid, fill=(255, 180, 40, 240))
142 d.polygon(pts_core, fill=(255, 250, 200, 255))
143 fr = fr.filter(ImageFilter.GaussianBlur(0.6))
144 strip.paste(fr, (i * SIZE, 0), fr)
145 return strip
146
147
148def draw_cell() -> Image.Image:
149 img = Image.new("RGBA", (SIZE, SIZE), (18, 22, 36, 255))
150 d = ImageDraw.Draw(img)
151 d.rectangle((0, 0, 63, 63), outline=(36, 44, 70, 255), width=1)
152 d.rectangle((1, 1, 62, 62), outline=(12, 14, 24, 200), width=1)
153 return img
154
155
156def draw_pixel() -> Image.Image:
157 return Image.new("RGBA", (1, 1), (0, 0, 0, 0))
158
159
160def main():
161 OUT.mkdir(parents=True, exist_ok=True)
162 blocks = {
163 "block-ember.png": ("triangle", "#e85d3a"),
164 "block-aqua.png": ("circle", "#3db8e8"),
165 "block-leaf.png": ("square", "#4cbf4c"),
166 "block-sun.png": ("diamond", "#f0c53a"),
167 "block-orchid.png": ("star", "#c45ad4"),
168 }
169 for name, (shape, color) in blocks.items():
170 draw_block(shape, color).save(OUT / name, "PNG")
171 print("wrote", name)
172 draw_gray(False).save(OUT / "block-gray.png", "PNG")
173 print("wrote block-gray.png")
174 draw_gray(True).save(OUT / "block-locked.png", "PNG")
175 print("wrote block-locked.png")
176 draw_flame_strip().save(OUT / "flame.png", "PNG")
177 print("wrote flame.png")
178 draw_cell().save(OUT / "cell.png", "PNG")
179 print("wrote cell.png")
180 draw_pixel().save(OUT / "pixel.png", "PNG")
181 print("wrote pixel.png")
182
183
184if __name__ == "__main__":
185 main()