2024-05-22 12:05:20 -07:00
|
|
|
"SVG-related functions."
|
2024-05-30 11:02:11 -07:00
|
|
|
from .colorizer import ColorMatrix
|
2024-05-21 18:23:14 -07:00
|
|
|
|
|
|
|
|
|
2024-05-30 11:02:11 -07:00
|
|
|
def gensvg(matrix: ColorMatrix, square_size: int) -> str:
|
2024-05-21 18:23:14 -07:00
|
|
|
"""
|
|
|
|
|
Generate an SVG based on a given matrix.
|
2024-05-22 12:05:20 -07:00
|
|
|
|
|
|
|
|
:param matrix: the color matrix to generate the SVG for.
|
|
|
|
|
:param square_size: the size of the squares generated, in pixels.
|
|
|
|
|
:returns: the full generated SVG as a string.
|
2024-05-21 18:23:14 -07:00
|
|
|
"""
|
|
|
|
|
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]
|
2024-05-30 11:02:11 -07:00
|
|
|
svg += f' <rect x="{x}" y="{y}" width="{square_size}" height="{square_size}" fill="{color.to_html_color()}" />\n'
|
2024-05-21 18:23:14 -07:00
|
|
|
|
|
|
|
|
# Close SVG string
|
|
|
|
|
svg += "</svg>"
|
|
|
|
|
return svg
|