Skip to content

Pick Algorithm Weight automatically and make K-means repeatable - #248

Merged
vycdev merged 6 commits into
developfrom
t3/seeded-kmeans-auto-weight
Nov 30, 2026
Merged

vycdev merged 6 commits into
developfrom
t3/seeded-kmeans-auto-weight

Conversation

@vycdev

@vycdev vycdev commented Nov 30, 2026 •

Copy link
Copy Markdown
Owner

Cleaning up an image in 2D was cumbersome, and one reason was a setting nobody could use well. Algorithm Weight sets the size of an intermediate palette, but measurements showed the default of 128 made K-means and Wu results worse and slower than reducing straight to the target. Also, K-means started from Math.random, so two Applies of the same settings could give different colors.

This PR hides Weight and picks it per algorithm, and seeds K-means so results repeat. It's the first of the approved color-cleanup PRs.

Weight rule (src/lib/algorithmWeight.ts)

Case Weight
K-means, Wu Number of Colors
Median-cut 4 × Number of Colors, 128–256
Octree 4 × Number of Colors, 64–256
Posterize max(128, Number of Colors)
Fixed palette 64 (Posterize: 128)
Delete, image ≤256 colors none: the algorithm is skipped
Delete, image >256 colors 128, as before

Delete on images with 256 colors or fewer now maps straight to the remaining swatches, so only the deleted color's pixels move, for every algorithm. Before, an image with more than 128 colors was first reduced to 128, and Posterize could merge two similar swatches nobody deleted.

Measured against the old default of 128 (line art 1600×1600 and a 1800×1350 photo, 8/16/32 colors):

  • K-means and Wu were never worse and usually clearly better. Line art at 16 colors with K-means: 97% of pixels in the right region instead of 85%.
  • Octree was within 0.04 mean ΔE or better.
  • Median-cut is unchanged up to 32 colors.

Faster K-means. The k-means++ setup now updates each color's nearest-centroid distance incrementally. The output is byte-identical, and the goldens are unchanged.

Photo, 1800×1350 Before After
K-means, 8 colors (Weight 128 → 8) 2.4 s 0.35 s
K-means, Weight 256 4.2 s 0.9 s

It also reports progress while it seeds and refines, and yields to the page during long scans.

Results change for existing users in two ways: the new Weight values, and the seeded K-means.

Other changes:

  • A selected palette that can't be resolved now falls back to Auto's count.
  • Deleting the last opaque swatch is now a no-op.
  • The reset arrow restores Auto, 16 colors and K-means.
  • The Algorithm select now has the id its label already pointed to.
  • Docs (the reducing-colors guide and its pipeline and swatch-operations diagrams) and locales are updated in all 12 languages, and diagram measurements are regenerated.

Note for #220: project files there save quantize.weight. After this lands, #220 should keep reading the field for older files and ignore it.

Deferred to the Delete rework (PR 4 of the plan): Delete's target palette is RGB-only, so two swatches with the same RGB and different alpha can't be told apart. This predates this PR, and the rework replaces Delete-by-requantize with a direct RGBA remap. Like every quantize run, Delete still makes partially transparent pixels opaque; the guide says so in its pipeline section.

Tests

  • Unit tests for the rule and countOpaqueColors.
  • Pinned K-means goldens, plus a check that K-means never calls Math.random.
  • A Node test that recomputes the browser golden through the real Apply steps.
  • @smoke browser tests:
    • two K-means Applies on Rocco produce the same pixels as Node;
    • the reset arrow takes a named palette back to Auto;
    • deleting a swatch with Posterize keeps both reds exactly and can be undone.

Checks run locally: lint, tsc -b, npm test (1312 pass), test:i18n, test:docs, check:i18n, test:e2e:all on port 47391: all suites passed after rebasing onto develop with #247 (smoke 199, docs 6, landing 19, privacy 13, terms 10, not-found 10, i18n 17, i18n build 4), with no port collisions.

Council review

Rounds: 4

Delegate Round 1 Round 2 Round 3 Round 4
Claude Opus 5.5 8/10 9/10 9/10 9.5/10
GPT-6.2 Sol 7/10 8/10 9/10 9/10

No third provider (Gemini or another) was enabled in this environment, so the council ran with two members.

Created with Claude Opus 5.5 in Claude Code (T3 Code). Reviews by Claude Opus 5.5 and GPT-6.2 Sol.

vycdev and others added 6 commits November 30, 2026 14:57
Remove the Algorithm Weight field. The intermediate palette size now
follows a measured per-algorithm rule (src/lib/algorithmWeight.ts):
Number of Colors for K-means and Wu, 4x clamped to 128-256 for
Median-cut and 64-256 for Octree, at least 128 for Posterize, 64 for
fixed palettes, and 256 when deleting a swatch.

K-means now draws from a seeded mulberry32 stream instead of
Math.random, so the same image and settings always give the same
colors.

Docs, the pipeline diagram and locales are updated in all 12
languages. Adds unit tests for the rule, pinned K-means goldens, and a
browser smoke test that matches the Node golden.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Council round 1. Deleting a swatch from an image with 256 colors or
fewer now skips the first stage, so only that color's pixels move for
every algorithm (Posterize used to merge similar swatches); larger
images keep the old 128. A selected palette that can't be resolved
falls back to Auto's count, and deleting the last opaque swatch is a
no-op.

K-means++ setup now updates each color's nearest-centroid distance
with only the newest centroid, which gives identical output in
O(k*n), and yields while it works.

Docs state the Median-cut, Octree and fixed-palette Posterize values
in all 12 languages. Adds a Node test behind the browser golden and
smoke tests for palette reset and Posterize Delete.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Council round 2. K-means reports progress while seeding, refining and
building its lookup, and yields inside long scans; output is
unchanged. The Posterize Delete smoke test now checks each stripe's
exact color, which the old path failed. Docs scope the "choose None
before Delete" advice to images with more than 256 colors in all 12
languages.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Council round 3. The swatch-operations diagram now says Delete moves
only the deleted color's pixels up to 256 colors and to choose None
above that, in all 12 languages. K-means reports progress inside each
refinement pass. The Posterize Delete smoke test asserts that an image
was recorded and that Undo brings the deleted swatch back.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Council round 4: the German, Spanish, Hindi and Bengali lines now name
None and Eraser exactly as the controls do.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
develop now opens on a drop zone (#247); load the sample the way the
other specs do.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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