Skip to content

Keep detail and a palette gap note for fixed palettes - #253

Merged
vycdev merged 5 commits into
developfrom
t3/palette-keep-detail
Nov 30, 2026
Merged

vycdev merged 5 commits into
developfrom
t3/palette-keep-detail

Conversation

@vycdev

@vycdev vycdev commented Nov 30, 2026 •

Copy link
Copy Markdown
Owner

For fixed palettes (built-in, supplier or custom; not Auto):

  • Keep detail switch, off by default. With it on, mapImageToPalette picks the palette color for all source colors together instead of each one's nearest. Energy: Σ nᵢ·ΔE² + λ × the edge contrast lost between colors that touch + a penalty for flipping which side of an edge is lighter. It is solved with ICM sweeps in a fixed order (pixel count, then first appearance), and ties keep the current color, so it is deterministic. It uses prototype B3's parameters: λ 12, μ 4, candidates within nearest + 22 ΔE (at most 5), contrast cap 30, min edge 4. It solves at most the 1,024 most common colors; the rest take their nearest palette color, which only happens with Algorithm None on a photo.
  • Palette gap note in Quantization Settings after Apply: the share of visible pixels more than ΔE 20 from every enabled palette color. It shows from 25% and names the dominant tone (OKLCH) when at least half of those pixels share it. It is informational, uses the primary (blue) tokens, and shows only while the image that Apply made is current and the same palette, with the same enabled colors, is selected.

With Keep detail off, the output is byte-identical to develop. SHA-256 goldens from origin/develop cover three synthetic palettes, rocco raw and rocco K-means 64 → Bambu. Asking for the gap does not change the pixels either.

Persistence: Algorithm and Number of Colors are plain session state; only the Palette selection is saved. Keep detail matches them: session state, turned off by the Reset arrow. No Strength control: λ is fixed at the research default of 12, and the approved UI is one switch. In the B3 sweep, a higher λ only helped a palette-starved case: λ 120 moved the jacket's skirt to gray, and λ 400 overreached. Adding the missing filament fixes that case better, which is what the gap note suggests.

Before / after

Source after K-means 64 | nearest (today) | Keep detail, Bambu Lab PLA Basic. Each crop is the 300×300 window where the two results differ most.

Synthetic scene: Keep detail keeps the faint hair strands.

scene

rocco photo: more fur detail and the green rim survive, but the gray beak area picks up specks of nearby palette colors. That is the trade-off of turning it on for busy photos, and the guide says so.

rocco

Image (after K-means 64) Palette Gap note Mean ΔE nearest → keep Edges kept nearest → keep
jacket, 400×400 crop of the maintainer's image (no picture posted) Bambu PLA Basic 36%, dark 18.22 → 18.37 80.7% → 90.2%
jacket crop screenshot-3 palette (11) 47%, dark 19.84 → 19.99 39.1% → 49.4%
synthetic scene 1600² Bambu PLA Basic 7% (hidden) 14.24 → 14.37 60.0% → 91.2%
synthetic scene screenshot-3 palette 8% (hidden) 15.66 → 15.88 41.4% → 76.5%
rocco 1000×835 Bambu PLA Basic 19% (hidden) 14.03 → 16.80 48.8% → 85.8%
rocco p32 56% 22.84 → 25.78 40.0% → 81.8%

The research measured 81% → 92% on the full jacket image. The 25% threshold sits between the palettes that suit an image (19% or less) and the jacket (32% on the full image, 36–47% on the crop).

Timing (Node, Bambu PLA Basic, whole mapping step):

Image Nearest Nearest + gap Keep detail + gap
1000×835, K-means 64 18 ms 18 ms 34 ms
2000×2000, K-means 64 38 ms 54 ms 100 ms
4000×3000, K-means 64 143 ms 141 ms 270 ms
4000×3000, Algorithm None 130 ms 145 ms 262 ms
2000×2000, 4,096 random colors (worst case from review) 129 ms — 141 ms

Every phase yields, so the longest event-loop block is about 33 ms. A synthetic 12 MP image with 8.7M distinct colors is slow (5–8 s) in today's nearest path too; that path stays byte-identical, so it is left alone.

Coordination

Tests

  • tests/paletteMapping.test.ts, 17 tests:
    • Keep detail off is byte-identical (goldens).
    • Keep detail on: deterministic and pinned by a golden; only palette colors; alpha handled like nearest; touching stripes stay apart when the palette has room; no edges means nearest; settles within the sweep limit; the 1,024-color cap (heap selection matches a full sort); duplicate palette entries; a palette too large for the distance table gives the same result.
    • Gap: matches brute force in both modes; transparent and partial alpha; tone naming, with no tone on an even split; threshold; visibility rules.
  • tests/e2e/quantize-determinism.spec.ts (@smoke): with the 4-gray palette (nothing darker than 20% L) and Algorithm None, the note reads "40% … mostly dark tones. Consider adding a dark filament."; nearest merges the stripes and Keep detail keeps them apart; Undo and another palette hide the note; Reset turns the switch off.
  • lint, tsc -b, npm test, test:i18n, test:docs, check:i18n, test:e2e:all (KROMACUT_E2E_PORT=50177).

Docs: reducing-colors.md (the table and the fixed-palette section) in all 12 languages; UI strings in all 12 locales; CHANGELOG.

Council review

Gemini isn't reachable from this environment (only Claude and Codex are enabled), so the council had two members.

Rounds: 5

Delegate Round 1 Round 2 Round 3 Round 4 Round 5
Claude Opus 5.5 7.5/10 8.5/10 9/10 9.5/10 10/10
GPT-6.2 Sol 6.5/10 7.5/10 8/10 8.5/10 10/10
Gemini Pro unavailable unavailable unavailable unavailable unavailable

Fixed:

  • Main-thread stalls: the solve is capped at 1,024 colors with a dense boundary table, a heap picks the top colors, and every phase yields. The gap is counted inside the mapping instead of in an extra pass.
  • Duplicate palette entries no longer use up candidate slots.
  • Large imported palettes compute distances on demand instead of building a table.
  • The note goes stale after an in-place palette edit; it now also hides then.
  • An even tone split no longer names a tone.
  • "a orange" → "an orange".
  • The guide now says which colors are solved together and what the percentage counts.
  • More tests.

Declined:

  • A worker for Keep detail: the yields already keep blocks near 35 ms.
  • UI wording "of the image": it is the approved wording, and the guide says what it counts.
  • Reset staying disabled under Auto while Keep detail is on: the control is hidden there, and the reviewers disagreed.
  • Slow cases in the unchanged nearest path (millions of colors, huge palettes): it must stay byte-identical.

🤖 Generated with Claude Code

vycdev and others added 5 commits November 30, 2026 17:25
Keep detail (off by default) solves the palette assignment for all source
colors at once: Σ n·ΔE² plus λ times the edge contrast lost between touching
colors, plus a penalty for flipping which side is lighter, solved with
deterministic ICM sweeps (prototype B3, λ 12). With it off, mapping is
byte-identical to before.

After an Apply with a fixed palette, a note in Quantization Settings says how
much of the image is more than ΔE 20 from every enabled palette color, and
which tone most of it is, once that reaches a quarter of the image.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…e note to the palette's colors

- Keep detail solves at most 1024 colors with a dense boundary table and
  cached palette distances, and yields while indexing and solving.
- The palette gap is counted inside mapImageToPalette from each color's
  nearest palette color instead of in a separate synchronous pass.
- Duplicate palette entries no longer use up Keep detail's candidates.
- The gap note also hides when the selected palette's enabled colors change.
- The Reset arrow's modified state ignores Keep detail while Auto is selected.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
- Palette-pair distances are tabled only for palettes of up to 256 colors.
- Candidate setup, indexing and writeback yield, also across transparent pixels.
- The nearest cache entry is built with its gap verdict in one literal.
- The guide says which colors Keep detail solves together and what the gap
  percentage counts; Keep detail's output is pinned by a golden hash.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@vycdev
vycdev force-pushed the t3/palette-keep-detail branch from a495ea9 to ccb3be2 Compare November 30, 2026 15:25
@vycdev
vycdev merged commit 8e0a4f4 into develop Nov 30, 2026
27 of 28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant