Merge pull request '修正色带试稿的轮廓与遮挡' (#39) from codex/brand into main
Deploy Rust Bedrock / deploy (push) Has been cancelled
Deploy Rust Bedrock / deploy (push) Has been cancelled
Reviewed-on: #39
This commit was merged in pull request #39.
This commit is contained in:
@@ -1,47 +1,64 @@
|
|||||||
"""Draw the Bedrock ribbon study with Python's standard library."""
|
"""Render a ribbon study as editable SVG, using only the standard library."""
|
||||||
from math import cos, sin, pi
|
from math import cos, sin, pi
|
||||||
from pathlib import Path
|
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 path(points, color):
|
|
||||||
coords = " ".join(f"{x:.2f},{y:.2f}" for x, y in points)
|
def gradient(name, colors):
|
||||||
return f'<polygon points="{coords}" fill="{color}"/>'
|
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():
|
def trefoil():
|
||||||
# Two faces share a crossing inside a rounded segment of the silhouette.
|
|
||||||
layers = []
|
layers = []
|
||||||
for angle, base, left, right in [
|
palettes = [
|
||||||
(0, "#beabb6", "#a35f81", "#817e92"),
|
("#d6c9c6", "#917687", "#a35c78", "#874969", "#99909d", "#766d82"),
|
||||||
(120, "#ffdabd", "#8a8599", "#ffa086"),
|
("#ffe0bf", "#ffc0a2", "#8e8e9b", "#726d84", "#ffb295", "#ff8c76"),
|
||||||
(240, "#ffac91", "#fc887b", "#a8617c"),
|
("#ffb390", "#ff917c", "#ff9983", "#ff846f", "#b57184", "#8c506f"),
|
||||||
]:
|
]
|
||||||
layers.append(f'''<g transform="rotate({angle})">
|
for index, colors in enumerate(palettes):
|
||||||
<path fill="{base}" d="M-80 -51 C-86 -63 -49 -96 0 -96 C46 -96 85 -65 80 -51 C75 -35 32 -35 0 -56 C-31 -35 -72 -34 -80 -51Z"/>
|
parts = []
|
||||||
<path fill="{left}" d="M-80 -51 C-83 -58 -59 -76 -36 -73 C-21 -72 -9 -63 0 -56 C-31 -35 -72 -34 -80 -51Z"/>
|
for face, outline in enumerate((CAP, LEFT, RIGHT)):
|
||||||
<path fill="{right}" d="M0 -56 C24 -74 51 -82 76 -60 Q83 -54 80 -51 C75 -35 32 -35 0 -56Z"/>
|
name = f"petal-{index}-{face}"
|
||||||
</g>''')
|
parts.append(gradient(name, colors[2*face:2*face+2]))
|
||||||
return "".join(layers)
|
parts.append(f'<path fill="url(#{name})" d="{outline}"/>')
|
||||||
|
layers.append(f'<g transform="rotate({120*index})">{"".join(parts)}</g>')
|
||||||
|
return ''.join(layers)
|
||||||
|
|
||||||
|
|
||||||
def globe(count, wave, tilt, phase):
|
def coil(turns, width, pitch, tilt, prefix):
|
||||||
front = ["#ffd9ba", "#ff947f", "#b8c6c3", "#e6a2a2"]
|
"""Project a spherical helix. Draw its far half-turns before its near ones."""
|
||||||
rear = ["#99607b", "#766780", "#ad798b", "#7e708a"]
|
palettes = [
|
||||||
faces = []
|
[("#7b566f", "#aa7889"), ("#a4b4bb", "#79748c"), ("#834764", "#b77987")],
|
||||||
for back in (True, False):
|
[("#ffa58a", "#ff8876"), ("#ffdebb", "#f2b9a8"), ("#c2d6d0", "#a39daa")],
|
||||||
for i in range(count):
|
]
|
||||||
center = -pi / 2 + (i + .5) * pi / count
|
layers = [[], []]
|
||||||
edges = []
|
for half in range(2 * turns):
|
||||||
for side in (-1, 1):
|
near = half % 2
|
||||||
edge = []
|
start = half*pi
|
||||||
for j in range(97):
|
name = f"{prefix}-{half}"
|
||||||
lon = (j / 96 + int(back)) * pi
|
|
||||||
lat = center + (side * .33 * pi / count
|
def point(t, side):
|
||||||
+ wave * sin(2 * lon + phase) * sin(lon) ** 2 * cos(center))
|
u = t / (2*pi*turns)
|
||||||
edge.append((96 * cos(lat) * cos(lon), -96 * sin(lat)))
|
lat = -pi/2 + pi*u + side*width*pi/(2*turns)*sin(pi*u)**.55
|
||||||
edges.append(edge)
|
lat += pitch*sin(t)*cos(lat) + .12*sin(2*t)*cos(lat)
|
||||||
faces.append(path(edges[0] + edges[1][::-1], (rear if back else front)[i % 4]))
|
return f"{100*cos(lat)*cos(t):.2f},{-100*sin(lat):.2f}"
|
||||||
return f'<g transform="rotate({tilt})">' + "".join(faces) + "</g>"
|
|
||||||
|
# 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):
|
def place(shape, x, y, scale):
|
||||||
@@ -49,13 +66,16 @@ def place(shape, x, y, scale):
|
|||||||
|
|
||||||
|
|
||||||
def draw():
|
def draw():
|
||||||
marks = [place(trefoil(), 239, 223, 1.58)]
|
marks = [place(trefoil(), 214, 238, 1.20)]
|
||||||
for x, y, spec in [(536, 126, (3, .34, -17, .5)), (713, 126, (5, .16, 12, 1)),
|
for index, (x, y, turns, width, pitch, tilt) in enumerate([
|
||||||
(536, 319, (4, .24, -10, 2)), (713, 319, (3, .28, 83, .4))]:
|
(502, 88, 2, .77, .45, -14), (673, 88, 3, .91, .40, 46),
|
||||||
marks.append(place(globe(*spec), x, y, .70))
|
(502, 239, 4, .85, .21, 4), (673, 239, 4, .46, .10, -10),
|
||||||
return ('<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 840 448" width="840" height="448">'
|
(502, 391, 3, .65, .39, -12), (673, 391, 3, .82, .21, 82),
|
||||||
'<title>Bedrock — ribbon form studies</title><rect width="840" height="448" fill="#421840"/>'
|
]):
|
||||||
+ "".join(marks) + '</svg>\n')
|
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__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
|
Before Width: | Height: | Size: 75 KiB After Width: | Height: | Size: 102 KiB |
Reference in New Issue
Block a user