-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-icons.py
More file actions
70 lines (63 loc) · 2.17 KB
/
Copy pathgenerate-icons.py
File metadata and controls
70 lines (63 loc) · 2.17 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
#!/usr/bin/env python3
"""
generate-icons.py
Run this once to create the PWA icon PNGs.
Usage: python3 generate-icons.py
"""
import struct, zlib, math, os
def make_png(size, bg_color, text_color):
pixels = []
cx, cy = size // 2, size // 2
r = size // 2
for y in range(size):
row = []
for x in range(size):
dx, dy = x - cx, y - cy
dist = math.sqrt(dx*dx + dy*dy)
if dist > r - 0.5:
row.extend([0, 0, 0, 0])
continue
br, bg, bb = bg_color
margin = int(size * 0.28)
stroke = max(2, int(size * 0.09))
mid_y = int(size * 0.45)
top_y = int(size * 0.25)
bot_y = int(size * 0.75)
right_x = int(size * 0.68)
on_f = False
if margin <= x <= margin + stroke and top_y <= y <= bot_y:
on_f = True
if top_y <= y <= top_y + stroke and margin <= x <= right_x:
on_f = True
if mid_y <= y <= mid_y + stroke and margin <= x <= int(size * 0.60):
on_f = True
if on_f:
tr, tg, tb = text_color
row.extend([tr, tg, tb, 255])
else:
row.extend([br, bg, bb, 255])
pixels.append(row)
def chunk(tag, data):
c = zlib.crc32(tag + data) & 0xffffffff
return struct.pack('>I', len(data)) + tag + data + struct.pack('>I', c)
raw = b''
for row in pixels:
raw += b'\x00' + bytes(row)
compressed = zlib.compress(raw, 9)
png = b'\x89PNG\r\n\x1a\n'
png += chunk(b'IHDR', struct.pack('>IIBBBBB', size, size, 8, 6, 0, 0, 0))
png += chunk(b'IDAT', compressed)
png += chunk(b'IEND', b'')
return png
script_dir = os.path.dirname(os.path.abspath(__file__))
icons_dir = os.path.join(script_dir, 'icons')
os.makedirs(icons_dir, exist_ok=True)
bg = (123, 140, 255)
fg = (255, 255, 255)
for sz, name in [(192, 'icon-192.png'), (512, 'icon-512.png')]:
data = make_png(sz, bg, fg)
path = os.path.join(icons_dir, name)
with open(path, 'wb') as f:
f.write(data)
print(f'Created {name} ({len(data):,} bytes)')
print('Done!')