2024-05-22 11:15:43 -07:00
|
|
|
from .colorizer import StrMatrix
|
2024-05-21 18:23:14 -07:00
|
|
|
|
|
|
|
|
|
2024-05-22 11:15:43 -07:00
|
|
|
def gensvg(matrix: StrMatrix, square_size: int) -> str:
|
2024-05-21 18:23:14 -07:00
|
|
|
"""
|
|
|
|
|
Generate an SVG based on a given matrix.
|
|
|
|
|
"""
|
|
|
|
|
h = len(matrix)
|
|
|
|
|
w = len(matrix[0])
|
|
|
|
|
|
|
|
|
|
# Start SVG string
|
|
|
|
|
svg = f'<svg width="{w * square_size}" height="{h * square_size}" xmlns="http://www.w3.org/2000/svg">\n'
|
|
|
|
|
|
|
|
|
|
# Generate grid
|
|
|
|
|
for r in range(h):
|
|
|
|
|
for c in range(w):
|
|
|
|
|
x = c * square_size
|
|
|
|
|
y = r * square_size
|
|
|
|
|
color = matrix[r][c]
|
|
|
|
|
svg += f' <rect x="{x}" y="{y}" width="{square_size}" height="{square_size}" fill="{color}" />\n'
|
|
|
|
|
|
|
|
|
|
# Close SVG string
|
|
|
|
|
svg += "</svg>"
|
|
|
|
|
return svg
|