#!/usr/bin/env python3 """Draw the record page's ENAMEL OBJECTS — the one style every object on a card is made in. python3 -m venv /tmp/enamel && /tmp/enamel/bin/pip install pillow /tmp/enamel/bin/python scripts/gen-cards/make_enamel_objects.py [--only clock bell …] [--sheet ] WHY: the architect's ruling for the record page's cards — every object on a card is a generated asset in ONE style, "a small flat enamel object on the card's paper, centred, no photographic paper or tear-off stationery". So these are drawn, not photographed: each object is a few vector shapes read like an enamel lapel pin — flat enamel fills with a quiet second tone along each field's lower right, one thin warm brass rim of the same width round every field, at most one soft white gloss streak (always upper left, the one light), no drop shadow, no texture, no text, no letters, no numbers. The colours are the house palette's calm enamels (OBJECTS below), muted enough for the pastel stocks (cream, sage, rose, slate, tan, grey) and dark enough to read on all of them. The owner's word for the cards — "the UI is aesthetic first … flat and pastel means drawn with the same care, not bare" — is the bar: one family, one rim, one weight, one light. WHAT IS LEFT BLANK, AND WHY: `clock` and `snooze` carry ticks but no hands, because the time is the record's value and the app draws the hands live; a hand baked in would be the same wrong time on every card. Their dial centres and the reach a hand may have are printed when they are drawn (fractions of the square canvas). Writes `apps/loom/Sources/CottonPaper/Media.xcassets/object-.imageset` — object-.png (120 px, 1x), @2x (240 px) and @3x (360 px) on a transparent square, centred, scaled so 8 % air (9.5 % round a whole disc) is left on the tightest side, plus Contents.json. Every shape is drawn at 6x the 3x size (2160 px) and reduced with a premultiplied Lanczos filter, so edges are clean at every scale. Deterministic: the same script writes the same bytes. `--sheet` also writes a contact sheet of every object at 128, 64 and 40 pt on each of the six card stocks, to judge them at card size. Pillow (and nothing else) — it is not installed system-wide, hence the venv above. """ import argparse import json import math import pathlib from PIL import Image, ImageChops, ImageDraw, ImageFilter ROOT = pathlib.Path(__file__).resolve().parents[2] CATALOGUE = ROOT / "apps/loom/Sources/CottonPaper/Media.xcassets" SS = 2160 # the supersampled canvas every object is drawn on SCALES = (("1x", "", 120), ("2x", "@2x", 240), ("3x", "@3x", 360)) PAD = 0.08 # the air round the object's bounding box, on its tightest side DISC_PAD = 0.095 # … more round a whole disc, which reads larger than a square of the same width RIM = 0.021 # the brass rim round every enamel field, as a fraction of the canvas SHADE = 0.034 # how far the second tone reaches in from a field's lower right edge GLOSS_W = 0.026 # the one gloss streak's width GLOSS_A = 0.50 # … and its opacity (0..1) # The enamels. Calm, muted, and each one clear on every card stock. INK = "#2E2B28" RED = "#A8322D" BRASS = "#B8924A" FOREST = "#4E6B4A" NAVY = "#2F4A6E" OCHRE = "#CC9C3A" CREAM = "#F6EEE1" CREAM_DEEP = "#E6D8C1" TAN = "#CDA872" SAGE = "#93A687" SKY = "#BFD0DE" BLUE = "#7F9AB6" GREY = "#A39C91" PINK = "#DA9C93" STEEL = "#8F8A83" WOOD = "#EBD3A8" METAL = BRASS # the rim # The stocks a card is printed on (`Media.xcassets/stock-*`), for the contact sheet only. PAPERS = (("cream", "#F0E5D8"), ("sage", "#E1E2D4"), ("rose", "#F1D8D5"), ("slate", "#DEE5ED"), ("tan", "#EBD8C3"), ("grey", "#E6E4DE")) # ---------------------------------------------------------------- geometry (unit square, y down, degrees clockwise) def arc(cx, cy, r, a0, a1, n=None): n = n or max(8, int(abs(a1 - a0) / 1.5)) return [(cx + r * math.cos(math.radians(a0 + (a1 - a0) * i / n)), cy + r * math.sin(math.radians(a0 + (a1 - a0) * i / n))) for i in range(n + 1)] def cubic(p0, p1, p2, p3, n=40): out = [] for i in range(n + 1): t = i / n a, b, c, d = (1 - t) ** 3, 3 * (1 - t) ** 2 * t, 3 * (1 - t) * t * t, t ** 3 out.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])) return out def path(start, *segments): """A run of cubic segments, each (control, control, end).""" pts = [start] for c1, c2, end in segments: pts += cubic(pts[-1], c1, c2, end)[1:] return pts def round_poly(pts, r, n=10): """Round every corner of a closed polygon with radius about r.""" out = [] count = len(pts) for i in range(count): prev, cur, nxt = pts[i - 1], pts[i], pts[(i + 1) % count] def toward(p, q, d): length = math.dist(p, q) k = min(d, length / 2) / length return (p[0] + (q[0] - p[0]) * k, p[1] + (q[1] - p[1]) * k) a, b = toward(cur, prev, r), toward(cur, nxt, r) for j in range(n + 1): t = j / n out.append(((1 - t) ** 2 * a[0] + 2 * (1 - t) * t * cur[0] + t * t * b[0], (1 - t) ** 2 * a[1] + 2 * (1 - t) * t * cur[1] + t * t * b[1])) return out def rrect(x0, y0, x1, y1, r): return round_poly([(x0, y0), (x1, y0), (x1, y1), (x0, y1)], r) def rotate(pts, deg, about=(0.5, 0.5)): c, s = math.cos(math.radians(deg)), math.sin(math.radians(deg)) return [(about[0] + (x - about[0]) * c - (y - about[1]) * s, about[1] + (x - about[0]) * s + (y - about[1]) * c) for x, y in pts] def move(pts, dx, dy): return [(x + dx, y + dy) for x, y in pts] def star_points(cx, cy, outer, inner, tips, start=-90): pts = [] for i in range(tips * 2): r = outer if i % 2 == 0 else inner a = math.radians(start + i * 180 / tips) pts.append((cx + r * math.cos(a), cy + r * math.sin(a))) return pts # Parts: ("poly", points) a filled polygon · ("line", points, width) a round-capped stroke · ("disc", (x, y), r) def poly(pts): return ("poly", pts) def line(pts, w): return ("line", pts, w) def disc(cx, cy, r): return ("disc", (cx, cy), r) def field(fill, *parts, rim=True, shade=None, clip=None): """One enamel field: its parts share one fill and one rim. A field without a rim is a detail laid on enamel.""" return {"fill": fill, "parts": parts, "rim": rim, "shade": rim if shade is None else shade, "clip": clip} def arrow(pts, w, head_len, head_half): """A stroke along pts ending in a straight arrowhead whose tip is pts[-1].""" (x0, y0), (x1, y1) = pts[-2], pts[-1] length = math.dist((x0, y0), (x1, y1)) ux, uy = (x1 - x0) / length, (y1 - y0) / length bx, by = x1 - ux * head_len, y1 - uy * head_len shaft = pts[:-1] + [(bx + ux * head_len * 0.2, by + uy * head_len * 0.2)] head = [(x1, y1), (bx - uy * head_half, by + ux * head_half), (bx + uy * head_half, by - ux * head_half)] return [line(shaft, w), poly(head)] def arc_arrow(cx, cy, r, a0, a1, w, head_len, head_half): """A stroke along a circle from a0 to a1 (clockwise when a1 > a0) whose head bends with the circle.""" sign = 1 if a1 > a0 else -1 shaft = arc(cx, cy, r, a0, a1 + sign * 4) a = math.radians(a1) tip = math.radians(a1 + sign * math.degrees(head_len / r)) head = [(cx + r * math.cos(tip), cy + r * math.sin(tip)), (cx + (r + head_half) * math.cos(a), cy + (r + head_half) * math.sin(a)), (cx + (r - head_half) * math.cos(a), cy + (r - head_half) * math.sin(a))] return [line(shaft, w), poly(head)] def gloss_arc(cx, cy, r, a0=203, a1=243): return arc(cx, cy, r, a0, a1) # ---------------------------------------------------------------- the objects ARROW_W = 0.105 # every arrow's shaft, so the arrows read as one set HEAD_LEN = 0.17 HEAD_HALF = 0.135 def tray(x0, x1, top, bottom, behind): """The open tray `move`, `share` and `restore` stand on: its dark opening, what rises out of it, its front.""" inset = (x1 - x0) * 0.07 opening = field(INK, poly(round_poly([(x0 + inset, top - 0.075), (x1 - inset, top - 0.075), (x1, top), (x0, top)], 0.02)), shade=False) front = field(TAN, poly(round_poly([(x0, top), (x1, top), (x1 - inset, bottom), (x0 + inset, bottom)], 0.04))) return [opening, *behind, front] def clock(): ticks = [] for i in range(12): a = math.radians(i * 30 - 90) r_in = 0.228 if i % 3 == 0 else 0.255 ticks.append(field(INK, line([(0.5 + r_in * math.cos(a), 0.5 + r_in * math.sin(a)), (0.5 + 0.292 * math.cos(a), 0.5 + 0.292 * math.sin(a))], 0.03 if i % 3 == 0 else 0.02), rim=False)) return { "fields": [field(BRASS, disc(0.5, 0.5, 0.40)), field(CREAM, disc(0.5, 0.5, 0.335)), *ticks], "gloss": (gloss_arc(0.5, 0.5, 0.368), BRASS), "pad": DISC_PAD, "dial": ((0.5, 0.5), 0.215), } def bell(): body = path((0.5, 0.20), ((0.615, 0.20), (0.675, 0.27), (0.68, 0.39)), ((0.685, 0.54), (0.715, 0.635), (0.80, 0.685)), ((0.5, 0.685), (0.5, 0.685), (0.20, 0.685)), ((0.285, 0.635), (0.315, 0.54), (0.32, 0.39)), ((0.325, 0.27), (0.385, 0.20), (0.5, 0.20))) return { "fields": [field(OCHRE, disc(0.5, 0.165, 0.055)), field(INK, disc(0.5, 0.79, 0.075)), field(OCHRE, poly(body)), field(OCHRE, poly(rrect(0.15, 0.655, 0.85, 0.755, 0.05)))], "gloss": (cubic((0.405, 0.29), (0.375, 0.36), (0.365, 0.45), (0.365, 0.55)), OCHRE), } def repeat(): r = 0.28 return { "fields": [field(NAVY, *arc_arrow(0.5, 0.5, r, 197, 317, ARROW_W, 0.15, 0.115)), field(OCHRE, *arc_arrow(0.5, 0.5, r, 17, 137, ARROW_W, 0.15, 0.115))], "gloss": (arc(0.5, 0.5, r + 0.012, 212, 252), NAVY), } def pin(): xs = (0.11, 0.373, 0.627, 0.89) top = (0.645, 0.60, 0.645, 0.60) bottom = (0.885, 0.84, 0.885, 0.84) def band(t0, t1): upper = [(x, a + (b - a) * t0) for x, a, b in zip(xs, top, bottom)] lower = [(x, a + (b - a) * t1) for x, a, b in zip(xs, top, bottom)] return poly(upper + lower[::-1]) folds = [field(METAL, line([(x, a), (x, b)], RIM * 1.1), rim=False) for x, a, b in zip(xs[1:3], top[1:3], bottom[1:3])] cx, cy, r, tip = 0.5, 0.30, 0.185, (0.5, 0.715) beta = math.degrees(math.asin(r / (tip[1] - cy))) drop = [tip] + arc(cx, cy, r, 90 - (90 - beta), 90 - (90 - beta) - 360 + 2 * (90 - beta)) return { "fields": [field(CREAM, poly(round_poly(list(zip(xs, top)) + list(zip(xs, bottom))[::-1], 0.012))), field(SAGE, band(0.50, 0.68), rim=False), field(BLUE, band(0.20, 0.30), rim=False), *folds, field(RED, poly(drop)), field(CREAM, disc(cx, cy, 0.068))], "gloss": (gloss_arc(cx, cy, r - 0.042, 200, 245), RED), } def flame(): # A tall tongue leaning right with a smaller tongue on its left shoulder; an ochre heart low inside it. outer = path((0.57, 0.08), ((0.64, 0.22), (0.80, 0.35), (0.80, 0.57)), ((0.80, 0.78), (0.67, 0.91), (0.50, 0.91)), ((0.32, 0.91), (0.20, 0.78), (0.20, 0.61)), ((0.20, 0.49), (0.235, 0.40), (0.285, 0.32)), ((0.30, 0.39), (0.335, 0.435), (0.385, 0.45)), ((0.395, 0.31), (0.50, 0.21), (0.57, 0.08))) inner = path((0.555, 0.41), ((0.60, 0.50), (0.665, 0.58), (0.665, 0.70)), ((0.665, 0.80), (0.595, 0.86), (0.50, 0.86)), ((0.41, 0.86), (0.335, 0.80), (0.335, 0.71)), ((0.335, 0.62), (0.39, 0.575), (0.435, 0.545)), ((0.43, 0.60), (0.445, 0.63), (0.47, 0.645)), ((0.475, 0.55), (0.52, 0.49), (0.555, 0.41))) return { "fields": [field(RED, poly(outer)), field(OCHRE, poly(inner))], "gloss": (cubic((0.275, 0.52), (0.26, 0.57), (0.255, 0.62), (0.265, 0.68)), RED), } def sheet_lines(x0, ys, x1s, w=0.048): return [field(GREY, line([(x0, y), (x1, y)], w), rim=False) for y, x1 in zip(ys, x1s)] def file(): sheet = round_poly([(0.20, 0.09), (0.60, 0.09), (0.80, 0.29), (0.80, 0.91), (0.20, 0.91)], 0.045) return { "fields": [field(CREAM, poly(sheet)), field(CREAM_DEEP, poly(round_poly([(0.60, 0.09), (0.60, 0.29), (0.80, 0.29)], 0.025))), *sheet_lines(0.32, (0.47, 0.60, 0.73), (0.68, 0.68, 0.55))], "gloss": None, } def picture(): back = [(0.20, 0.73), (0.20, 0.60)] + path((0.20, 0.60), ((0.29, 0.50), (0.39, 0.485), (0.51, 0.575)), ((0.60, 0.64), (0.68, 0.69), (0.72, 0.73)))[1:] front = path((0.44, 0.73), ((0.53, 0.62), (0.65, 0.565), (0.80, 0.575)))[:] + [(0.80, 0.73)] return { "fields": [field(CREAM, poly(rrect(0.10, 0.17, 0.90, 0.83, 0.045))), field(SKY, poly(rrect(0.20, 0.27, 0.80, 0.73, 0.012))), field(OCHRE, disc(0.645, 0.415, 0.065)), field(SAGE, poly(back)), field(FOREST, poly(front))], "gloss": None, } def done(): return { "fields": [field(INK, disc(0.5, 0.5, 0.40)), field(CREAM, line([(0.32, 0.515), (0.443, 0.638), (0.685, 0.388)], 0.09), shade=False)], "gloss": (gloss_arc(0.5, 0.5, 0.345), INK), "pad": DISC_PAD, } def snooze(): cx, cy, r = 0.5, 0.555, 0.30 bells = [] for side in (-1, 1): a = math.radians(-90 + side * 44) bx, by = cx + (r - 0.01) * math.cos(a), cy + (r - 0.01) * math.sin(a) dome = arc(0, 0, 0.115, 180, 360) dome = [(bx + x * math.cos(math.radians(side * 44)) - y * math.sin(math.radians(side * 44)), by + x * math.sin(math.radians(side * 44)) + y * math.cos(math.radians(side * 44))) for x, y in dome] bells.append(field(OCHRE, poly(dome))) ticks = [] for i in range(4): a = math.radians(i * 90 - 90) ticks.append(field(INK, line([(cx + 0.165 * math.cos(a), cy + 0.165 * math.sin(a)), (cx + 0.2 * math.cos(a), cy + 0.2 * math.sin(a))], 0.03), rim=False)) return { "fields": [field(INK, line([(0.335, 0.80), (0.27, 0.885)], 0.06)), field(INK, line([(0.665, 0.80), (0.73, 0.885)], 0.06)), *bells, field(OCHRE, disc(cx, cy - r - 0.035, 0.04)), field(NAVY, disc(cx, cy, r)), field(CREAM, disc(cx, cy, 0.232)), *ticks], "gloss": (gloss_arc(cx, cy, 0.267), NAVY), "dial": ((cx, cy), 0.15), } def move_(): shaft = [(0.36, 0.70), (0.36, 0.47)] + arc(0.47, 0.47, 0.11, 180, 270)[1:] + [(0.90, 0.36)] return { "fields": tray(0.10, 0.60, 0.635, 0.885, [field(NAVY, *arrow(shaft, ARROW_W, HEAD_LEN, HEAD_HALF))]), "gloss": ([(0.50, 0.334), (0.66, 0.334)], NAVY), } def share(): return { "fields": tray(0.20, 0.80, 0.60, 0.895, [field(NAVY, *arrow([(0.5, 0.72), (0.5, 0.10)], ARROW_W, HEAD_LEN, HEAD_HALF))]), "gloss": ([(0.476, 0.36), (0.476, 0.50)], NAVY), } def restore(): shaft = [(0.64, 0.72), (0.64, 0.40)] + arc(0.47, 0.40, 0.17, 0, -90)[1:] + [(0.16, 0.23)] return { "fields": tray(0.22, 0.86, 0.635, 0.895, [field(NAVY, *arrow(shaft, ARROW_W, HEAD_LEN, HEAD_HALF))]), "gloss": ([(0.616, 0.42), (0.616, 0.55)], NAVY), } def four_point(cx, cy, r, pinch=0.16): quarter = cubic((0, -r), (r * pinch, -r * pinch * 1.1), (r * pinch * 1.1, -r * pinch), (r, 0), 30) pts = [] for k in range(4): c, s = [(1, 0), (0, 1), (-1, 0), (0, -1)][k] pts += [(cx + x * c - y * s, cy + x * s + y * c) for x, y in quarter[:-1]] return pts def agent(): return { "fields": [field(NAVY, poly(four_point(0.46, 0.54, 0.37))), field(OCHRE, poly(four_point(0.79, 0.21, 0.12)))], "gloss": (cubic((0.425, 0.30), (0.415, 0.36), (0.405, 0.42), (0.39, 0.47)), NAVY), } def bin_(): body = round_poly([(0.22, 0.31), (0.78, 0.31), (0.72, 0.905), (0.28, 0.905)], 0.05) ribs = [field(METAL, line([(a, 0.43), (b, 0.80)], 0.03), rim=False) for a, b in ((0.385, 0.405), (0.5, 0.5), (0.615, 0.595))] return { "fields": [field(INK, line([(0.40, 0.22), (0.40, 0.135), (0.60, 0.135), (0.60, 0.22)], 0.05)), field(INK, poly(body)), *ribs, field(INK, poly(rrect(0.15, 0.20, 0.85, 0.315, 0.04)))], "gloss": ([(0.29, 0.40), (0.31, 0.70)], INK), } def archive(): return { "fields": [field(TAN, poly(rrect(0.15, 0.38, 0.85, 0.88, 0.045))), field(OCHRE, poly(rrect(0.11, 0.19, 0.89, 0.395, 0.05))), field(CREAM, poly(rrect(0.36, 0.52, 0.64, 0.67, 0.03)))], "gloss": ([(0.19, 0.255), (0.40, 0.255)], OCHRE), } def open_(): return { "fields": [field(CREAM, poly(rrect(0.11, 0.29, 0.71, 0.89, 0.07))), field(NAVY, *arrow([(0.33, 0.67), (0.89, 0.11)], ARROW_W, HEAD_LEN + 0.02, HEAD_HALF))], "gloss": None, } def stadium(cx, cy, length, width, deg): pts = rrect(cx - length / 2, cy - width / 2, cx + length / 2, cy + width / 2, width / 2 - 0.0001) return rotate(pts + [pts[0]], deg, (cx, cy)) def link(): d, length, width, w = 0.12, 0.50, 0.26, 0.072 a = stadium(0.5 - d, 0.5 + d, length, width, -45) b = stadium(0.5 + d, 0.5 - d, length, width, -45) # The ends overlap and cross at two points either side of the links' axis. Upper left of the axis A is drawn # again over B, so A passes over B there and under it at the lower right: the two are interlocked. The axis # crosses each link where the other is not, so the cut never shows. over = poly([(-1, -1), (2, -1), (-1, 2)]) return { "fields": [field(NAVY, line(a, w)), field(OCHRE, line(b, w)), field(NAVY, line(a, w), clip=[over])], "gloss": None, } def person(): shoulders = path((0.20, 0.90), ((0.21, 0.67), (0.35, 0.585), (0.5, 0.585)), ((0.65, 0.585), (0.79, 0.67), (0.80, 0.90))) + [(0.80, 0.98), (0.20, 0.98)] return { "fields": [field(NAVY, disc(0.5, 0.5, 0.40)), field(CREAM, disc(0.5, 0.395, 0.13)), field(CREAM, poly(shoulders), clip=[disc(0.5, 0.5, 0.40)])], "gloss": (gloss_arc(0.5, 0.5, 0.35, 200, 236), NAVY), "pad": DISC_PAD, } def edit(): h = 0.088 def place(pts): return poly(rotate(move(pts, 0.5, 0.5), 135)) return { "fields": [field(PINK, place(round_poly([(-0.50, -h), (-0.33, -h), (-0.33, h), (-0.50, h)], 0.06))), field(METAL, place([(-0.35, -h), (-0.245, -h), (-0.245, h), (-0.35, h)])), field(OCHRE, place([(-0.255, -h), (0.215, -h), (0.215, h), (-0.255, h)])), field(WOOD, place([(0.205, -h), (0.50, 0), (0.205, h)])), field(INK, place([(0.385, -0.034), (0.50, 0), (0.385, 0.034)]))], "gloss": (rotate(move([(-0.20, 0.046), (0.15, 0.046)], 0.5, 0.5), 135), OCHRE), } def phone(): # A handset drawn lying flat (back arched up, the two cups flaring down), then turned so the ear is at the # upper left, the mouth at the lower right and the cups face the upper right. handle = arc(0.5, 0.98, 0.50, 240, 300) left = round_poly([(0.13, 0.47), (0.37, 0.50), (0.36, 0.72), (0.06, 0.67)], 0.065) right = [(1 - x, y) for x, y in left] turn = -135 parts = [line(handle, 0.16), poly(left), poly(right)] parts = [(p[0], rotate(p[1], turn), *p[2:]) for p in parts] return { "fields": [field(INK, *parts)], "gloss": (rotate([(0.845, 0.54), (0.88, 0.635)], turn), INK), } def message(): bubble = rrect(0.10, 0.17, 0.90, 0.70, 0.17) tail = round_poly([(0.25, 0.62), (0.45, 0.66), (0.20, 0.86)], 0.02) return { "fields": [field(CREAM, poly(bubble), poly(tail)), *[field(INK, disc(x, 0.435, 0.047), rim=False) for x in (0.32, 0.5, 0.68)]], "gloss": None, } def mail(): return { "fields": [field(CREAM, poly(rrect(0.09, 0.21, 0.91, 0.79, 0.05))), field(METAL, line([(0.12, 0.765), (0.43, 0.50)], RIM * 1.1), rim=False), field(METAL, line([(0.88, 0.765), (0.57, 0.50)], RIM * 1.1), rim=False), field(CREAM_DEEP, poly(round_poly([(0.10, 0.225), (0.90, 0.225), (0.5, 0.555)], 0.03))), field(RED, disc(0.5, 0.54, 0.078))], "gloss": (gloss_arc(0.5, 0.54, 0.05, 195, 250), RED), } def star(): return { "fields": [field(OCHRE, poly(round_poly(star_points(0.5, 0.535, 0.43, 0.205, 5), 0.035, 8)))], "gloss": ([(0.385, 0.43), (0.455, 0.335)], OCHRE), } def pushpin(): a = math.radians(55) ux, uy = math.cos(a), math.sin(a) sx, sy, length, half = 0.44, 0.40, 0.52, 0.026 needle = [(sx - uy * half, sy + ux * half), (sx + ux * length, sy + uy * length), (sx + uy * half, sy - ux * half)] return { "fields": [field(STEEL, poly(needle), shade=False), field(RED, disc(0.40, 0.34, 0.215))], "gloss": (gloss_arc(0.40, 0.34, 0.165, 200, 248), RED), } def retry(): return { "fields": [field(NAVY, *arc_arrow(0.5, 0.5, 0.29, -35, 250, ARROW_W, 0.16, 0.12))], "gloss": (arc(0.5, 0.5, 0.30, 160, 205), NAVY), } def copy(): return { "fields": [field(CREAM_DEEP, poly(rrect(0.12, 0.09, 0.64, 0.69, 0.05))), field(CREAM, poly(rrect(0.36, 0.31, 0.88, 0.91, 0.05))), *sheet_lines(0.47, (0.50, 0.62, 0.74), (0.77, 0.77, 0.66))], "gloss": None, } def camera(): lens = round_poly([(0.66, 0.46), (0.90, 0.32), (0.90, 0.72), (0.66, 0.58)], 0.035) return { "fields": [field(INK, poly(lens)), field(INK, poly(rrect(0.10, 0.28, 0.69, 0.76, 0.08))), field(RED, disc(0.585, 0.375, 0.034), rim=False)], "gloss": (arc(0.18, 0.36, 0.045, 180, 270)[::-1] + [(0.30, 0.315)], INK), } def group(): """A group is a stack over its list (Home's group card): three cards fanned, the front one cream.""" card = rrect(0.25, 0.14, 0.75, 0.86, 0.06) return { "fields": [field(OCHRE, poly(rotate(move(card, -0.09, 0.0), -16))), field(SAGE, poly(rotate(move(card, 0.0, -0.01), -4))), field(CREAM, poly(rotate(move(card, 0.09, 0.02), 9))), *[field(GREY, line([(0.47, y), (0.73, y)], 0.046), rim=False) for y in (0.42, 0.53)]], "gloss": None, } def bookmark(): """Saving to read later: a ribbon bookmark, its foot cut in a V.""" ribbon = round_poly([(0.27, 0.08), (0.73, 0.08), (0.73, 0.92), (0.5, 0.71), (0.27, 0.92)], 0.035) return { "fields": [field(RED, poly(ribbon))], "gloss": ([(0.345, 0.16), (0.345, 0.46)], RED), } def flag(): """Urgent: a pennant on its pole, its fly cut in a swallowtail.""" pennant = round_poly([(0.29, 0.10), (0.86, 0.10), (0.70, 0.32), (0.86, 0.54), (0.29, 0.54)], 0.03) return { "fields": [field(STEEL, line([(0.22, 0.07), (0.22, 0.93)], 0.07), shade=False), field(RED, poly(pennant))], "gloss": ([(0.36, 0.17), (0.56, 0.17)], RED), } def chat(): """A chat that runs over a phone number (WhatsApp, never its mark): a round green bubble, the phone's handset in it.""" bubble = arc(0.52, 0.47, 0.39, 0, 360, 120) tail = round_poly([(0.22, 0.68), (0.36, 0.80), (0.10, 0.92)], 0.02) k, cx, cy, turn = 0.50, 0.52, 0.47, -135 def small(points): return [(cx + (x - 0.5) * k, cy + (y - 0.5) * k) for x, y in rotate(points, turn)] left = round_poly([(0.13, 0.47), (0.37, 0.50), (0.36, 0.72), (0.06, 0.67)], 0.065) right = [(1 - x, y) for x, y in left] return { "fields": [field(FOREST, poly(bubble), poly(tail)), field(CREAM, line(small(arc(0.5, 0.98, 0.50, 240, 300)), 0.16 * k), poly(small(left)), poly(small(right)), rim=False)], "gloss": (gloss_arc(0.52, 0.47, 0.32, 200, 248), FOREST), } def lens(): """Pictures someone posts (Instagram, never its mark): a rounded square camera, its round lens, a dot of light.""" return { "fields": [field(PINK, poly(rrect(0.10, 0.10, 0.90, 0.90, 0.22))), field(CREAM, disc(0.5, 0.52, 0.215)), field(INK, disc(0.5, 0.52, 0.125), rim=False), field(CREAM, disc(0.71, 0.27, 0.045), rim=False)], "gloss": ([(0.20, 0.36), (0.26, 0.21)], PINK), } def muted(): """A bell that will not ring (Mute): the bell, a red bar across it from upper left to lower right.""" spec = bell() spec["fields"].append(field(RED, line([(0.16, 0.14), (0.84, 0.86)], 0.085))) return spec def device(): """This phone (Keep on this phone): an upright handheld with its glass and the one button under it — the device that keeps a copy, never the handset that calls.""" return { "fields": [field(NAVY, poly(rrect(0.27, 0.07, 0.73, 0.93, 0.085))), field(SKY, poly(rrect(0.32, 0.15, 0.68, 0.77, 0.03)), rim=False), field(CREAM, disc(0.5, 0.85, 0.035), rim=False)], "gloss": ([(0.36, 0.24), (0.36, 0.44)], SKY), } def junk(): """Junk: the envelope, crossed out — a red X over it, never the bin (the bin is Delete's alone).""" spec = mail() spec["fields"].append(field(RED, line([(0.20, 0.16), (0.80, 0.84)], 0.085))) spec["fields"].append(field(RED, line([(0.80, 0.16), (0.20, 0.84)], 0.085))) return spec def block(): """Block sender: a raised open hand, palm out — stop — in one skin-toned enamel.""" fingers = [poly(rrect(x0, top, x0 + 0.115, 0.60, 0.0575)) for x0, top in ((0.255, 0.20), (0.385, 0.10), (0.515, 0.12), (0.645, 0.22))] thumb = poly(rotate(rrect(0.13, 0.50, 0.245, 0.80, 0.0575), -32, about=(0.24, 0.78))) palm = poly(rrect(0.255, 0.45, 0.76, 0.92, 0.12)) return { # Each finger its own field, so its rim parts it from the next; the palm last, over their roots. "fields": [*(field(TAN, f) for f in fingers), field(TAN, thumb), field(TAN, palm)], "gloss": ([(0.34, 0.55), (0.34, 0.78)], TAN), } def hidden(): """Hide: an eye struck through — an almond in ink with its cream white and a navy iris, a red bar across it from upper left to lower right, as Mute's bell wears — never the bin (the bin is Delete's alone).""" def almond(x0, x1, cy, lift): width = x1 - x0 top = path((x0, cy), ((x0 + width * 0.28, cy - lift), (x1 - width * 0.28, cy - lift), (x1, cy))) bottom = path((x1, cy), ((x1 - width * 0.28, cy + lift), (x0 + width * 0.28, cy + lift), (x0, cy))) return top + bottom[1:-1] return { "fields": [field(INK, poly(almond(0.06, 0.94, 0.5, 0.42))), field(CREAM, poly(almond(0.15, 0.85, 0.5, 0.33)), rim=False), field(NAVY, disc(0.5, 0.5, 0.155), rim=False), field(INK, disc(0.5, 0.5, 0.065), rim=False), field(RED, line([(0.16, 0.14), (0.84, 0.86)], 0.085))], "gloss": (arc(0.5, 0.5, 0.115, 200, 245), NAVY), } def unlink(): """Revoke: the link's two links pulled apart — navy and ochre as Make the link wears them, no longer interlocked — with a red break struck across the gap between them, so a link's Revoke never stands Make the link's chain.""" d, length, width, w = 0.235, 0.38, 0.24, 0.072 a = stadium(0.5 - d, 0.5 + d, length, width, -45) b = stadium(0.5 + d, 0.5 - d, length, width, -45) return { "fields": [field(NAVY, line(a, w)), field(OCHRE, line(b, w)), field(RED, line([(0.36, 0.36), (0.45, 0.45)], 0.055)), field(RED, line([(0.55, 0.55), (0.64, 0.64)], 0.055))], "gloss": None, } def lock(): """Only you: a closed padlock — a brass-steel shackle over a navy body with a cream keyhole — the record held for its owner alone; never the person, which is a field's and Call's, and never Hide's struck eye.""" shackle = arc(0.5, 0.40, 0.165, 180, 360) shackle = [(0.335, 0.50)] + shackle + [(0.665, 0.50)] keyhole = [field(CREAM, disc(0.5, 0.64, 0.055)), field(CREAM, poly(rrect(0.477, 0.645, 0.523, 0.765, 0.02)), rim=False)] return { "fields": [field(STEEL, line(shackle, 0.075)), field(NAVY, poly(rrect(0.20, 0.46, 0.80, 0.90, 0.07))), *keyhole], "gloss": ([(0.26, 0.53), (0.40, 0.53)], NAVY), } OBJECTS = { "clock": clock, "bell": bell, "repeat": repeat, "pin": pin, "flame": flame, "file": file, "picture": picture, "done": done, "snooze": snooze, "move": move_, "share": share, "agent": agent, "bin": bin_, "archive": archive, "open": open_, "link": link, "person": person, "edit": edit, "phone": phone, "message": message, "mail": mail, "star": star, "pushpin": pushpin, "retry": retry, "restore": restore, "copy": copy, "camera": camera, "group": group, "bookmark": bookmark, "flag": flag, "chat": chat, "lens": lens, "muted": muted, "device": device, "junk": junk, "block": block, "hidden": hidden, "unlink": unlink, "lock": lock, } # ---------------------------------------------------------------- drawing def hex_rgba(colour, alpha=255): return tuple(int(colour[i:i + 2], 16) for i in (1, 3, 5)) + (alpha,) def deeper(colour): """The field's second tone: the same enamel a little deeper and warmer.""" r, g, b, _ = hex_rgba(colour) k = 0.86 if (r + g + b) / 3 > 140 else 0.80 return (round(r * k), round(g * k * 0.99), round(b * k * 0.97), 255) def _thick(draw, pts, half): for (x0, y0), (x1, y1) in zip(pts, pts[1:]): length = math.dist((x0, y0), (x1, y1)) if length == 0: continue nx, ny = -(y1 - y0) / length * half, (x1 - x0) / length * half draw.polygon([((x0 + nx) * SS, (y0 + ny) * SS), ((x1 + nx) * SS, (y1 + ny) * SS), ((x1 - nx) * SS, (y1 - ny) * SS), ((x0 - nx) * SS, (y0 - ny) * SS)], fill=255) for x, y in pts: draw.ellipse([(x - half) * SS, (y - half) * SS, (x + half) * SS, (y + half) * SS], fill=255) def mask(parts, grow=0.0): """The parts' union, grown outward by `grow` — growing is how a field gets its rim (a round-joined offset).""" m = Image.new("L", (SS, SS), 0) d = ImageDraw.Draw(m) for part in parts: if part[0] == "disc": (x, y), r = part[1], part[2] + grow d.ellipse([(x - r) * SS, (y - r) * SS, (x + r) * SS, (y + r) * SS], fill=255) elif part[0] == "poly": d.polygon([(x * SS, y * SS) for x, y in part[1]], fill=255) if grow: _thick(d, part[1] + [part[1][0]], grow) else: _thick(d, part[1], part[2] / 2 + grow) return m def shifted(m, dx, dy): out = Image.new("L", m.size, 0) out.paste(m, (round(dx * SS), round(dy * SS))) return out def scaled(spec, k): """The same object k times the size about the canvas centre. Rim, second tone and gloss width stay fixed, so every object in the set keeps the one rim however large its shapes are drawn.""" def pt(p): return (0.5 + (p[0] - 0.5) * k, 0.5 + (p[1] - 0.5) * k) def part(p): if p[0] == "disc": return ("disc", pt(p[1]), p[2] * k) if p[0] == "poly": return ("poly", [pt(q) for q in p[1]]) return ("line", [pt(q) for q in p[1]], p[2] * k) out = dict(spec) out["fields"] = [dict(f, parts=tuple(part(p) for p in f["parts"]), clip=[part(p) for p in f["clip"]] if f["clip"] else None) for f in spec["fields"]] if spec.get("gloss"): out["gloss"] = ([pt(q) for q in spec["gloss"][0]], spec["gloss"][1]) if "dial" in spec: out["dial"] = (pt(spec["dial"][0]), spec["dial"][1] * k) return out def fitted(spec): """Scale the object so its longer side, rim included, fills the canvas less PAD on each side.""" img = draw_raw(spec) x0, y0, x1, y1 = img.getchannel("A").getbbox() extent = max(x1 - x0, y1 - y0) / SS pad = spec.get("pad", PAD) return scaled(spec, (1 - 2 * pad - 2 * RIM) / (extent - 2 * RIM)) def draw(spec): img = draw_raw(spec) # Centre the object on its bounding box. x0, y0, x1, y1 = img.getchannel("A").getbbox() dx, dy = round(SS / 2 - (x0 + x1) / 2), round(SS / 2 - (y0 + y1) / 2) centred = Image.new("RGBA", (SS, SS), (0, 0, 0, 0)) centred.paste(img, (dx, dy)) air = min(x0 + dx, y0 + dy, SS - (x1 + dx), SS - (y1 + dy)) / SS return centred, (dx / SS, dy / SS), air def draw_raw(spec): img = Image.new("RGBA", (SS, SS), (0, 0, 0, 0)) for f in spec["fields"]: clip = mask(f["clip"]) if f["clip"] else None if f["rim"]: rim = mask(f["parts"], RIM) if clip: rim = ImageChops.multiply(rim, clip) img.paste(hex_rgba(METAL), mask=rim) body = mask(f["parts"]) if clip: body = ImageChops.multiply(body, clip) img.paste(hex_rgba(f["fill"]), mask=body) if f["shade"]: # The second tone: where the field, moved toward the upper left, no longer covers it — a crescent # along the lower right edge, as on a domed enamel. Soft-edged inside, hard against the rim. lit = shifted(body, -SHADE * 0.7, -SHADE) band = ImageChops.subtract(body, lit).filter(ImageFilter.GaussianBlur(SS * 0.006)) band = ImageChops.multiply(band, body) img.paste(deeper(f["fill"]), mask=band) if spec.get("gloss"): pts, colour = spec["gloss"] streak = mask([line(pts, GLOSS_W)]).filter(ImageFilter.GaussianBlur(SS * 0.005)) # Only on the enamel it was drawn for (its own colour, either tone), never spilling onto the rim. rgb = img.convert("RGB") own = Image.new("L", (SS, SS), 0) for tone in (hex_rgba(colour), deeper(colour)): diff = ImageChops.difference(rgb, Image.new("RGB", (SS, SS), tone[:3])).convert("L") own = ImageChops.lighter(own, diff.point(lambda v: 255 if v < 6 else 0)) own = own.filter(ImageFilter.MinFilter(9)) streak = ImageChops.multiply(streak, own).point(lambda v: round(v * GLOSS_A)) white = Image.new("RGBA", (SS, SS), (255, 255, 255, 0)) white.putalpha(streak) img = Image.alpha_composite(img, white) return img def reduce(img, side): return img.convert("RGBa").resize((side, side), Image.LANCZOS).convert("RGBA") def write(name, img): asset = f"object-{name}" dest = CATALOGUE / f"{asset}.imageset" dest.mkdir(parents=True, exist_ok=True) images = [] for scale, suffix, side in SCALES: filename = f"{asset}{suffix}.png" reduce(img, side).save(dest / filename, optimize=True) images.append({"filename": filename, "idiom": "universal", "scale": scale}) contents = {"images": images, "info": {"author": "xcode", "version": 1}} (dest / "Contents.json").write_text(json.dumps(contents, indent=2) + "\n") def contact_sheet(drawn, out): """Every object at 128, 64 and 40 pt on each card stock, at 1 px per point.""" names = list(drawn) cols = 6 rows = math.ceil(len(names) / cols) cell_w, cell_h = 128 + 64 + 36, 128 + 24 panel_w, panel_h = cols * cell_w + 32, rows * cell_h + 32 sheet = Image.new("RGBA", (2 * panel_w + 48, 3 * panel_h + 64), (250, 248, 244, 255)) smalls = {n: (reduce(img, 128), reduce(img, 64), reduce(img, 40)) for n, img in drawn.items()} for p, (_, colour) in enumerate(PAPERS): px, py = 16 + (p % 2) * (panel_w + 16), 16 + (p // 2) * (panel_h + 16) ImageDraw.Draw(sheet).rounded_rectangle([px, py, px + panel_w, py + panel_h], 28, fill=hex_rgba(colour)) for i, name in enumerate(names): cx, cy = px + 16 + (i % cols) * cell_w, py + 16 + (i // cols) * cell_h big, small, least = smalls[name] sheet.alpha_composite(big, (cx, cy)) sheet.alpha_composite(small, (cx + 128 + 12, cy + 4)) sheet.alpha_composite(least, (cx + 128 + 24, cy + 80)) sheet.convert("RGB").save(out) def main(): parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) parser.add_argument("--only", nargs="*", choices=sorted(OBJECTS), help="draw just these") parser.add_argument("--sheet", type=pathlib.Path, help="also write a contact sheet PNG here") parser.add_argument("--no-write", action="store_true", help="draw (and sheet) without touching the catalogue") args = parser.parse_args() drawn = {} for name in args.only or OBJECTS: spec = fitted(OBJECTS[name]()) img, (dx, dy), air = draw(spec) if air < spec.get("pad", PAD) - 0.005: print(f" ! {name}: only {air:.3f} of air (want {spec.get('pad', PAD)})") if "dial" in spec: (x, y), reach = spec["dial"] print(f" {name}: dial centre ({x + dx:.4f}, {y + dy:.4f}), a hand may reach {reach:.3f} of the canvas") if not args.no_write: write(name, img) drawn[name] = img print(f"drew object-{name} (air {air:.3f})") if args.sheet: contact_sheet(drawn, args.sheet) print("sheet", args.sheet) if __name__ == "__main__": main()