Branchwell

Jotley / app · jotley.ai / packaging / macos / generate-background.py

Raw 4.7 KB on 0004e76b60187af1b51e974d64db6d5531ee0ab1
1"""Render original installer artwork at 1x/2x. Requires Pillow, not used in CI."""
2from pathlib import Path
3import math
4import subprocess
5
6from PIL import Image, ImageDraw, ImageFont
7
8ROOT = Path(__file__).resolve().parent
9FONT = Path("/System/Library/Fonts/SFNS.ttf")
10
11
12def draw_arrow(canvas: Image.Image, scale: int) -> None:
13 # Supersample the stroke mask so the diagonal edges stay smooth at Finder's 1x size too.
14 left, top = 315, 210
15 width, height = 88, 40
16 resolution = 4 * scale
17 mask = Image.new("L", (width * resolution, height * resolution))
18 stroke = ImageDraw.Draw(mask)
19 shaft = [(321, 230), (397, 230)]
20 head = [(381, 215), (397, 230), (381, 245)]
21 for path in (shaft, head):
22 points = [((x - left) * resolution, (y - top) * resolution) for x, y in path]
23 stroke.line(points, fill=255, width=3 * resolution)
24 radius = 1.5 * resolution
25 for x, y in points:
26 stroke.ellipse((x - radius, y - radius, x + radius, y + radius), fill=255)
27 coverage = mask.resize((width * scale, height * scale), Image.Resampling.LANCZOS)
28 canvas.paste("#7C3AED", (left * scale, top * scale), coverage)
29
30
31def render(scale: int) -> None:
32 width, height = 720 * scale, 460 * scale
33 canvas = Image.new("RGB", (width, height))
34 pixels = canvas.load()
35 # A light lavender field with a softer center behind the installation path.
36 for y in range(height):
37 for x in range(width):
38 glow = math.exp(-(((x / scale - 360) / 410) ** 2
39 + ((y / scale - 240) / 300) ** 2))
40 pixels[x, y] = tuple(round(base + glow * lift)
41 for base, lift in zip((236, 227, 250), (14, 17, 5)))
42 draw = ImageDraw.Draw(canvas)
43
44 def box(bounds):
45 return tuple(round(value * scale) for value in bounds)
46
47 def text(value: str, y: int, size: int, color: str, weight: int = 400):
48 font = ImageFont.truetype(str(FONT), size * scale)
49 # SF's weight axis uses its own range. Select named instances when present.
50 names = font.get_variation_names()
51 desired = b"Bold" if weight >= 600 else b"Regular"
52 if desired in names:
53 font.set_variation_by_name(desired)
54 draw.text((360 * scale, y * scale), value, font=font,
55 fill=color, anchor="mt")
56
57 draw.rounded_rectangle(box((18, 18, 702, 442)), radius=22 * scale,
58 outline="#C4B5E8", width=scale)
59 text("Install Jotley", 48, 30, "#201739", 700)
60 text("Drag Jotley to Applications to get started.", 95, 16, "#4C3F62")
61
62 # The real draggable Finder icons are placed over these two quiet wells.
63 draw.rounded_rectangle(box((117, 154, 273, 316)), radius=22 * scale,
64 fill="#FCFAFF")
65 draw.rounded_rectangle(box((447, 154, 603, 316)), radius=22 * scale,
66 fill="#FCFAFF", outline="#C4B5E8", width=scale)
67 # Rounded dotted outline: evenly spaced dots along four sides and corner arcs.
68 left, top, right, bottom, radius = 110, 147, 280, 323, 29
69 dots = []
70 for start, stop, fixed, horizontal in (
71 (left + radius, right - radius, top, True),
72 (left + radius, right - radius, bottom, True),
73 (top + radius, bottom - radius, left, False),
74 (top + radius, bottom - radius, right, False),
75 ):
76 count = round((stop - start) / 9)
77 for index in range(count + 1):
78 value = start + (stop - start) * index / count
79 dots.append((value, fixed) if horizontal else (fixed, value))
80 for cx, cy, begin in ((left + radius, top + radius, 180),
81 (right - radius, top + radius, 270),
82 (right - radius, bottom - radius, 0),
83 (left + radius, bottom - radius, 90)):
84 for angle in range(begin + 15, begin + 90, 20):
85 dots.append((cx + radius * math.cos(math.radians(angle)),
86 cy + radius * math.sin(math.radians(angle))))
87 for x, y in dots:
88 draw.ellipse(box((x - 1.15, y - 1.15, x + 1.15, y + 1.15)), fill="#8B5CF6")
89
90 draw_arrow(canvas, scale)
91 draw.line(box((88, 355, 632, 355)), fill="#C4B5E8", width=scale)
92 text("Eject this disk image when copying is complete.", 378, 15, "#342846")
93 text("Open Jotley from your Applications folder.", 410, 14, "#4C3F62")
94 target = ROOT / ("background.png" if scale == 1 else "background@2x.png")
95 canvas.save(target, dpi=(72 * scale, 72 * scale))
96 print(target.name, canvas.size)
97
98
99if __name__ == "__main__":
100 render(1)
101 render(2)
102 subprocess.run(["tiffutil", "-cathidpicheck", str(ROOT / "background.png"),
103 str(ROOT / "background@2x.png"), "-out", str(ROOT / "background.tiff")],
104 check=True)