"""Draw a 32 x 32 pixel ribbon bow and export a transparent PNG."""

from pathlib import Path

from PIL import Image, ImageDraw


SIZE = 32
PALETTE = {
    "outline": "#492338",
    "deep": "#93274F",
    "shadow": "#BF3564",
    "rose": "#E94F7F",
    "pink": "#FA7197",
    "light": "#FFA7BF",
    "shine": "#FFD4DF",
}


def draw_bow():
    image = Image.new("RGBA", (SIZE, SIZE), (0, 0, 0, 0))
    draw = ImageDraw.Draw(image)

    def polygon(points, color, *, mirror=False, outline=False):
        if mirror:
            points = [(SIZE - 1 - x, y) for x, y in points]
        draw.polygon(
            points,
            fill=PALETTE[color],
            outline=PALETTE["outline"] if outline else None,
        )

    # The two forked ribbon tails sit behind the loops.
    for mirror in (False, True):
        polygon(
            [(12, 15), (16, 17), (14, 22), (13, 28),
             (10, 25), (6, 27), (8, 22), (10, 18)],
            "shadow", mirror=mirror, outline=True,
        )
        polygon(
            [(12, 18), (14, 18), (12, 25), (10, 23), (8, 25), (10, 20)],
            "rose", mirror=mirror,
        )
        polygon(
            [(12, 18), (13, 18), (11, 22), (9, 24), (10, 21)],
            "pink", mirror=mirror,
        )

    # Integer coordinates and a limited palette keep every edge pixel sharp.
    for mirror in (False, True):
        polygon(
            [(3, 5), (5, 4), (7, 4), (11, 6), (14, 10), (15, 12),
             (15, 16), (12, 18), (8, 20), (4, 20), (2, 18),
             (1, 15), (1, 9), (2, 6)],
            "shadow", mirror=mirror, outline=True,
        )
        polygon(
            [(3, 7), (5, 6), (7, 6), (10, 8), (13, 11), (14, 14),
             (12, 16), (8, 18), (5, 18), (3, 16), (2, 13), (2, 9)],
            "rose", mirror=mirror,
        )
        polygon(
            [(4, 6), (7, 6), (10, 8), (12, 10), (13, 12),
             (10, 10), (7, 8), (4, 8), (3, 10), (3, 8)],
            "pink", mirror=mirror,
        )
        polygon(
            [(4, 6), (7, 6), (9, 7), (10, 8), (7, 7), (4, 7), (3, 8)],
            "light", mirror=mirror,
        )
        polygon(
            [(4, 14), (7, 13), (12, 13), (14, 14), (11, 16),
             (7, 18), (5, 17)],
            "shadow", mirror=mirror,
        )
        polygon(
            [(6, 14), (10, 13), (13, 13), (12, 14), (8, 15)],
            "deep", mirror=mirror,
        )
        polygon([(4, 6), (6, 6)], "shine", mirror=mirror)

    # A rounded central knot covers the joins and gathers the ribbon.
    polygon(
        [(14, 10), (17, 10), (19, 12), (19, 16), (17, 18),
         (14, 18), (12, 16), (12, 12)],
        "shadow", outline=True,
    )
    polygon(
        [(14, 11), (17, 11), (18, 13), (18, 15), (16, 17),
         (14, 16), (13, 15), (13, 13)],
        "pink",
    )
    polygon([(14, 11), (17, 11), (17, 12), (14, 12), (13, 13)], "light")
    polygon([(17, 13), (18, 13), (18, 16), (16, 17), (16, 15)], "rose")
    draw.point((14, 11), fill=PALETTE["shine"])
    return image


if __name__ == "__main__":
    destination = Path(__file__).resolve().parent
    bow = draw_bow()
    bow.save(destination / "bow.png")
    bow.resize((256, 256), Image.Resampling.NEAREST).save(
        destination / "bow-preview.png"
    )
    print(f"Saved {destination / 'bow.png'}: {bow.width} x {bow.height}, RGBA")
