File size: 1,512 Bytes
bd00f85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"""The prompts, and the color ground truth.

The font ground truths ship as rendered images under ``references/`` -- one
PIL render of the case's word in its pinned font file (provenance per case in
``data/fonts.jsonl``: google/fonts commit, file path, sha256). The color
ground truth is defined here: a solid rectangle on white, which scores exactly
zero through the color metric.
"""

from __future__ import annotations

from PIL import Image, ImageDraw

from .cases import FontCase

CANVAS = 1024

FONT_PROMPT_TEMPLATE = (
    'A plain white image. The word "{word}" in solid black, centered, in the '
    "font {family} {variant}. Nothing else in the image."
)

COLOR_PROMPT_TEMPLATE = (
    "A plain white (#FFFFFF) image with a single solid rectangle of {hex} in "
    "the middle. No border, no shadow, no texture, no gradient."
)

RECT_FRACTION = 0.5  # rectangle side, as a fraction of the canvas


def font_prompt(case: FontCase) -> str:
    return FONT_PROMPT_TEMPLATE.format(
        family=case.family, variant=case.variant, word=case.word
    )


def color_prompt(hex_color: str) -> str:
    return COLOR_PROMPT_TEMPLATE.format(hex=hex_color.upper())


def render_color_reference(hex_color: str, canvas: int = CANVAS) -> Image.Image:
    image = Image.new("RGB", (canvas, canvas), "#FFFFFF")
    side = round(canvas * RECT_FRACTION)
    corner = (canvas - side) // 2
    ImageDraw.Draw(image).rectangle(
        (corner, corner, corner + side, corner + side), fill=hex_color
    )
    return image