"""潮騒 / Sound of the Tide — stacked sine bands painted back to front."""
import math
import numpy as np
from PIL import Image, ImageDraw
from _common import W, H, C, rgb, smooth1d, paper, cli


def render(seed, rows=38):
    rng = np.random.default_rng(seed)
    bg, cols = C["kinari"], [C["fuji"], C["gunjo"], C["ai"], C["kon"]]
    img = Image.new("RGB", (W, H), rgb(bg)); d = ImageDraw.Draw(img)
    xs = np.linspace(-40, W + 40, 700)
    for r in range(rows):
        y0 = H * 0.08 + r * (H * 0.9 / rows) + np.sin(xs / W * math.pi * 2 + r * 0.3) * 60
        n = smooth1d(700, rng, 4, 1.2)
        ys = y0 - n * 90
        c = rgb(cols[r % len(cols)])
        poly = list(zip(xs, ys)) + [(xs[-1], H + 10), (xs[0], H + 10)]
        d.polygon(poly, fill=c)
        d.line(list(zip(xs, ys)), fill=rgb(bg), width=6)  # the white seam is just background colour
    return paper(img, rng)


SEED = 31

if __name__ == "__main__":
    cli(render, SEED)
