#!/usr/bin/env python3 """Generates the raster sampler icons (run inside ../icons). Each icon is a round clock face on a rounded-square background, drawn to fill the exact pixel size of the file. The dial is a true circle inside the file, so any platform that stretches the bitmap to a square turns it into an ellipse and tilts the hands. """ from PIL import Image, ImageDraw SS = 8 # supersampling factor def clock_icon(width, height, bg, dial, hand, hour, minute): w, h = width * SS, height * SS image = Image.new("RGBA", (w, h), (0, 0, 0, 0)) draw = ImageDraw.Draw(image) radius = int(min(w, h) * 0.22) draw.rounded_rectangle([0, 0, w - 1, h - 1], radius=radius, fill=bg) # Dial: a circle sized off the smaller dimension, centered. d = int(min(w, h) * 0.72) cx, cy = w // 2, h // 2 box = [cx - d // 2, cy - d // 2, cx + d // 2, cy + d // 2] draw.ellipse(box, fill=dial, outline=hand, width=max(2, d // 28)) # Hour ticks. import math for i in range(12): angle = math.radians(i * 30) r_outer = d / 2 * 0.86 r_inner = d / 2 * (0.68 if i % 3 else 0.58) x1 = cx + r_inner * math.sin(angle) y1 = cy - r_inner * math.cos(angle) x2 = cx + r_outer * math.sin(angle) y2 = cy - r_outer * math.cos(angle) draw.line([x1, y1, x2, y2], fill=hand, width=max(2, d // (28 if i % 3 == 0 else 48))) # Hands. for value, per_turn, length, thickness in ((hour, 12, 0.42, 34), (minute, 60, 0.66, 48)): angle = math.radians(value * (360 / per_turn)) x = cx + d / 2 * length * math.sin(angle) y = cy - d / 2 * length * math.cos(angle) draw.line([cx, cy, x, y], fill=hand, width=max(2, d // thickness)) pin = max(3, d // 26) draw.ellipse([cx - pin, cy - pin, cx + pin, cy + pin], fill=hand) return image.resize((width, height), Image.LANCZOS) ICONS = [ # file, size, background, dial, hands/ticks, hour, minute ("icon-square-192.png", (192, 192), "#2E7D32", "#E8F5E9", "#1B5E20", 10, 10), ("icon-maskable-144x120.png", (144, 120), "#C62828", "#FFEBEE", "#7F1D1D", 3, 0), ("icon-any-160x120.png", (160, 120), "#1565C0", "#E3F2FD", "#0D3C73", 8, 20), ] for name, (width, height), bg, dial, hand, hour, minute in ICONS: clock_icon(width, height, bg, dial, hand, hour, minute).save(name) print(name, width, "x", height)