Skip to content

Replace code generation with hand-kept constants and JSON test data - #348

Merged
smelfungus merged 5 commits into
color-composefrom
color-constants
Sep 24, 2026
Merged

smelfungus merged 5 commits into
color-composefrom
color-constants

Conversation

@smelfungus

@smelfungus smelfungus commented Sep 24, 2026 •

Copy link
Copy Markdown
Member

Builds on #347. Nothing in the repository writes Kotlin any more.

  • Constants. tools/generate_color_constants.py and its CI step are gone; ColorConstants.kt is edited by hand. ColorConstantsDerivationTest (JVM) derives the whites, the sRGB and Display P3 matrices, their inverses and the four LMS shortcuts in exact fractions and requires each literal to be the nearest double, bit for bit, printing the list to paste on failure. The Bradford and Oklab matrices are checked against CSS Color 4's printed text and against the whites they must carry, within 1e-15 (they hold to 5.7e-16).
  • Reference data. The WPT and color.js vectors were Kotlin string literals and listOf tables written by a script. They are now JSON under color/src/commonTest/resources/reference/, read through kotlinx-resources and decoded strictly into @Serializable classes on JVM, Android host, wasm and iOS. tools/reference/export.mjs (renamed from generate.mjs) writes them with JSON.stringify and fails on a failed fetch or an empty list. Every case carries over unchanged: 206 valid colors, 25 color() templates, 405 invalid texts and 348 conversions from WPT; 1,000 conversions and 400 Okhsl/Okhsv colors from color.js.
  • Named colors. NAMED_COLORS is checked against the named-color table in CSS Color 4's own source, pinned at the 2026-09-13 Editor's Draft: every name with its value, none missing, none extra.
  • The Compose mapping table in toColorValue's KDoc is aligned in the source.

New test-only dependencies: kotlinx-serialization-json 1.11.0 and kotlinx-resources 0.16.2.

A literal moved by one ulp, a changed or misspelled named color, and a broken case in either JSON file each fail the tests; the broken JSON fails on JVM, Android host and wasm alike. Tests: color 171 JVM, 162 Android host, 162 wasm; apiCheck clean.

A Python generator for sixteen arrays that change only when a built-in space arrives cost the build a second language and CI a step of its own.

ColorConstantsDerivationTest recomputes every derived literal in exact fractions and fails on a one-ulp difference, printing the list to paste. The matrices CSS publishes are checked against its printed text, and against the whites they must carry, to within 6e-16.
It rendered correctly but read as a jumble in the source; README's tables are padded, and this one now is too.
A rename on its own, so history follows the file through the rewrite that makes it export JSON instead of writing Kotlin.
The WPT lists and the color.js vectors lived in Kotlin files that a script wrote as string literals and listOf tables, one of them ported by a script that was never committed. They are now two JSON files under commonTest/resources: export.mjs writes them with JSON.stringify, and the tests read them through kotlinx-resources and decode them into @serializable classes, on JVM, Android host, wasm and iOS alike.

Every case carries over unchanged: 206 valid colors, 25 color() templates, 405 invalid texts and 348 conversions from WPT, 1,000 conversions and 400 Okhsl/Okhsv colors from color.js. export.mjs also fails on a failed fetch or an empty list, where it used to write empty data that passed.
NAMED_COLORS was tested for its size and two entries only. export.mjs now takes the named-color table from CSS Color 4's Bikeshed source at the 2026-09-13 Editor's Draft, checking each row's hex against its decimal, and the test holds the library's table to it exactly: every name with its value, none missing, none extra, and each one parsing to that color.

WPT's computed named-color list could not serve as the reference: it leaves out rebeccapurple.
@smelfungus smelfungus changed the title Keep the color constants by hand and verify them in a test Replace code generation with hand-kept constants and JSON test data Sep 24, 2026
@smelfungus
smelfungus merged commit 20de4d2 into color-compose Sep 24, 2026
2 checks passed
@smelfungus
smelfungus deleted the color-constants branch September 24, 2026 21:00
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