Skip to content

Generate clean 2D game sprites & animation atlases — component-row pipeline: state rows, alpha cleanup, frame extraction, runtime atlases. Codex/Claude skill.

docs(chroma): decontam palette method, guards, measurements, limits

Soohong Kim1537054da18a
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 1818416..5375ff0 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,10 @@
All notable public changes to `sprite-gen` are recorded here. Versions track the `version:` field in `SKILL.md` and `pyproject.toml`.
+## Unreleased (v2.7.0)
+
+- Add opt-in edge decontamination, `decontam: "palette"`. After the matte, each edge pixel is explained as a blend of the locally measured key background with one colour from a palette learned on the subject's own interior. The colour written is that palette colour at the pixel's observed luma, so thin hair strands and outlines come back in the subject's hue: not green, and not the orange that despill's channel gain produces. Available as `cutout --decontam palette`, `gen --transparent --decontam palette` (chroma keying), `video-frames` / `video-set --decontam palette` (video fit, one palette per clip) and request `chroma.decontam` for the row extractor and `inspect`. The default `off` returns every existing output byte for byte. On a procedural ground-truth set, key contamination at the edge falls from 22.5 % to 0.07 % on stills and from 33 % to 11 % on H.264 frames; halo against the true composite shrinks on dark and white backgrounds, and strand recall rises from 84 % to 99 % (stills) and from 82 % to 91 % (frames). Method, guards and limits: [docs/chroma-alpha.md](docs/chroma-alpha.md).
+
## v2.6.0 - Automatic local gait selection and XY motion correction
- In `motion-auto`, an uncertain fine XY search-boundary match can emit the selected frames unchanged only after the existing rendered-cell seam gate and animation checks pass. Reports and the CLI expose the rejected measurement and unapplied correction. Other failures and explicit `motion` registration remain strict.
diff --git a/docs/chroma-alpha.md b/docs/chroma-alpha.md
index 228b5c1..fba7ae0 100644
--- a/docs/chroma-alpha.md
+++ b/docs/chroma-alpha.md
@@ -60,6 +60,8 @@ When unsure, let `--chroma-key auto` sample the base. It excludes the detected f
2. **Soft-alpha unmix** — key-tinted blends near the keyed region are separated into despilled RGB + **partial alpha**: the blend model `observed = (1-k)·subject + k·key` is solved from the key-tint score, so antialiased silhouettes keep their coverage ramp instead of collapsing into a binary staircase. In-band blends (inside `--fringe-key-threshold`) are eligible only at key-distance `<= 2`; out-of-band blends use `--fringe-unmix-reach` (default 4). This preserves deeper key-tinted material while fixing trapped blend pockets between hair strands.
3. **Trapped-spill despill** — a small connected cluster of key-tinted pixels (≤ `--spill-max-fraction` of the subject, default 0.005) containing at least one strongly tinted pixel is generator spill, wherever it sits: its color is corrected in place with alpha kept (no pinholes). Large key-tinted regions are intentional material and are never touched; marginally warm subject colors (skin) never qualify.
+An opt-in fourth pass, edge decontamination (`chroma.decontam: "palette"`, below), then re-explains the edge with the subject's own colours.
+
The unmix/spill tunables live in the run's `sprite-request.json` under `chroma` (`unmix_reach`, `spill_max_fraction`) — the extractor reads them from there, CLI flags override, and the effective values are written back to the request so every run records what produced it. Then it extracts connected components and writes fresh transparent cells. The pixel-unfake path is unaffected: it binarizes alpha downstream (α ≥ 128 → opaque), so soft-alpha input degrades gracefully. This is intentionally closer to hatch-pet than to simple `magick -transparent`.
If component extraction cannot find the declared frame count, the row is blocked. `--allow-slot-fallback` exists for explicit debugging only; it must be reported as `slots-explicit` and is not the default path.
@@ -76,6 +78,58 @@ Port of perfectpixel-studio's `chroma.go` (MIT, see NOTICE). The default RGB pat
Select with request `chroma.mode: "ycbcr"` or `--chroma-mode ycbcr`. **The default stays `"rgb"`**: on clean flat-key raws the RGB path's exact-solve unmix already removes key tint completely, while ycbcr's fixed-scale despill leaves a small tinted soft-edge halo — ycbcr is for degraded sources (shaded/gradient backgrounds, JPEG chroma noise), not a general upgrade.
+## `decontam: "palette"` — give the edge the subject's own colour back (opt-in)
+
+The unmix above separates a blend by its key-tint score, and that assumes the subject's own colour carries no tint (grey, white). A saturated subject breaks the assumption. A crimson hair strand blended a third of the way into green scores as barely tinted, so it stays opaque and olive; where the full spill pass does reach it, the `1/(1-k)` gain of `despill_color` turns it orange. Video adds a second loss: H.264 4:2:0 has already averaged the key into the chroma of every strand one or two pixels wide. Its luma survives at full resolution.
+
+`decontam: "palette"` re-explains the edge after the matte (either matte). It learns a 32-colour palette from the subject's confident interior (opaque, deeper than 6 px) and fits every subject pixel within 6 px of the keyed background, and every keyed pixel within 3 px of the subject, as
+
+ observed = alpha · P + (1 − alpha) · B
+
+B is the key background measured locally (mean of keyed pixels within 12 px). P is the palette colour whose line through B passes closest to the observation, and alpha is the projection onto that line. The colour written is P's chroma at the observation's luma: the hue comes from the subject, the light and shade from the pixel. When the subject owns no key-hued material (≤ 0.5 % of its interior carries the key hue, the same test `video-frames --spill auto` runs on its reference still), the palette is key-free, so the output cannot carry the key's hue and no channel is amplified.
+
+| The pixel | Outcome |
+|---|---|
+| no palette colour explains it (a colour the interior never shows), or, in the still fit, the explanation misses its luma (outline ink darker than any blend) | engine bytes |
+| closer to a palette colour as observed than the blend explains it (margin 10, or 3 noise sigmas of the background) | engine bytes; with a key-free palette, a leftover key hue is capped (keyed channel ≤ largest other channel + 8), which never raises a channel |
+| pushed toward the key clearly more than the fit misses by, but not proven partial coverage | colour only |
+| a blend within the unmix reach (`chroma.unmix_reach`, default 4) | alpha and colour |
+| a blend deeper in the 6 px band | colour only, so a key reflection on a solid surface is recoloured rather than made see-through |
+| a keyed pixel within 3 px of the subject that fits a blend above the background's noise floor and chains to the subject | coverage recovered: the faint sides of a strand the hard cut erased |
+
+Interior pixels are never touched, `off` returns the engine's output byte for byte, and everything is integer-exact or a fixed sequence of float64 operations (no random seed, no iteration-order dependence). The palette is a histogram k-means with a greedy weighted farthest-point seed and a fixed iteration count. There is no new dependency.
+
+Two fits. `still` chooses P per pixel. `video` chooses P on a 3×3 mean of the observation, whose chroma is blurred anyway, and averages the projection alpha with a luma-only alpha where P and B differ in luma. `video-frames` uses the video fit and learns one palette per clip on the first frame, so the colours an edge may take cannot change from frame to frame.
+
+| Entry point | Flag |
+|---|---|
+| `sprite-gen cutout IMAGE` | `--decontam palette` (chroma routes; the white matte refuses it) |
+| `sprite-gen gen … --transparent` | `--decontam palette` (chroma strategy only; refused before the provider is called otherwise) |
+| `sprite-gen video-frames`, `video-set` | `--decontam palette` |
+| row extractor (`extract`, `inspect`) | request `chroma.decontam: "palette"`, or `--decontam palette` to override; written back to the request once in play |
+
+Reports record what the pass did under `decontam`: mode, fit, the palette, and changed / refit / tint / recovered / unexplained pixel counts.
+
+Measured on a procedural ground-truth set (red strands 0.5–3 px wide, outline and interior ink, highlights, translucent wisps, white cloth and a gold accent on a painted green key; a still, and an H.264 4:2:0 round trip at CRF 16). Key contamination counts edge pixels covered in both output and truth whose hue turned 12° or more toward the key (green residue and the despill's warm drift both turn that way), or, on near-grey truth, whose a*/b* moved toward the key by 8. Halo is the mean CIEDE2000 against the true composite on #0A0A0D and on white:
+
+| Source | Path | Key contamination | Halo dark / white | Strand recall |
+|---|---|---|---|---|
+| still | RGB engine (default) | 22.5 % | 6.65 / 7.16 | 84.3 % |
+| still | RGB engine + `decontam` (still fit) | 0.07 % | 1.14 / 1.14 | 99.0 % |
+| still | YCbCr matte (`gen`) | 19.1 % | 9.77 / 9.36 | 77.3 % |
+| still | YCbCr matte + `decontam` | 0.16 % | 1.15 / 1.14 | 99.0 % |
+| H.264 frames | RGB engine + `--spill full` | 33.2 % | 12.79 / 12.43 | 82.1 % |
+| H.264 frames | + `decontam` (video fit) | 10.8 % | 9.38 / 8.73 | 91.4 % |
+
+**The default stays `off`.** It returns the current output byte for byte, and every published run and golden fixture depends on those bytes.
+
+Limitations:
+
+- An edge takes one of the subject's interior colours at its observed brightness. A thin feature whose colour appears nowhere inside the subject keeps the engine's pixels.
+- In video a thin strand's chroma is gone before keying, so its colour is chosen from blurred chroma; strands come back in the hair's hue, sometimes a shade darker or paler than drawn.
+- A subject that owns key-coloured material keeps it in its palette, and its edges are repainted with whatever explains them best, key colour included. Keep the default when that material matters.
+- The pixel-unfake path binarizes alpha downstream, so on pixel-art rows only edge colours change.
+
## Related
- [`../SKILL.md`](../SKILL.md) — 필수 게이트 (크로마 키 소재색 분기 + 변환 후 소재색 보존 검증)
diff --git a/docs/video-pipeline.md b/docs/video-pipeline.md
index bdf3498..8782838 100644
--- a/docs/video-pipeline.md
+++ b/docs/video-pipeline.md
@@ -172,6 +172,18 @@ count even on an edge. The report names the metric, band and dark-only policy.
Genuine key-coloured material above the 0.5% share still keeps the conservative
mode. Tiny accents or dark, edge-only material can fall below that reference test; use `--spill small` when preserving those is essential.
+### Edges: `--decontam palette`
+
+`--spill` fixes key colour painted *into* the subject. The strands and outlines at its
+edge are a different problem. H.264 4:2:0 has already averaged the key into the chroma of
+anything one or two pixels wide, and despilling what is left turns thin red strands
+orange. `video-frames --decontam palette` (also `video-set --decontam palette`) re-explains
+each edge pixel as a blend of the local key background with one colour the subject owns,
+and writes that colour at the pixel's observed luma. It uses the video fit and one palette
+per clip, learned on the first frame, so edge colours cannot flicker between palettes. The
+default `off` keeps frames byte-identical. Method, guards and measurements:
+[chroma-alpha.md](chroma-alpha.md#decontam-palette--give-the-edge-the-subjects-own-colour-back-opt-in).
+
## 3b. Canvas shape for raised limbs and wide costumes
`video-set` picks the canvas from the state row alone. Two things that are not a jump or