#!/usr/bin/env python3 """Compare two LookCaptureTests captures (COTTON_LOOK_CAPTURE=). python3 scripts/look-diff.py A screen is the same when its pixel hash matches, or when its visible pixels are equal (a fully transparent pixel is one colour whatever bytes it carries). A screen the capture marks UNSTABLE — it drew differently twice in one run — is listed apart and not compared. Exits 1 and names each screen that differs or is missing on one side. RULE (architect, 13 Oct 2026): the ONE tolerance is a colour rounding — every channel of a pixel may differ by at most one unit (of 255), the image the same size, and nothing else. It exists because a theme writes its colours as 8-bit hex (`#2E2925`) where Swift had written floats (0.18, 0.16, 0.145), and the hex is the right form; each channel of a blended (antialiased) pixel rounds on its own, so one pixel can move by one in two channels (measured on the lookdata review set: 30 of 5,639 changed pixels on the todo page). It is NOT a licence for any other difference: a moved line, a changed size, a different object or a colour off by two is a real difference and fails. """ import pathlib import sys from PIL import Image, ImageChops def manifest(directory: pathlib.Path) -> dict[str, str]: lines = (directory / "manifest.txt").read_text().splitlines() return dict(line.split(" ", 1) for line in lines if line) def visible(image: Image.Image) -> Image.Image: """The pixels as seen: a fully transparent pixel is one colour, whatever bytes it carries (a few faces vary those run to run).""" clear = Image.new("RGBA", image.size, (0, 0, 0, 0)) return Image.composite(image, clear, image.getchannel("A").point(lambda a: 255 if a else 0)) def within_rounding(x: Image.Image, y: Image.Image) -> bool: """Every channel of every pixel within one unit (the RULE above).""" return all(channel.getextrema()[1] <= 1 for channel in ImageChops.difference(x, y).split()) def main(before: pathlib.Path, after: pathlib.Path) -> int: a, b = manifest(before), manifest(after) differ = [] unstable = sorted(name for name in set(a) | set(b) if "UNSTABLE" in (a.get(name, "") + b.get(name, ""))) for name in sorted(set(a) | set(b)): if name in unstable: continue if a.get(name) == b.get(name): continue if name not in a or name not in b: differ.append(f"{name} (missing on one side)") continue x = visible(Image.open(before / f"{name}.png").convert("RGBA")) y = visible(Image.open(after / f"{name}.png").convert("RGBA")) if x.size != y.size or not within_rounding(x, y): differ.append(name) if unstable: print(f"unstable (drew differently twice in one run, not compared): {', '.join(unstable)}") print(f"{len(a)} screens; differ: {len(differ)}", *differ, sep="\n") return 1 if differ else 0 if __name__ == "__main__": sys.exit(main(pathlib.Path(sys.argv[1]), pathlib.Path(sys.argv[2])))