"""The drawing kit for problem cards: one canvas, one palette, light and dark built in. It lives here only: tools/cards/kit.py in the catalog, which each problem folder links as kit.py. The Shoal site serves no copy of it: a card's cover is the problem's poster (tools/cards/posters.py), and what this kit draws is kept for the problem's own page. A figure.py at a problem's root, with this file beside it, imports it, draws the problem's object and writes cover.svg next to itself (docs/cards.md says what a figure must be): import kit body = kit.circle(320, 152, 40, 'f') kit.write(__file__, 'mathematics', body, label='what is drawn') Classes (each colour means one thing on a figure; pick the meaning, then the class): ink, ink-s the object itself (fill / stroke) ink2-s secondary structure (edges, outlines) f, f-s, f-soft the field's colour: the part the question is about (fill / stroke / pale fill) sand, sand-s the one accent: a second colour class, an open or contested part faint, faint-s context: grids, axes, ghosts ring a halo in the page's colour, so dots read on top of lines bg a fill in the page's colour, to hide what lies behind (front faces, occluding balls) soft a pale fill of any class's colour: add it next to f, sand or ink (class="sand soft") lbl the one label line """ import math from pathlib import Path W, H = 640, 304 # shown at 320 x 152 on a card, larger on a featured card # the site's field colours (public/app.css, --f-*), light and dark FIELD = { 'mathematics': ('#4f6bed', '#93a6ff'), 'computer-science': ('#9b59d0', '#c9a2f3'), 'physics-astronomy': ('#129d96', '#5fd0c8'), 'chemistry-materials': ('#d08a17', '#f2c063'), 'life-health': ('#5a9e2c', '#a3d47a'), 'earth-environment': ('#2a8ccf', '#7cc4f2'), 'engineering-energy': ('#d4533f', '#f29484'), 'social-economics': ('#c2448f', '#f09ac8'), } LIGHT = dict(ink='#1f1f1f', ink2='#3c4043', faint='#9aa0a6', line='#dadce0', sand='#b8862c', bg='#ffffff', muted='#5f6368') DARK = dict(ink='#e8eaed', ink2='#c4c7c5', faint='#6f7479', line='#3c4043', sand='#d9b26a', bg='#1f2023', muted='#9aa0a6') def _vars(c, f): return (f'--ink:{c["ink"]};--ink2:{c["ink2"]};--faint:{c["faint"]};--line:{c["line"]};--sand:{c["sand"]};' f'--bg:{c["bg"]};--muted:{c["muted"]};--f:{f}') def style(field): light, dark = FIELD[field] return (f'') def f1(n): return f'{round(n, 1):g}' def circle(x, y, r, cls, extra=''): return f'' def dot(x, y, r=6, cls='ink'): """A point that stays readable on top of lines: filled, with a halo in the page's colour.""" return circle(x, y, r, f'{cls} ring') def line(x1, y1, x2, y2, cls='ink2-s', w=2, extra=''): return f'' def path(d, cls='ink2-s', w=2, extra=''): return f'' def polygon(pts, cls='f-soft', extra=''): return f'' def polyline(pts, cls='ink2-s', w=2, extra=''): return f'' DASH = ' stroke-dasharray="7 6"' # pass as `extra` for a dashed stroke def polypath(pts, cls='ink2-s', w=2, closed=False, extra=''): """Many points as one relative path: the same line as polyline, a fraction of the bytes.""" (x0, y0), d, px, py = pts[0], [], pts[0][0], pts[0][1] for x, y in pts[1:]: d.append(f'{f1(x - px)} {f1(y - py)}'); px, py = x, y return path(f'M{f1(x0)} {f1(y0)}l' + ' '.join(d) + ('z' if closed else ''), cls, w, extra) def arrow(x1, y1, x2, y2, cls='ink2-s', w=2, head=10): """A line ending in an open arrowhead at (x2, y2).""" a = math.atan2(y2 - y1, x2 - x1) tips = [(x2 - head * math.cos(a - s), y2 - head * math.sin(a - s)) for s in (0.45, -0.45)] return line(x1, y1, x2, y2, cls, w) + polyline([tips[0], (x2, y2), tips[1]], cls, w) def text(x, y, s, cls='lbl', anchor='middle'): s = s.replace('&', '&').replace('<', '<').replace('>', '>') return f'{s}' def svg(field, body, label=None, title=''): """The whole figure. `label` is the one line naming what is drawn, at the bottom; leave room for it (y < 262).""" if field not in FIELD: raise ValueError(f'unknown field {field!r}') if label and len(label) > 50: raise ValueError(f'label over 50 characters (more does not fit at card size): {label!r}') t = f'{title or label or ""}' tail = text(W / 2, H - 16, label) if label else '' return f'{t}{style(field)}{body}{tail}\n' def write(figure_py, field, body, label=None, title=''): """Writes cover.svg next to the figure.py that calls it.""" out = Path(figure_py).resolve().parent / 'cover.svg' out.write_text(svg(field, body, label, title)) return out