Files
Bedrock/brand/ribbon-study/generate.py
T

83 lines
3.7 KiB
Python

"""Render a ribbon study as editable SVG, using only the standard library."""
from math import cos, sin, pi
from pathlib import Path
BACKGROUND = "#431744"
# Coordinates are relative to the centre of the reference mark.
CAP = "M-91 -78 C-67 -111 -34 -121 0 -121 C33 -121 70 -107 90 -80 C105 -59 82 -52 63 -53 C37 -52 14 -63 0 -74 C-23 -58 -47 -52 -69 -53 C-91 -53 -100 -63 -91 -78Z"
LEFT = "M-91 -78 C-58 -107 -28 -99 0 -74 C-23 -58 -47 -52 -69 -53 C-91 -53 -100 -63 -91 -78Z"
RIGHT = "M0 -74 C37 -104 65 -98 90 -80 C105 -59 82 -52 63 -53 C37 -52 14 -63 0 -74Z"
def gradient(name, colors):
stops = ''.join(f'<stop offset="{i/(len(colors)-1):.2f}" stop-color="{color}"/>'
for i, color in enumerate(colors))
return f'<linearGradient id="{name}" x2="100%" y2="24%">{stops}</linearGradient>'
def trefoil():
layers = []
palettes = [
("#d6c9c6", "#917687", "#a35c78", "#874969", "#99909d", "#766d82"),
("#ffe0bf", "#ffc0a2", "#8e8e9b", "#726d84", "#ffb295", "#ff8c76"),
("#ffb390", "#ff917c", "#ff9983", "#ff846f", "#b57184", "#8c506f"),
]
for index, colors in enumerate(palettes):
parts = []
for face, outline in enumerate((CAP, LEFT, RIGHT)):
name = f"petal-{index}-{face}"
parts.append(gradient(name, colors[2*face:2*face+2]))
parts.append(f'<path fill="url(#{name})" d="{outline}"/>')
layers.append(f'<g transform="rotate({120*index})">{"".join(parts)}</g>')
return ''.join(layers)
def coil(turns, width, pitch, tilt, prefix):
"""Project a spherical helix. Draw its far half-turns before its near ones."""
palettes = [
[("#7b566f", "#aa7889"), ("#a4b4bb", "#79748c"), ("#834764", "#b77987")],
[("#ffa58a", "#ff8876"), ("#ffdebb", "#f2b9a8"), ("#c2d6d0", "#a39daa")],
]
layers = [[], []]
for half in range(2 * turns):
near = half % 2
start = half*pi
name = f"{prefix}-{half}"
def point(t, side):
u = t / (2*pi*turns)
lat = -pi/2 + pi*u + side*width*pi/(2*turns)*sin(pi*u)**.55
lat += pitch*sin(t)*cos(lat) + .12*sin(2*t)*cos(lat)
return f"{100*cos(lat)*cos(t):.2f},{-100*sin(lat):.2f}"
# Follow the actual circular limb between both edges at each turn.
points = [point(start + pi*j/80, -1) for j in range(81)]
points += [point(start + pi, -1+j/8) for j in range(1,17)]
points += [point(start + pi*j/80, 1) for j in reversed(range(80))]
points += [point(start, 1-j/8) for j in range(1,17)]
layers[near].append(gradient(name, palettes[near][(half//2)%3]))
opacity = .90 if near else .84
layers[near].append(f'<polygon opacity="{opacity}" points="{" ".join(points)}" fill="url(#{name})"/>')
return f'<g transform="rotate({tilt})">{"".join(layers[0]+layers[1])}</g>'
def place(shape, x, y, scale):
return f'<g transform="translate({x} {y}) scale({scale})">{shape}</g>'
def draw():
marks = [place(trefoil(), 214, 238, 1.20)]
for index, (x, y, turns, width, pitch, tilt) in enumerate([
(502, 88, 2, .77, .45, -14), (673, 88, 3, .91, .40, 46),
(502, 239, 4, .85, .21, 4), (673, 239, 4, .46, .10, -10),
(502, 391, 3, .65, .39, -12), (673, 391, 3, .82, .21, 82),
]):
marks.append(place(coil(turns, width, pitch, tilt, f"s{index}"), x, y, .48))
return ('<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 800 480" width="800" height="480">'
f'<title>Bedrock ribbon studies</title><rect width="800" height="480" fill="{BACKGROUND}"/>'
+ ''.join(marks) + '</svg>\n')
if __name__ == "__main__":
Path(__file__).with_name("preview.svg").write_text(draw(), encoding="utf-8")