| 1 | """Generate the favicon, app icons and social preview image. |
| 2 | |
| 3 | python3 apps/diffinery/scripts/make-icons.py |
| 4 | |
| 5 | Writes apps/diffinery/src/web/icons.generated.ts (base64 PNG/ICO strings, served by |
| 6 | src/web/seo.ts). Needs Pillow and the Inter font (fonts-inter). Re-run after changing the |
| 7 | mark; the output is committed so builds don't need either. |
| 8 | """ |
| 9 | import base64 |
| 10 | import io |
| 11 | import os |
| 12 | from PIL import Image, ImageDraw, ImageFont |
| 13 | |
| 14 | ORANGE = (246, 130, 31, 255) |
| 15 | WHITE = (255, 255, 255, 255) |
| 16 | INK = (24, 24, 27, 255) |
| 17 | HERE = os.path.dirname(os.path.abspath(__file__)) |
| 18 | OUT = os.path.join(HERE, "..", "src", "web", "icons.generated.ts") |
| 19 | |
| 20 | # The mark (src/web/assets.ts ICONS.logo) on a 24-unit grid. |
| 21 | CIRCLES = [(6, 6), (6, 18), (18, 12)] |
| 22 | LINES = [((6, 8.5), (6, 15.5))] |
| 23 | CURVES = [((8.3, 7.2), (12.3, 8.2), (15.7, 9.4), (15.9, 10.5)), ((8.3, 16.8), (12.3, 15.8), (15.7, 14.6), (15.9, 13.5))] |
| 24 | |
| 25 | |
| 26 | def bezier(p0, p1, p2, p3, n=64): |
| 27 | pts = [] |
| 28 | for i in range(n + 1): |
| 29 | t = i / n |
| 30 | a, b, c, d = (1 - t) ** 3, 3 * (1 - t) ** 2 * t, 3 * (1 - t) * t * t, t ** 3 |
| 31 | pts.append((a * p0[0] + b * p1[0] + c * p2[0] + d * p3[0], a * p0[1] + b * p1[1] + c * p2[1] + d * p3[1])) |
| 32 | return pts |
| 33 | |
| 34 | |
| 35 | def draw_mark(d, ox, oy, unit, color, stroke=2.2): |
| 36 | w = stroke * unit |
| 37 | tx = lambda p: (ox + p[0] * unit, oy + p[1] * unit) |
| 38 | |
| 39 | def thick(points): |
| 40 | # Stamp round dots along the path: wide PIL polylines leave seams on curves. |
| 41 | pts = [tx(p) for p in points] |
| 42 | for (x0, y0), (x1, y1) in zip(pts, pts[1:]): |
| 43 | n = max(1, int(((x1 - x0) ** 2 + (y1 - y0) ** 2) ** 0.5 / max(1, w / 8))) |
| 44 | for i in range(n + 1): |
| 45 | x, y = x0 + (x1 - x0) * i / n, y0 + (y1 - y0) * i / n |
| 46 | d.ellipse([x - w / 2, y - w / 2, x + w / 2, y + w / 2], fill=color) |
| 47 | |
| 48 | for cx, cy in CIRCLES: |
| 49 | c = tx((cx, cy)) |
| 50 | r = 2.5 * unit |
| 51 | d.ellipse([c[0] - r - w / 2, c[1] - r - w / 2, c[0] + r + w / 2, c[1] + r + w / 2], outline=color, width=max(1, round(w))) |
| 52 | for a, b in LINES: |
| 53 | thick([a, b]) |
| 54 | for c in CURVES: |
| 55 | thick(bezier(*c)) |
| 56 | |
| 57 | |
| 58 | def icon(size, radius=0.22, glyph=0.8, bg=ORANGE): |
| 59 | """Square icon: rounded orange tile with the white mark. radius is a fraction of the size.""" |
| 60 | s = 8 # supersampling |
| 61 | big = size * s |
| 62 | img = Image.new("RGBA", (big, big), (0, 0, 0, 0)) |
| 63 | d = ImageDraw.Draw(img) |
| 64 | d.rounded_rectangle([0, 0, big - 1, big - 1], radius=round(radius * big), fill=bg) |
| 65 | unit = big * glyph / 24 |
| 66 | off = (big - 24 * unit) / 2 |
| 67 | draw_mark(d, off, off, unit, WHITE) |
| 68 | return img.resize((size, size), Image.LANCZOS) |
| 69 | |
| 70 | |
| 71 | def social(): |
| 72 | s = 2 |
| 73 | w, h = 1200 * s, 630 * s |
| 74 | img = Image.new("RGBA", (w, h), INK) |
| 75 | d = ImageDraw.Draw(img) |
| 76 | tile = icon(220 * s) |
| 77 | img.alpha_composite(tile, (100 * s, (h - tile.height) // 2)) |
| 78 | title = ImageFont.truetype("/usr/share/fonts/opentype/inter/Inter-Bold.otf", 112 * s) |
| 79 | sub = ImageFont.truetype("/usr/share/fonts/opentype/inter/Inter-Medium.otf", 40 * s) |
| 80 | x = 370 * s |
| 81 | d.text((x, 205 * s), "Diffinery", font=title, fill=WHITE) |
| 82 | lines = ["Code hosting and CI for the agentic era.", "GitHub Actions compatible, on Cloudflare."] |
| 83 | for i, line in enumerate(lines): |
| 84 | d.text((x, (360 + i * 56) * s), line, font=sub, fill=(212, 212, 216, 255)) |
| 85 | d.text((x, 500 * s), "diffinery.com", font=sub, fill=ORANGE) |
| 86 | return img.resize((1200, 630), Image.LANCZOS).convert("RGB") |
| 87 | |
| 88 | |
| 89 | def png(img): |
| 90 | b = io.BytesIO() |
| 91 | img.save(b, "PNG", optimize=True) |
| 92 | return b.getvalue() |
| 93 | |
| 94 | |
| 95 | def ico(): |
| 96 | b = io.BytesIO() |
| 97 | big = icon(256) |
| 98 | big.save(b, "ICO", sizes=[(16, 16), (32, 32), (48, 48), (64, 64)]) |
| 99 | return b.getvalue() |
| 100 | |
| 101 | |
| 102 | assets = { |
| 103 | "favicon.ico": ico(), |
| 104 | "favicon-48.png": png(icon(48)), |
| 105 | "favicon-96.png": png(icon(96)), |
| 106 | "apple-touch-icon.png": png(icon(180, radius=0).convert("RGB")), |
| 107 | "icon-192.png": png(icon(192)), |
| 108 | "icon-512.png": png(icon(512)), |
| 109 | "icon-maskable-512.png": png(icon(512, radius=0, glyph=0.62)), |
| 110 | "og-image.png": png(social()), |
| 111 | } |
| 112 | |
| 113 | with open(OUT, "w") as f: |
| 114 | f.write("// Generated by scripts/make-icons.py. Do not edit.\n") |
| 115 | f.write("export const ICON_FILES: Record<string, string> = {\n") |
| 116 | for name, data in assets.items(): |
| 117 | f.write(f' "{name}": "{base64.b64encode(data).decode()}",\n') |
| 118 | f.write("};\n") |
| 119 | for name, data in assets.items(): |
| 120 | print(f"{name}: {len(data)} bytes") |