"""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'' for i, color in enumerate(colors)) return f'{stops}' 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'') layers.append(f'{"".join(parts)}') 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'') return f'{"".join(layers[0]+layers[1])}' def place(shape, x, y, scale): return f'{shape}' 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 ('' f'Bedrock ribbon studies' + ''.join(marks) + '\n') if __name__ == "__main__": Path(__file__).with_name("preview.svg").write_text(draw(), encoding="utf-8")