"""Single-color audit: matches images where one color dominates beyond
the threshold (within the given Euclidean RGB tolerance). The canonical
predicate for BOTH surfaces: FC-Cleanup's retroactive audit and — since
2026-07-02 — the import-side filter (Importer._single_color_hit /
SkipReason.single_color), so what the audit flags and what the import
skips can never disagree.
"""

from PIL import Image

_THUMB_SIZE = (64, 64)


def evaluate(
    pil_image,
    *,
    threshold: float,
    tolerance: int,
) -> bool:
    """True iff the fraction of pixels within `tolerance` (Euclidean RGB
    distance) of the dominant color exceeds `threshold`.

    Downsamples to 64x64 for speed (~4ms regardless of source size).
    Alpha channels are stripped; only RGB is considered. Animated images
    use frame 0 (PIL's default after Image.open without seek).
    """
    im = pil_image
    if im.mode == "RGBA":
        im = im.convert("RGB")
    elif im.mode not in ("RGB", "L"):
        im = im.convert("RGB")
    if im.size != _THUMB_SIZE:
        im = im.resize(_THUMB_SIZE, Image.Resampling.BILINEAR)
    pixels = list(im.getdata())
    if not pixels:
        return False
    # Normalize L-mode pixels to RGB tuples for distance math.
    if isinstance(pixels[0], int):
        pixels = [(p, p, p) for p in pixels]
    # Dominant color = mean RGB.
    n = len(pixels)
    sum_r = sum(p[0] for p in pixels)
    sum_g = sum(p[1] for p in pixels)
    sum_b = sum(p[2] for p in pixels)
    dom = (sum_r / n, sum_g / n, sum_b / n)
    tol_sq = tolerance * tolerance
    within = 0
    for r, g, b in pixels:
        dr = r - dom[0]
        dg = g - dom[1]
        db = b - dom[2]
        if dr * dr + dg * dg + db * db <= tol_sq:
            within += 1
    return (within / n) > threshold
