#!/usr/bin/env python3
"""Generates assets/hound.png: a pixel-art bloodhound, lightly dithered.

Hand-drawn sprite grid, sparse Bayer shading, nearest-neighbor upscale.
Deterministic on purpose; rerun after editing the grid.
"""

from PIL import Image

K = (24, 24, 27)       # black (outline, ear, nose)
O = (232, 93, 31)      # orange fur
Y = (212, 225, 87)     # yellow-green chest patch
C = (247, 243, 233)    # cream (eye)
BG1 = (199, 210, 231)  # panel light blue
BG2 = (215, 224, 241)  # panel dither dot

# Sitting bloodhound, side profile facing left. One char per pixel:
# K black · o orange (dithered) · y yellow (dithered) · c cream · . panel
SPRITE = """
........................................
..........KKKKKK........................
.........KooooooKK......................
........KoooooooooK.....................
.......KoocKooooooKK....................
......KKooooooooooKKK...................
....KKKoooooooooooKKKK..................
...KKooooooooooooKKKKKK.................
...KoooooooooooooKKKKKK.................
...KKKooooooooooKKKKKKK.................
.....KKoooooooooKKKKKK..................
.......KoooooooooKKKK...................
.......KKoooooooooKK....................
........KyyooooooooK....................
........KyyyoooooooKK...................
........KyyyooooooooKKKKKKK.............
........KyyooooooooooooooooKKK..........
........KyooooooooooooooooooooKK........
........KyoooooooooooooooooooooK........
........KyoooooooooooooooooooooK..KKK...
........KooooooooooooooooooooooK.KooK...
........KooooooooooooooooooooooKKooK....
........KoooooooooooooooooooooooooK.....
........KKoooooKKKKKKooooooooooooK......
.........KooooK......KooooooooooK.......
.........KooooK.......KoooooooooK.......
.........KooooK.......KoooooKKKKK.......
.........KKooKK.......KKooooK...........
..........KKKK.........KKKKKK...........
........................................
"""


def build(scale=10, pad=4):
    rows = SPRITE.strip("\n").split("\n")
    h, w = len(rows), max(len(r) for r in rows)
    img = Image.new("RGBA", (w + pad * 2, h + pad * 2), (*BG1, 255))
    px = img.load()

    # Sparse dither dots on the panel
    for y in range(img.height):
        for x in range(img.width):
            if (x % 4 == 1 and y % 4 == 3) or (x % 4 == 3 and y % 4 == 1):
                px[x, y] = (*BG2, 255)

    def shade(base, x, y):
        # Sparse Bayer: darken one cell in eight for texture
        if (x % 4 == 0 and y % 4 == 0) or (x % 4 == 2 and y % 4 == 2):
            return tuple(int(v * 0.55) for v in base)
        return base

    for y, row in enumerate(rows):
        for x, ch in enumerate(row.ljust(w, ".")):
            color = None
            if ch == "K":
                color = K
            elif ch == "o":
                color = shade(O, x, y)
            elif ch == "y":
                color = shade(Y, x, y)
            elif ch == "c":
                color = C
            if color is not None:
                px[x + pad, y + pad] = (*color, 255)

    img = img.resize((img.width * scale, img.height * scale), Image.NEAREST)
    img.save("assets/hound.png")
    print(f"assets/hound.png {img.width}x{img.height}")


if __name__ == "__main__":
    build()
