"""Independent PNG-only inverse arm for longest vertical step selection.""" from __future__ import annotations import numpy as np from PIL import Image # Public visual palette, duplicated deliberately instead of importing renderer state. _PALETTE: dict[str, tuple[int, int, int]] = { "red": (214, 54, 64), "blue": (40, 112, 184), "green": (35, 142, 82), "orange": (226, 126, 34), } _COLOR_DISTANCE = 32.0 _MIN_INK_PIXELS = 350 _MIN_VERTICAL_SPAN = 14 def _longest_vertical_extent(mask: np.ndarray) -> int: extents: list[int] = [] for x in np.flatnonzero(mask.any(axis=0)): ys = np.flatnonzero(mask[:, x]) if ys.size: extents.append(int(ys[-1] - ys[0] + 1)) if not extents: raise ValueError("no colored stroke columns found") return max(extents) def decision_from_image(image: Image.Image) -> str: arr = np.asarray(image.convert("RGB"), dtype=np.int32) if arr.ndim != 3 or arr.shape[2] != 3 or arr.shape[0] < 100 or arr.shape[1] < 100: raise ValueError("unsupported raster") scores: dict[str, int] = {} for name, rgb in _PALETTE.items(): delta = arr - np.asarray(rgb, dtype=np.int32) distance = np.sqrt(np.sum(delta * delta, axis=2, dtype=np.int32)) mask = distance <= _COLOR_DISTANCE if int(mask.sum()) < _MIN_INK_PIXELS: raise ValueError(f"missing or materially erased {name} curve") score = _longest_vertical_extent(mask) if score < _MIN_VERTICAL_SPAN: raise ValueError(f"no measurable vertical step for {name}") scores[name] = score ranked = sorted(((score, name) for name, score in scores.items()), reverse=True) if ranked[0][0] - ranked[1][0] < 2: raise ValueError("pixel measurement is tied or ambiguous") return ranked[0][1]