64 lines
1.5 KiB
Python
64 lines
1.5 KiB
Python
import struct
|
|
import zlib
|
|
import os
|
|
|
|
# Minimal 256x256 RGBA PNG: a violet rounded square with a white "spark".
|
|
W = H = 256
|
|
BG = (124, 58, 237, 255) # violet-600
|
|
FG = (255, 255, 255, 255)
|
|
TRANSPARENT = (0, 0, 0, 0)
|
|
|
|
|
|
def inside_rounded(x, y, r=44):
|
|
if r <= x < W - r or r <= y < H - r:
|
|
return 0 <= x < W and 0 <= y < H
|
|
cx = r if x < r else W - 1 - r
|
|
cy = r if y < r else H - 1 - r
|
|
return (x - cx) ** 2 + (y - cy) ** 2 <= r * r
|
|
|
|
|
|
def spark(x, y):
|
|
"""A four-point star centred in the tile."""
|
|
cx, cy = W / 2, H / 2
|
|
dx, dy = abs(x - cx), abs(y - cy)
|
|
# diamond body
|
|
if dx + dy < 62:
|
|
return True
|
|
# thin cross arms
|
|
if dx < 9 and dy < 104:
|
|
return True
|
|
if dy < 9 and dx < 104:
|
|
return True
|
|
return False
|
|
|
|
|
|
rows = []
|
|
for y in range(H):
|
|
row = bytearray([0]) # PNG filter type 0
|
|
for x in range(W):
|
|
if not inside_rounded(x, y):
|
|
row.extend(TRANSPARENT)
|
|
elif spark(x, y):
|
|
row.extend(FG)
|
|
else:
|
|
row.extend(BG)
|
|
rows.append(bytes(row))
|
|
|
|
raw = b"".join(rows)
|
|
|
|
|
|
def chunk(tag, data):
|
|
return (struct.pack(">I", len(data)) + tag + data
|
|
+ struct.pack(">I", zlib.crc32(tag + data) & 0xFFFFFFFF))
|
|
|
|
|
|
png = (b"\x89PNG\r\n\x1a\n"
|
|
+ chunk(b"IHDR", struct.pack(">IIBBBBB", W, H, 8, 6, 0, 0, 0))
|
|
+ chunk(b"IDAT", zlib.compress(raw, 9))
|
|
+ chunk(b"IEND", b""))
|
|
|
|
out = os.path.join(os.path.dirname(os.path.abspath(__file__)), "icon.png")
|
|
with open(out, "wb") as fh:
|
|
fh.write(png)
|
|
print("written", out, len(png), "bytes")
|