116 lines
4.8 KiB
Python
116 lines
4.8 KiB
Python
"""Draw all twelve reference marks, or export one with --mark 1..12."""
|
|
import argparse
|
|
import json
|
|
from math import cos, sin, pi, radians
|
|
from pathlib import Path
|
|
|
|
from generate import gradient, trefoil
|
|
|
|
DIRECTORY = Path(__file__).resolve().parent
|
|
NAMES = (
|
|
"Crossed horizontal bands", "Nested rings", "Vertical split", "Diagonal weave",
|
|
"Opposing caps", "Diagonal fold", "Three petals", "Horizontal belt",
|
|
"Stepped wrap", "Vertical weave", "Open wave", "Open helix",
|
|
)
|
|
|
|
|
|
def outline(points):
|
|
"""Blend adjacent contour segments into a closed cubic spline."""
|
|
commands = [f"M{points[0][0]},{points[0][1]}"]
|
|
for i, a in enumerate(points):
|
|
prev, b, after = (points[j % len(points)] for j in (i-1, i+1, i+2))
|
|
# Short handles preserve tight crossings and narrow gaps.
|
|
c = [a[k] + (b[k]-prev[k])*.10 for k in (0, 1)]
|
|
d = [b[k] - (after[k]-a[k])*.10 for k in (0, 1)]
|
|
commands.append(f"C{c[0]:.2f},{c[1]:.2f} {d[0]:.2f},{d[1]:.2f} {b[0]},{b[1]}")
|
|
return "".join(commands) + "Z"
|
|
|
|
|
|
def rings():
|
|
"""Project parallel latitude bands, keeping the lower pole in view."""
|
|
strips = []
|
|
palette = [("#ffddad", "#ffd5be"), ("#ffaf92", "#ffa38c"),
|
|
("#ffdab3", "#ffc4a6"), ("#ff927c", "#ffa58b"),
|
|
("#ffc392", "#ffe1ba")]
|
|
eye = radians(27)
|
|
for front in (False, True):
|
|
bands = [(-83, -59), (-52, -31), (-23, -2), (7, 24), (35, 47), (58, 74)]
|
|
for row in reversed(range(len(bands))):
|
|
low, high = bands[row]
|
|
points = []
|
|
for lat, steps in [(low, range(81)), (high, reversed(range(81)))]:
|
|
lat = radians(lat)
|
|
for j in steps:
|
|
lon = pi*(j/80 + int(not front))
|
|
x = cos(lat)*cos(lon)
|
|
y = sin(lat)*cos(eye) + cos(lat)*sin(lon)*sin(eye)
|
|
points.append(f"{50+45*x:.2f},{50-45*y:.2f}")
|
|
colors = palette[row % len(palette)] if front else ("#8f8498", "#a27085")
|
|
name = f"ring-{row}-{int(front)}"
|
|
strips.append(gradient(name, colors))
|
|
strips.append(f'<polygon points="{" ".join(points)}" fill="url(#{name})"/>')
|
|
return "".join(strips)
|
|
|
|
|
|
def belt():
|
|
paths = []
|
|
for i, (shape, colors) in enumerate([
|
|
("M20 19C27 3 69 2 79 18C82 25 17 25 20 19Z", ("#a9637b", "#783753")),
|
|
("M6 42C7 29 91 28 94 41L94 61C92 73 9 73 6 60Z", ("#88849a", "#c4d5d1")),
|
|
("M6 42C7 29 91 28 94 41C83 49 18 50 6 42Z", ("#64516e", "#93889c")),
|
|
("M6 60C14 52 84 54 94 61C91 72 10 73 6 60Z", ("#67516c", "#8c8194")),
|
|
("M19 79C24 72 73 72 80 78C72 99 32 101 19 79Z", ("#ff947f", "#ffcaab")),
|
|
]):
|
|
name = f"belt-{i}"
|
|
paths.extend([gradient(name, colors), f'<path d="{shape}" fill="url(#{name})"/>'])
|
|
return "".join(paths)
|
|
|
|
|
|
def symbol(index, layers):
|
|
if index == 1:
|
|
art = rings()
|
|
elif index == 6:
|
|
art = f'<g transform="translate(50 50) scale(.37)">{trefoil()}</g>'
|
|
elif index == 7:
|
|
art = belt()
|
|
else:
|
|
art = traced_faces(index, layers)
|
|
return f'<g id="mark-{index+1:02}"><title>{NAMES[index]}</title>{art}</g>'
|
|
|
|
|
|
def traced_faces(index, layers):
|
|
paths = []
|
|
for number, (left, right, loops) in enumerate(layers):
|
|
ident = f"g{index}-{number}"
|
|
paths.append(f'<linearGradient id="{ident}"><stop stop-color="{left}"/>'
|
|
f'<stop offset="1" stop-color="{right}"/></linearGradient>')
|
|
curves = "".join(outline(loop) for loop in loops)
|
|
paths.append(f'<path d="{curves}" fill="url(#{ident})" fill-rule="evenodd" '
|
|
f'stroke="url(#{ident})" stroke-width=".55" stroke-linejoin="round"/>')
|
|
return "".join(paths)
|
|
|
|
|
|
def draw(mark=None):
|
|
layers = json.loads((DIRECTORY / "contours.json").read_text())
|
|
if mark:
|
|
width = height = 100
|
|
shapes = [symbol(mark-1, layers.get(str(mark)))]
|
|
else:
|
|
width, height = 680, 460
|
|
shapes = []
|
|
for i in range(len(NAMES)):
|
|
x, y = 50 + i % 4 * 160, 32 + i // 4 * 148
|
|
shapes.append(f'<g transform="translate({x} {y})">{symbol(i, layers.get(str(i+1)))}</g>')
|
|
background = '' if mark else '<rect width="100%" height="100%" fill="#421743"/>'
|
|
return (f'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 {width} {height}" '
|
|
f'width="{width}" height="{height}">{background}' + "\n".join(shapes) + '</svg>\n')
|
|
|
|
|
|
if __name__ == "__main__":
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument("--mark", type=int, choices=range(1, 13))
|
|
parser.add_argument("--output", type=Path)
|
|
args = parser.parse_args()
|
|
target = args.output or DIRECTORY / (f"mark-{args.mark:02}.svg" if args.mark else "catalogue.svg")
|
|
target.write_text(draw(args.mark), encoding="utf-8")
|