File: chalk.py

package info (click to toggle)
markdown-exec 1.10.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 740 kB
  • sloc: python: 2,497; javascript: 180; makefile: 32; sh: 30
file content (23 lines) | stat: -rw-r--r-- 637 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from tempfile import NamedTemporaryFile
from chalk import Diagram, triangle, unit_x
from colour import Color

papaya = Color("#ff9700")

def sierpinski(n: int, size: int) -> Diagram:
    if n <= 1:
        return triangle(size)
    else:
        smaller = sierpinski(n - 1, size / 2)
        return smaller.above(smaller.beside(smaller, unit_x).center_xy())

d = sierpinski(5, 4).fill_color(papaya)

# Chalk doesn't provide an easy method to get a string directly,
# so we use a temporary file.
with NamedTemporaryFile("w+") as tmpfile:
    d.render_svg(tmpfile.name, height=256)
    tmpfile.seek(0)
    svg = tmpfile.read()

print(svg)