"""Draw a 32 x 32 ribbon bow. Requires Pillow: pip install Pillow.

Run: python draw_bow.py
Outputs are saved beside this script; the preview uses nearest-neighbor scaling.
"""

from pathlib import Path

from PIL import Image, ImageDraw


OUT = Path(__file__).resolve().parent
PALETTE = {
    "outline": "#642039",
    "deep": "#9D2948",
    "shade": "#C83753",
    "red": "#ED4D68",
    "light": "#FF8292",
    "shine": "#FFC0BE",
}


def draw_bow():
    bow = Image.new("RGBA", (32, 32), (0, 0, 0, 0))
    d = ImageDraw.Draw(bow)

    def poly(points, color, outline=None, mirror=False):
        if mirror:
            points = [(31 - x, y) for x, y in points]
        d.polygon(points, fill=PALETTE[color],
                  outline=PALETTE[outline] if outline else None)

    # Two fork-cut ribbon tails, behind the loops and center knot.
    for mirror in (False, True):
        poly([(12, 14), (16, 16), (13, 28), (10, 25), (5, 26),
              (7, 21), (9, 16)], "shade", "outline", mirror)
        poly([(11, 17), (14, 17), (12, 25), (10, 23), (7, 24),
              (9, 20)], "red", mirror=mirror)
        poly([(10, 19), (11, 19), (9, 23), (8, 23)],
             "light", mirror=mirror)
        poly([(14, 18), (14, 21), (12, 26), (11, 25)],
             "deep", mirror=mirror)

    # Loops: stepped silhouettes, bright upper folds, dark gathered bases.
    for mirror in (False, True):
        poly([(4, 5), (7, 5), (9, 6), (11, 8), (14, 10),
              (15, 12), (15, 16), (12, 18), (8, 20), (4, 20),
              (2, 18), (2, 8)], "red", "outline", mirror)
        poly([(3, 14), (5, 16), (9, 16), (13, 13), (14, 14),
              (13, 16), (10, 18), (7, 19), (4, 19), (3, 17)],
             "shade", mirror=mirror)
        poly([(4, 17), (8, 18), (12, 15), (13, 16), (10, 18),
              (7, 19), (4, 19)], "deep", mirror=mirror)
        poly([(4, 8), (5, 6), (7, 6), (9, 7), (11, 9),
              (11, 10), (8, 9), (6, 9), (4, 12)],
             "light", mirror=mirror)
        poly([(5, 7), (7, 7), (8, 8), (5, 8), (4, 10), (4, 9)],
             "shine", mirror=mirror)
        poly([(7, 11), (9, 12), (12, 12), (13, 13),
              (11, 14), (9, 13)], "deep", mirror=mirror)

    # Rounded center wrap covers the gathers.
    poly([(14, 10), (17, 10), (18, 11), (18, 17), (17, 18),
          (14, 18), (13, 17), (13, 11)], "red", "outline")
    d.line([(14, 11), (16, 11)], fill=PALETTE["shine"])
    d.line([(14, 12), (14, 15)], fill=PALETTE["light"])
    d.line([(17, 12), (17, 16)], fill=PALETTE["shade"])
    d.line([(14, 17), (17, 17)], fill=PALETTE["deep"])
    return bow


if __name__ == "__main__":
    image = draw_bow()
    image.save(OUT / "bow.png")
    image.resize((512, 512), Image.Resampling.NEAREST).save(OUT / "bow-preview.png")
    with Image.open(OUT / "bow.png") as exported:
        assert exported.size == (32, 32) and exported.mode == "RGBA"
        assert set(exported.getchannel("A").getdata()) == {0, 255}
    print(f"Saved {OUT / 'bow.png'}: 32 x 32 RGBA, transparent background")
