62 lines
2.4 KiB
Python
62 lines
2.4 KiB
Python
|
|
"""Generate the multi-resolution Windows icon without third-party packages.
|
||
|
|
|
||
|
|
The geometry mirrors icons/cmvr-app.svg. Run after changing the logo.
|
||
|
|
"""
|
||
|
|
import math
|
||
|
|
import pathlib
|
||
|
|
import struct
|
||
|
|
import zlib
|
||
|
|
|
||
|
|
|
||
|
|
def chunk(kind, data):
|
||
|
|
return struct.pack('>I', len(data)) + kind + data + struct.pack('>I', zlib.crc32(kind + data))
|
||
|
|
|
||
|
|
|
||
|
|
def color_at(x, y):
|
||
|
|
# Rounded blue app tile, a white C, and a cyan status dot.
|
||
|
|
dx, dy = max(abs(x - 32) - 15, 0), max(abs(y - 32) - 15, 0)
|
||
|
|
if dx * dx + dy * dy > 15 * 15:
|
||
|
|
return (0, 0, 0, 0)
|
||
|
|
color = (27, 110, 243, 255)
|
||
|
|
radius = math.hypot(x - 32, y - 32)
|
||
|
|
angle = abs(math.atan2(y - 32, x - 32))
|
||
|
|
if 13.5 <= radius <= 20.5 and angle >= math.radians(40):
|
||
|
|
color = (255, 255, 255, 255)
|
||
|
|
for angle in (-40, 40):
|
||
|
|
cx, cy = 32 + 17 * math.cos(math.radians(angle)), 32 + 17 * math.sin(math.radians(angle))
|
||
|
|
if math.hypot(x - cx, y - cy) <= 3.5:
|
||
|
|
color = (255, 255, 255, 255)
|
||
|
|
dx, dy = max(abs(x - 47.5) - 1.5, 0), max(abs(y - 32.5) - 1.5, 0)
|
||
|
|
if dx * dx + dy * dy <= 9:
|
||
|
|
color = (143, 221, 255, 255)
|
||
|
|
return color
|
||
|
|
|
||
|
|
|
||
|
|
def png(size):
|
||
|
|
rows = bytearray()
|
||
|
|
samples = 4
|
||
|
|
for row in range(size):
|
||
|
|
rows.append(0)
|
||
|
|
for col in range(size):
|
||
|
|
colors = [color_at((col + (sx + .5) / samples) * 64 / size,
|
||
|
|
(row + (sy + .5) / samples) * 64 / size)
|
||
|
|
for sy in range(samples) for sx in range(samples)]
|
||
|
|
alpha = sum(c[3] for c in colors)
|
||
|
|
rgb = [round(sum(c[k] * c[3] for c in colors) / alpha) if alpha else 0 for k in range(3)]
|
||
|
|
rows.extend(rgb + [round(alpha / (samples * samples))])
|
||
|
|
return (b'\x89PNG\r\n\x1a\n' + chunk(b'IHDR', struct.pack('>IIBBBBB', size, size, 8, 6, 0, 0, 0))
|
||
|
|
+ chunk(b'IDAT', zlib.compress(rows, 9)) + chunk(b'IEND', b''))
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == '__main__':
|
||
|
|
sizes = (16, 24, 32, 48, 64, 128, 256)
|
||
|
|
images = [png(size) for size in sizes]
|
||
|
|
offset = 6 + 16 * len(images)
|
||
|
|
directory = bytearray(struct.pack('<HHH', 0, 1, len(images)))
|
||
|
|
for size, data in zip(sizes, images):
|
||
|
|
directory.extend(struct.pack('<BBBBHHII', size % 256, size % 256, 0, 0, 1, 32, len(data), offset))
|
||
|
|
offset += len(data)
|
||
|
|
target = pathlib.Path(__file__).resolve().parents[1] / 'icons' / 'cmvr-app.ico'
|
||
|
|
target.write_bytes(directory + b''.join(images))
|
||
|
|
print(target)
|