|
| 1 | +""" |
| 2 | +Generate the pgraph logo. |
| 3 | +
|
| 4 | +A snake follows a path through a small graph, its head acting as the arrowhead |
| 5 | +pointing at the goal vertex, beside the "pgraph" wordmark. The style matches |
| 6 | +the snake logos used across the RVC toolboxes (RTB, MVTB, bdsim, SMTB): a |
| 7 | +grey-to-teal body, yellow eyes and a magenta forked tongue. |
| 8 | +
|
| 9 | +Renders ``pgraph_logo.png`` (2x scale, white background) next to this |
| 10 | +script, using ``rsvg-convert`` (from librsvg) to rasterize the SVG. |
| 11 | +
|
| 12 | +The wordmark is SVG text in Roboto Medium, so Roboto must be installed for |
| 13 | +the PNG to render correctly. The PNG is the committed artifact used by the |
| 14 | +README. |
| 15 | +
|
| 16 | +Usage:: |
| 17 | +
|
| 18 | + python docs/figs/pgraph_logo.py |
| 19 | +""" |
| 20 | + |
| 21 | +import math |
| 22 | +import shutil |
| 23 | +import subprocess |
| 24 | +from pathlib import Path |
| 25 | + |
| 26 | +# palette |
| 27 | +TEAL = "#2bb39a" # snake head, end of body gradient |
| 28 | +GREY_TAIL = "#5c5c5c" # start of body gradient |
| 29 | +GREY_TEXT = "#4d4d4d" |
| 30 | +GREY_EDGE = "#9a9a9a" |
| 31 | +GREY_NODE = "#555555" |
| 32 | +EYE = "#ffee00" |
| 33 | +TONGUE = "#d14fc4" |
| 34 | + |
| 35 | +WIDTH, HEIGHT = 580, 210 |
| 36 | +NODE_RADIUS = 11 |
| 37 | +BODY_WIDTH = 14 |
| 38 | +HEAD_SCALE = 1.3 |
| 39 | + |
| 40 | +# graph: vertex coordinates, edges, and the path the snake follows |
| 41 | +VERTICES: dict[int, tuple[float, float]] = { |
| 42 | + 1: (35, 165), |
| 43 | + 2: (85, 55), |
| 44 | + 3: (125, 150), |
| 45 | + 4: (180, 90), |
| 46 | + 5: (225, 175), |
| 47 | + 6: (255, 40), |
| 48 | +} |
| 49 | +EDGES: list[tuple[int, int]] = [ |
| 50 | + (1, 2), (1, 3), (2, 3), (2, 4), (3, 4), |
| 51 | + (3, 5), (4, 5), (4, 6), (2, 6), (5, 6), |
| 52 | +] # fmt: skip |
| 53 | +PATH: list[int] = [1, 3, 4, 6] |
| 54 | + |
| 55 | + |
| 56 | +def snake_head(x: float, y: float, angle: float, scale: float = 1.0) -> str: |
| 57 | + """ |
| 58 | + Snake head as an SVG group. |
| 59 | +
|
| 60 | + :param x: x-coordinate of the neck |
| 61 | + :param y: y-coordinate of the neck |
| 62 | + :param angle: heading of the head, in radians |
| 63 | + :param scale: scale factor applied to the head |
| 64 | + :returns: SVG ``<g>`` element |
| 65 | +
|
| 66 | + The head is defined with its neck at the origin and nose pointing along +x, |
| 67 | + then translated and rotated into place. |
| 68 | + """ |
| 69 | + return f"""<g transform="translate({x:.1f},{y:.1f}) rotate({math.degrees(angle):.1f}) scale({scale})"> |
| 70 | + <path d="M 22 0 l 9 0 l 5 -4 m -5 4 l 5 4" stroke="{TONGUE}" stroke-width="2.2" fill="none" stroke-linecap="round"/> |
| 71 | + <path d="M -4 -7 C 6 -12, 18 -11, 23 -3 C 25 0, 25 0, 23 3 C 18 11, 6 12, -4 7 Z" fill="{TEAL}"/> |
| 72 | + <circle cx="13" cy="-4.5" r="3.2" fill="{EYE}"/> |
| 73 | + <circle cx="13" cy="4.5" r="3.2" fill="{EYE}"/> |
| 74 | +</g>""" |
| 75 | + |
| 76 | + |
| 77 | +def logo_svg(font_weight: int = 500) -> str: |
| 78 | + """ |
| 79 | + Build the logo as an SVG document. |
| 80 | +
|
| 81 | + :param font_weight: CSS font weight of the "pgraph" wordmark |
| 82 | + :returns: SVG document |
| 83 | + """ |
| 84 | + pts = [VERTICES[v] for v in PATH] |
| 85 | + (x0, y0), (xN, yN) = pts[0], pts[-1] |
| 86 | + |
| 87 | + # body gradient runs from the tail to near the neck, so the neck |
| 88 | + # blends into the solid-colored head |
| 89 | + gx = x0 + 0.8 * (xN - x0) |
| 90 | + gy = y0 + 0.8 * (yN - y0) |
| 91 | + out = [ |
| 92 | + f'<svg xmlns="http://www.w3.org/2000/svg" width="{WIDTH}" height="{HEIGHT}" viewBox="0 0 {WIDTH} {HEIGHT}">', |
| 93 | + f'<defs><linearGradient id="body" gradientUnits="userSpaceOnUse" x1="{x0}" y1="{y0}" x2="{gx:.1f}" y2="{gy:.1f}">' |
| 94 | + f'<stop offset="0" stop-color="{GREY_TAIL}"/><stop offset="1" stop-color="{TEAL}"/>' |
| 95 | + "</linearGradient></defs>", |
| 96 | + ] |
| 97 | + |
| 98 | + # graph edges not on the path, drawn thin |
| 99 | + on_path = {frozenset(e) for e in zip(PATH, PATH[1:])} |
| 100 | + for a, b in EDGES: |
| 101 | + if frozenset((a, b)) in on_path: |
| 102 | + continue |
| 103 | + (xa, ya), (xb, yb) = VERTICES[a], VERTICES[b] |
| 104 | + out.append( |
| 105 | + f'<line x1="{xa}" y1="{ya}" x2="{xb}" y2="{yb}" ' |
| 106 | + f'stroke="{GREY_EDGE}" stroke-width="3" stroke-linecap="round"/>' |
| 107 | + ) |
| 108 | + |
| 109 | + # the body stops short of the goal vertex, leaving room for head and tongue |
| 110 | + (xa, ya), (xb, yb) = pts[-2], pts[-1] |
| 111 | + heading = math.atan2(yb - ya, xb - xa) |
| 112 | + tongue_len, head_len = 15, 30 |
| 113 | + back = NODE_RADIUS + tongue_len + head_len + 2 |
| 114 | + hx, hy = xb - back * math.cos(heading), yb - back * math.sin(heading) |
| 115 | + |
| 116 | + # thin tail poking out behind the start vertex |
| 117 | + x1, y1 = pts[1] |
| 118 | + t = math.atan2(y0 - y1, x0 - x1) |
| 119 | + tx, ty = x0 + 22 * math.cos(t), y0 + 22 * math.sin(t) |
| 120 | + out.append( |
| 121 | + f'<path d="M {tx:.1f} {ty:.1f} L {x0} {y0}" stroke="url(#body)" ' |
| 122 | + 'stroke-width="6" stroke-linecap="round"/>' |
| 123 | + ) |
| 124 | + |
| 125 | + body = f"M {x0} {y0} " + " ".join(f"L {x} {y}" for x, y in pts[1:-1]) |
| 126 | + body += f" L {hx:.1f} {hy:.1f}" |
| 127 | + out.append( |
| 128 | + f'<path d="{body}" stroke="url(#body)" stroke-width="{BODY_WIDTH}" ' |
| 129 | + 'fill="none" stroke-linejoin="round" stroke-linecap="round"/>' |
| 130 | + ) |
| 131 | + out.append(snake_head(hx, hy, heading, HEAD_SCALE)) |
| 132 | + |
| 133 | + # vertices drawn last, over the edges and the snake's body |
| 134 | + for x, y in VERTICES.values(): |
| 135 | + out.append( |
| 136 | + f'<circle cx="{x}" cy="{y}" r="{NODE_RADIUS}" fill="white" ' |
| 137 | + f'stroke="{GREY_NODE}" stroke-width="3"/>' |
| 138 | + ) |
| 139 | + |
| 140 | + out.append( |
| 141 | + f'<text x="290" y="140" font-family="Roboto" font-weight="{font_weight}" ' |
| 142 | + f'font-size="84" fill="{GREY_TEXT}">pgraph</text>' |
| 143 | + ) |
| 144 | + out.append("</svg>") |
| 145 | + return "\n".join(out) |
| 146 | + |
| 147 | + |
| 148 | +def main() -> None: |
| 149 | + if shutil.which("rsvg-convert") is None: |
| 150 | + raise SystemExit("rsvg-convert not found (brew install librsvg)") |
| 151 | + png = Path(__file__).parent / "pgraph_logo.png" |
| 152 | + subprocess.run( |
| 153 | + ["rsvg-convert", "-z", "2", "-b", "white", "-o", str(png)], |
| 154 | + input=logo_svg().encode(), |
| 155 | + check=True, |
| 156 | + ) |
| 157 | + print(f"wrote {png}") |
| 158 | + |
| 159 | + |
| 160 | +if __name__ == "__main__": |
| 161 | + main() |
0 commit comments