Cotton code
68 lines · 3.0 KB · PythonRaw
#!/usr/bin/env python3"""Compare two LookCaptureTests captures (COTTON_LOOK_CAPTURE=<dir>).    python3 scripts/look-diff.py <before-dir> <after-dir>A screen is the same when its pixel hash matches, or when its visible pixelsare 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 screenthat differs or is missing on one side.RULE (architect, 13 Oct 2026): the ONE tolerance is a colour rounding — everychannel of a pixel may differ by at most one unit (of 255), the image the samesize, and nothing else. It exists because a theme writes its colours as 8-bithex (`#2E2925`) where Swift had written floats (0.18, 0.16, 0.145), and the hexis the right form; each channel of a blended (antialiased) pixel rounds on itsown, so one pixel can move by one in two channels (measured on the lookdatareview set: 30 of 5,639 changed pixels on the todo page). It is NOT a licencefor any other difference: a moved line, a changed size, a different object or acolour off by two is a real difference and fails."""import pathlibimport sysfrom PIL import Image, ImageChopsdef 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 0if __name__ == "__main__":    sys.exit(main(pathlib.Path(sys.argv[1]), pathlib.Path(sys.argv[2])))