#!/usr/bin/env python3
"""Recolour the theme-picker thumbnails.

These are miniature screenshots rather than widget art, so unlike the rest of
assets/ they draw on the whole Nord palette at once -- including hues that
appear nowhere else in the theme. Every Nord entry is anchored here so none of
them falls through to a neutral.
"""

import os
import sys

sys.path.insert(0, os.path.join(os.path.dirname(__file__), "lib"))

import darkmatter as dm
from png import Png
from remap import Remapper

NORD = [
    ("#2e3440", dm.DARK),        # polar night -> the surface ladder
    ("#3b4252", dm.BASE),
    ("#434c5e", dm.BG),
    ("#4c566a", dm.BORDER),
    ("#232831", dm.SHADOW),
    ("#d8dee9", dm.FG),          # snow storm -> foreground
    ("#e5e9f0", dm.FG),
    ("#eceff4", dm.FG),
    ("#8fbcbb", dm.ORANGE),      # frost -> the accent ramp
    ("#88c0d0", dm.ORANGE),
    ("#81a1c1", dm.ORANGE_L1),
    ("#5e81ac", dm.ORANGE_D1),
    ("#bf616a", dm.RED),         # aurora
    ("#d08770", dm.ORANGE),
    ("#ebcb8b", dm.CREAM),
    ("#a3be8c", dm.TEAL),
    ("#b48ead", dm.CREAM),
    ("#000000", "#000000"),
    ("#ffffff", dm.FG),
]

TARGETS = ["gtk-3.0/thumbnail.png", "gtk-4.0/thumbnail.png"]


def main():
    remap = Remapper([(dm.rgb(s), dm.rgb(t)) for s, t in NORD])
    for path in TARGETS:
        if not os.path.exists(path):
            continue
        img = Png(path)
        if img.color_type == 3:
            img.set_palette([remap(c) for c in img.palette()])
        elif img.color_type == 6:
            img.set_rgba([remap(p[:3]) + (p[3],) for p in img.rgba()])
        else:
            print("  skipped (colour type %d): %s" % (img.color_type, path))
            continue
        img.save()
        print("  recoloured %s" % path)


if __name__ == "__main__":
    main()
