"""Render a 32×32 pixel-art archery bow using Pillow."""

from pathlib import Path
from PIL import Image, ImageDraw


def draw_bow():
    image = Image.new("RGBA", (32, 32), (0, 0, 0, 0))
    draw = ImageDraw.Draw(image)
    outline = "#38241e"
    wood = "#aa6030"
    light = "#e1a353"

    # Taut bowstring, behind the wooden limbs.
    draw.line([(9, 3), (9, 28)], fill="#756452", width=1)
    draw.line([(10, 4), (10, 27)], fill="#e9dfbf", width=1)

    # Stepped silhouettes keep every edge aligned to the pixel grid.
    upper = [(8, 2), (11, 2), (11, 4), (14, 4), (14, 6),
             (17, 6), (17, 8), (20, 8), (20, 11), (22, 11),
             (22, 14), (24, 14), (24, 16), (19, 16),
             (19, 13), (17, 13), (17, 10), (14, 10),
             (14, 8), (11, 8), (11, 6), (8, 6)]
    lower = [(x, 31 - y) for x, y in upper]
    for limb in (upper, lower):
        draw.polygon(limb, fill=outline)

    upper_wood = [(10, 4), (12, 5), (13, 7), (16, 7),
                  (16, 9), (19, 9), (19, 12), (21, 12),
                  (21, 15), (20, 15), (20, 12), (18, 12),
                  (18, 10), (15, 9), (12, 7), (10, 6)]
    for limb in (upper_wood, [(x, 31 - y) for x, y in upper_wood]):
        draw.polygon(limb, fill=wood)
    for points in (
        [(11, 5), (13, 6), (13, 7), (16, 8), (16, 9),
         (18, 10), (18, 11), (20, 13)],
        [(11, 26), (13, 25), (13, 24), (16, 23), (16, 22),
         (18, 21), (18, 20), (20, 18)],
    ):
        draw.line(points, fill=light, width=1)

    # Leather-wrapped handle with small brass collars.
    draw.rectangle((19, 13, 24, 18), fill=outline)
    draw.rectangle((20, 13, 23, 13), fill="#e4b558")
    draw.rectangle((20, 18, 23, 18), fill="#bc833e")
    draw.rectangle((20, 14, 23, 17), fill="#684335")
    draw.line([(20, 14), (22, 15)], fill="#b07c51")
    draw.line([(20, 16), (22, 17)], fill="#b07c51")
    draw.point((9, 3), fill="#ead6a1")
    draw.point((9, 28), fill="#ead6a1")
    return image


if __name__ == "__main__":
    output = Path(__file__).with_name("archery_bow.png")
    bow = draw_bow()
    bow.save(output)
    assert bow.size == (32, 32)
    print(f"Saved {output} ({bow.width}×{bow.height}, transparent RGBA)")
