Repository navigation
Expand file tree
/
Copy pathgenerate_app_icon.py
More file actions
287 lines (239 loc) · 9.13 KB
/
Copy pathgenerate_app_icon.py
File metadata and controls
287 lines (239 loc) · 9.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#!/usr/bin/env python3
"""Generate Meter's macOS app icon — the "Aurora Ring".
A bold ~72% progress ring (green -> amber -> red, mirroring the app's live
menu-bar glyph) glowing on a deep-graphite Apple squircle. Renders one
supersampled master and downscales to every size the asset catalog needs.
Usage: python3 Scripts/generate_app_icon.py
Requires: Pillow (pip install pillow)
Writes:
Sources/Meter/Resources/Assets.xcassets/AppIcon.appiconset/*.png + Contents.json
docs/icon.png (1024px master, for the README)
All design values are constants below — tweak and re-run.
"""
from __future__ import annotations
import math
from pathlib import Path
from PIL import Image, ImageChops, ImageDraw, ImageFilter
# ---------------------------------------------------------------------------
# Design constants (expressed in a 1024pt logical canvas; SS supersamples them)
# ---------------------------------------------------------------------------
LOGICAL = 1024
SS = 4 # supersample factor -> 4096px master
SIZE = LOGICAL * SS
MARGIN = 100 # transparent margin around the squircle (for shadow)
BODY = LOGICAL - 2 * MARGIN # 824 — Apple's Big Sur icon-grid body size
SQUIRCLE_N = 5.0 # superellipse exponent (continuous "squircle" corner)
CENTER = LOGICAL / 2
RING_RADIUS = 300 # centerline radius of the ring
RING_STROKE = 86 # ring thickness
PROGRESS = 0.72 # fraction filled
START_DEG = -90.0 # 12 o'clock
GAP_DEG = 3.0 # tiny gap so the round caps read as caps
# Palette — mirrors ProviderPalette.threshold in the app so icon == live meter.
GRAPHITE_TOP = (0x2B, 0x2F, 0x38)
GRAPHITE_BOT = (0x12, 0x14, 0x19)
GREEN = (0x2F, 0xB1, 0x70)
AMBER = (0xE0, 0xA1, 0x00)
RED = (0xE0, 0x50, 0x3A)
STOPS = [(0.0, GREEN), (0.55, AMBER), (1.0, RED)]
TRACK_RGBA = (255, 255, 255, 38) # muted unfilled remainder
GLOW_ALPHA = 0.45
GLOW_BLUR = 50 # bloom softness (render px)
GLOW_STROKE_MULT = 1.5
SHADOW_ALPHA = 90
SHADOW_BLUR = 34 * SS // 2
SHADOW_DY = 16
SHEEN_ALPHA = 34 # top-lit sheen strength (0-255)
SHEEN_SPAN = 0.46 # fades out by this fraction of body height
SHEEN_FALLOFF = 1.7
REPO = Path(__file__).resolve().parent.parent
ICONSET = REPO / "Sources/Meter/Resources/Assets.xcassets/AppIcon.appiconset"
DOCS = REPO / "docs"
# filename -> pixel size
OUTPUTS = [
("icon_16x16.png", 16), ("icon_16x16@2x.png", 32),
("icon_32x32.png", 32), ("icon_32x32@2x.png", 64),
("icon_128x128.png", 128), ("icon_128x128@2x.png", 256),
("icon_256x256.png", 256), ("icon_256x256@2x.png", 512),
("icon_512x512.png", 512), ("icon_512x512@2x.png", 1024),
]
CONTENTS_JSON = """{
"images" : [
{
"filename" : "icon_16x16.png",
"idiom" : "mac",
"scale" : "1x",
"size" : "16x16"
},
{
"filename" : "icon_16x16@2x.png",
"idiom" : "mac",
"scale" : "2x",
"size" : "16x16"
},
{
"filename" : "icon_32x32.png",
"idiom" : "mac",
"scale" : "1x",
"size" : "32x32"
},
{
"filename" : "icon_32x32@2x.png",
"idiom" : "mac",
"scale" : "2x",
"size" : "32x32"
},
{
"filename" : "icon_128x128.png",
"idiom" : "mac",
"scale" : "1x",
"size" : "128x128"
},
{
"filename" : "icon_128x128@2x.png",
"idiom" : "mac",
"scale" : "2x",
"size" : "128x128"
},
{
"filename" : "icon_256x256.png",
"idiom" : "mac",
"scale" : "1x",
"size" : "256x256"
},
{
"filename" : "icon_256x256@2x.png",
"idiom" : "mac",
"scale" : "2x",
"size" : "256x256"
},
{
"filename" : "icon_512x512.png",
"idiom" : "mac",
"scale" : "1x",
"size" : "512x512"
},
{
"filename" : "icon_512x512@2x.png",
"idiom" : "mac",
"scale" : "2x",
"size" : "512x512"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
"""
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
def px(v: float) -> float:
"""Logical units -> render pixels."""
return v * SS
def lerp(a: float, b: float, t: float) -> float:
return a + (b - a) * t
def color_at(t: float) -> tuple[int, int, int]:
"""Interpolate the green->amber->red gradient at t in [0, 1]."""
t = max(0.0, min(1.0, t))
for (t0, c0), (t1, c1) in zip(STOPS, STOPS[1:]):
if t <= t1:
f = 0.0 if t1 == t0 else (t - t0) / (t1 - t0)
return tuple(round(lerp(c0[i], c1[i], f)) for i in range(3))
return STOPS[-1][1]
def superellipse(cx: float, cy: float, r: float, n: float, steps: int = 2048):
"""Parametric superellipse (squircle) polygon points, in render px."""
pts = []
for i in range(steps):
th = 2 * math.pi * i / steps
ct, st = math.cos(th), math.sin(th)
x = cx + r * math.copysign(abs(ct) ** (2 / n), ct)
y = cy + r * math.copysign(abs(st) ** (2 / n), st)
pts.append((x, y))
return pts
def vertical_gradient(h: int, top: tuple, bot: tuple) -> Image.Image:
"""A 1px-wide vertical RGB gradient, stretched to SIZE x SIZE."""
strip = Image.new("RGB", (1, h))
for y in range(h):
t = y / (h - 1)
strip.putpixel((0, y), tuple(round(lerp(top[i], bot[i], t)) for i in range(3)))
return strip.resize((SIZE, SIZE))
def stamp_arc(layer: Image.Image, radius: float, start_deg: float,
sweep_deg: float, stroke: float, color_fn, step_deg: float = 0.35):
"""Draw an arc by stamping overlapping filled circles -> smooth rounded
caps and an optional per-position color gradient in a single pass."""
draw = ImageDraw.Draw(layer)
cx = cy = px(CENTER)
r = px(radius)
half = px(stroke) / 2
n = max(1, int(abs(sweep_deg) / step_deg))
for i in range(n + 1):
t = i / n
ang = math.radians(start_deg + sweep_deg * t)
x = cx + r * math.cos(ang)
y = cy + r * math.sin(ang)
draw.ellipse([x - half, y - half, x + half, y + half], fill=color_fn(t))
def scale_alpha(img: Image.Image, factor: float) -> Image.Image:
r, g, b, a = img.split()
return Image.merge("RGBA", (r, g, b, a.point(lambda v: int(v * factor))))
# ---------------------------------------------------------------------------
# Compose the master
# ---------------------------------------------------------------------------
def render_master() -> Image.Image:
canvas = Image.new("RGBA", (SIZE, SIZE), (0, 0, 0, 0))
# Squircle mask (255 inside the body).
sq_pts = superellipse(px(CENTER), px(CENTER), px(BODY) / 2, SQUIRCLE_N)
mask = Image.new("L", (SIZE, SIZE), 0)
ImageDraw.Draw(mask).polygon(sq_pts, fill=255)
# 1. Ambient drop shadow beneath the squircle.
shadow = Image.new("RGBA", (SIZE, SIZE), (0, 0, 0, 0))
ImageDraw.Draw(shadow).polygon(sq_pts, fill=(0, 0, 0, SHADOW_ALPHA))
shadow = shadow.filter(ImageFilter.GaussianBlur(SHADOW_BLUR))
shadow = ImageChops.offset(shadow, 0, int(px(SHADOW_DY)))
canvas.alpha_composite(shadow)
# 2. Graphite body.
body = vertical_gradient(SIZE, GRAPHITE_TOP, GRAPHITE_BOT).convert("RGBA")
body.putalpha(mask)
canvas.alpha_composite(body)
# 3. Top-lit sheen.
ramp = Image.new("L", (1, SIZE), 0)
span = SIZE * SHEEN_SPAN
for y in range(SIZE):
f = max(0.0, 1.0 - y / span) ** SHEEN_FALLOFF
ramp.putpixel((0, y), int(SHEEN_ALPHA * f))
sheen_a = ImageChops.multiply(ramp.resize((SIZE, SIZE)), mask)
sheen = Image.new("RGBA", (SIZE, SIZE), (255, 255, 255, 0))
sheen.putalpha(sheen_a)
canvas.alpha_composite(sheen)
# 4/5/6. Ring: track, aurora glow, crisp progress — clipped to the squircle.
ring = Image.new("RGBA", (SIZE, SIZE), (0, 0, 0, 0))
stamp_arc(ring, RING_RADIUS, 0, 360, RING_STROKE, lambda t: TRACK_RGBA)
sweep = PROGRESS * 360 - GAP_DEG
glow = Image.new("RGBA", (SIZE, SIZE), (0, 0, 0, 0))
stamp_arc(glow, RING_RADIUS, START_DEG, sweep,
RING_STROKE * GLOW_STROKE_MULT, lambda t: (*color_at(t), 255))
glow = scale_alpha(glow.filter(ImageFilter.GaussianBlur(GLOW_BLUR)), GLOW_ALPHA)
ring.alpha_composite(glow)
stamp_arc(ring, RING_RADIUS, START_DEG, sweep,
RING_STROKE, lambda t: (*color_at(t), 255))
r, g, b, a = ring.split()
ring = Image.merge("RGBA", (r, g, b, ImageChops.multiply(a, mask)))
canvas.alpha_composite(ring)
return canvas
def main() -> None:
ICONSET.mkdir(parents=True, exist_ok=True)
DOCS.mkdir(parents=True, exist_ok=True)
print(f"Rendering {SIZE}x{SIZE} master…")
master = render_master()
for filename, size in OUTPUTS:
img = master.resize((size, size), Image.LANCZOS)
img.save(ICONSET / filename)
print(f" {filename} ({size}px)")
(ICONSET / "Contents.json").write_text(CONTENTS_JSON)
print(f" Contents.json")
master.resize((1024, 1024), Image.LANCZOS).save(DOCS / "icon.png")
print(f"Wrote {DOCS / 'icon.png'}")
print("Done. Run `xcodegen generate` to wire the icon into the build.")
if __name__ == "__main__":
main()