Branchwell

Graphley / app / installer / make-background.py

1"""Render Graphley's Finder installer background at 1x and 2x. Requires Pillow.
2
3The layout is the shared drag installer (Inkspring, OpenTalon, PulseCanvas) in Graphley's palette.
4Run it with the packaging Python and commit the result:
5
6 python installer/make-background.py
7
8The TIFF goes into the app's resources (Apps/Mac/installer-background.tiff, Mac builds only), so the
9installer window points at signed artwork inside Graphley.app and the disk image holds no loose background file.
10"""
11
12import math
13import subprocess
14from pathlib import Path
15
16from PIL import Image, ImageDraw, ImageFont
17
18APP = "Graphley"
19FONT = "/System/Library/Fonts/SFNS.ttf"
20BASE, GLOW = (230, 237, 235), (247, 250, 249)
21INK = "#0e2830"
22SECONDARY = "#557070"
23BORDER = "#c4d3d0"
24WELL = "#f7faf9"
25ACCENT = "#1d6e8a"
26DOT = "#5db6d2"
27
28
29def draw_arrow(canvas: Image.Image, scale: int) -> None:
30 # Finder can use the 1x TIFF page. Render the arrow at 4x before downsampling
31 # either page so the diagonals and their joins have smooth edge coverage.
32 # Drawing it directly at 1x is what leaves the arrow head ragged.
33 left, top, width, height = 315, 210, 89, 40
34 resolution = 4 * scale
35 mask = Image.new("L", (width * resolution, height * resolution))
36 draw = ImageDraw.Draw(mask)
37 radius = 1.5 * resolution
38 for path in ([(321, 230), (397, 230)], [(382, 215), (397, 230), (382, 245)]):
39 points = [((x - left) * resolution, (y - top) * resolution) for x, y in path]
40 draw.line(points, fill=255, width=3 * resolution)
41 for x, y in points:
42 draw.ellipse((x - radius, y - radius, x + radius, y + radius), fill=255)
43 coverage = mask.resize((width * scale, height * scale), Image.Resampling.LANCZOS)
44 canvas.paste(ACCENT, (left * scale, top * scale), coverage)
45
46
47def render(scale: int, target: Path) -> None:
48 # The shared layout is 720 x 460. The extra rows only continue the backdrop, so a Finder window
49 # with no status bar (492 points of content) shows no bare strip under the artwork.
50 width, height = 720 * scale, 492 * scale
51 canvas = Image.new("RGB", (width, height))
52 canvas.putdata([
53 tuple(round(start + (end - start) * amount) for start, end in zip(BASE, GLOW))
54 for y in range(height)
55 for x in range(width)
56 for amount in [math.exp(-(((x / scale - 360) / 410) ** 2
57 + ((y / scale - 230) / 300) ** 2))]
58 ])
59 draw = ImageDraw.Draw(canvas)
60
61 def points(*values: float) -> tuple[int, ...]:
62 return tuple(round(value * scale) for value in values)
63
64 def text(value: str, y: int, size: int, color: str, bold: bool = False) -> None:
65 font = ImageFont.truetype(FONT, size * scale)
66 font.set_variation_by_name("Bold" if bold else "Regular")
67 if draw.textlength(value, font=font) > 632 * scale:
68 raise ValueError(f"Installer text exceeds the safe width: {value}")
69 draw.text(points(360, y), value, font=font, fill=color, anchor="mt")
70
71 draw.rounded_rectangle(points(18, 18, 702, 442), radius=22 * scale, outline=BORDER, width=scale)
72 text(f"Install {APP}", 48, 30, INK, bold=True)
73 text(f"Drag {APP} to Applications to get started.", 95, 16, SECONDARY)
74 for left in (109, 439):
75 draw.rounded_rectangle(points(left, 154, left + 172, 320), radius=22 * scale, fill=WELL)
76
77 # Finder draws the real app and the Applications shortcut over the two wells.
78 left, top, right, bottom, radius = 102, 147, 288, 327, 29
79 dots = []
80 for start, end, fixed, horizontal in (
81 (left + radius, right - radius, top, True),
82 (left + radius, right - radius, bottom, True),
83 (top + radius, bottom - radius, left, False),
84 (top + radius, bottom - radius, right, False),
85 ):
86 count = round((end - start) / 9)
87 for index in range(count + 1):
88 value = start + (end - start) * index / count
89 dots.append((value, fixed) if horizontal else (fixed, value))
90 for cx, cy, start in ((left + radius, top + radius, 180),
91 (right - radius, top + radius, 270),
92 (right - radius, bottom - radius, 0),
93 (left + radius, bottom - radius, 90)):
94 for angle in range(start + 15, start + 90, 20):
95 dots.append((cx + radius * math.cos(math.radians(angle)),
96 cy + radius * math.sin(math.radians(angle))))
97 for x, y in dots:
98 draw.ellipse(points(x - 1.15, y - 1.15, x + 1.15, y + 1.15), fill=DOT)
99
100 draw_arrow(canvas, scale)
101 draw.line(points(88, 355, 632, 355), fill=BORDER, width=scale)
102 text("Eject the disk image when copying is complete.", 378, 15, INK)
103 text(f"Open {APP} from your Applications folder.", 410, 14, SECONDARY)
104 canvas.save(target, dpi=(72 * scale, 72 * scale))
105
106
107def main() -> None:
108 here = Path(__file__).resolve().parent
109 standard, retina = here / "background.png", here / "background@2x.png"
110 render(1, standard)
111 render(2, retina)
112 subprocess.run(["tiffutil", "-cathidpicheck", str(standard), str(retina),
113 "-out", str(here.parent / "Apps" / "Mac" / "installer-background.tiff")], check=True)
114
115
116if __name__ == "__main__":
117 main()