Files
colorhash/colorhash/svg.py
Alek Ratzloff 1def2fa7bf Update colors to be expressed explicitly as RGB or HSL
Generally, palettes are expressed using HSL, but sometimes we want the
output to be in RGB (or some other format). Colors are now expressed
using either RGBColor or HSLColor and can easily be converted between
the two.

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
2024-05-30 11:02:11 -07:00

30 lines
893 B
Python

"SVG-related functions."
from .colorizer import ColorMatrix
def gensvg(matrix: ColorMatrix, square_size: int) -> str:
"""
Generate an SVG based on a given matrix.
: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.
"""
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.to_html_color()}" />\n'
# Close SVG string
svg += "</svg>"
return svg