Expand source code
def preview_colors(
hex_codes: list[str],
output_path: str,
rect_width: int = 40,
font_size: int = 20,
padding: int = 10,
width: int = 600,
# font_path: str = "/Library/Fonts/Arial Unicode.ttf",
):
def get_height(data):
return len(data) * (font_size + padding) + padding
img = Image.new("RGB", (width, get_height(hex_codes)), "white")
d = ImageDraw.Draw(img)
# font = ImageFont.truetype(font_path, font_size)
y_position = padding
for hex_code in hex_codes:
d.rectangle(
(
padding,
y_position,
padding + rect_width,
y_position + font_size,
),
fill=hex_code,
)
d.text(
(padding + rect_width + padding, y_position),
hex_code,
# font=font,
fill="black",
)
y_position += font_size + padding
img.save(output_path)