Material color and Monet module
Overview
The color/ module is a standalone Android library (color/build.gradle.kts) that exposes a small Compose/Material 3 palette API on top of Material color-science utilities. It publishes Compose UI/runtime/foundation/material3 dependencies as api, so app code can consume its Color, ColorScheme, CompositionLocal, and palette extension APIs directly.
The module has two package layers:
com.kyant.monetis the app-facing Monet/dynamic color layer. color/…/Monet.kt definesLocalTonalPalettes, tone shortcut extensions (Number.a1,Number.a2,Number.a3,Number.n1,Number.n2), anddynamicColorScheme(), which maps generated tones into a Material 3ColorSchemefor light or dark mode. color/…/TonalPalettes.kt definesTonalPalettes, a five-palette container foraccent1,accent2,accent3,neutral1, andneutral2tones. color/…/PaletteStyle.kt defines the supported Monet styles, and color/…/ColorSpec.kt is the chroma/hue-shift rule used by each style slot.io.material.hctandio.material.utilsare the Material color utility layer. color/…/Hct.kt, Cam16.kt, HctSolver.kt, and ViewingConditions.kt implement HCT/CAM16 color conversion and solving; color/…/ColorUtils.kt and MathUtils.kt provide sRGB, XYZ, Lab/L*, matrix, clamp, interpolation, and angle helpers. color/…/StringUtils.kt only formats ARGB as hex and is internal.
Palette generation starts from a Compose Color. TonalPalettes.Companion.Color.toTonalPalettes() in color/…/TonalPalettes.kt converts the key color to HCT, then precomputes Material 3 tone stops for accent palettes and neutral palettes. Accent and neutral tones are generated by Color.transform(), which reads the key color's HCT hue/chroma/tone, applies the selected ColorSpec chroma function and hue shift, sets the requested tone, and converts back to an ARGB-backed Compose Color via Hct.toInt(). The standard tone array includes 0, 10, 20, ... 100 plus 85/95/99; neutral1 uses a larger surface tone array with additional low and container tones such as 4, 6, 12, 17, 22, 24, 87, 92, 94, 96, and 98 because dynamicColorScheme() consumes those surface values.
Palette styles are declarative presets in color/…/PaletteStyle.kt. Each style supplies five ColorSpecs: accent1Spec, accent2Spec, accent3Spec, neutral1Spec, and neutral2Spec. Fixed styles such as TonalSpot, Spritz, Rainbow, and Monochrome use constant chroma and hue shifts. Content derives chroma from the source color's chroma. Vibrant and Expressive use hue-rotation tables and the private Double.hueRotation() helper to choose secondary/tertiary hue shifts by source hue range.
The generated palettes can be consumed in two ways:
- For full Material 3 theming, provide a
TonalPalettesvalue throughLocalTonalPalettesfrom color/…/Monet.kt, then calldynamicColorScheme(). The function chooses light or dark defaults usingisSystemInDarkTheme()unlessisLightis passed explicitly, and maps tones such as40.a1/80.a1to primary roles,40.a2/80.a2to secondary roles,40.a3/80.a3to tertiary roles, and neutral tones to background/surface/container/outline roles. - For direct color access in Composables, use the tonal extensions from color/…/Monet.kt: for example,
40.a1reads tone 40 fromaccent1, and98.n1reads tone 98 fromneutral1. These extensions are@Composablebecause they readLocalTonalPalettes.current.
TonalPalettes.Companion.ColorScheme.toTonalPalettes() in color/…/TonalPalettes.kt supports the reverse/bridge case: it derives tonal palettes from an existing Material 3 ColorScheme, using primary, secondary, tertiary, surface, and surfaceVariant as the key colors for the five tonal families. This conversion is explicitly style-independent and uses each role color's own HCT values rather than a PaletteStyle preset.
The HCT/CAM16 layer is the numerical engine behind those APIs. Hct in color/…/Hct.kt stores hue, chroma, tone, and ARGB together; mutating hue/chroma/tone re-solves a valid sRGB color through HctSolver.solveToInt(). Cam16 in color/…/Cam16.kt converts between ARGB/XYZ and CAM16 dimensions under ViewingConditions, and can measure distance in CAM16-UCS. ViewingConditions in color/…/ViewingConditions.kt caches adaptation values and provides an sRGB-like default. HctSolver in color/…/HctSolver.kt finds the closest displayable sRGB color for requested hue, chroma, and L* tone: it first tries a Newton solve by CAM16 lightness, then falls back to bisection along the RGB cube boundary to maximize available chroma at the target hue/tone. ColorUtils and MathUtils in color/src/main/java/io/material/utils/ provide the lower-level conversions and bounds used by those algorithms.
Routing guidance for color/** paths:
- Changes under
color/src/main/java/com/kyant/monet/**belong to the public Monet palette/theme API and should be reviewed for app-facing behavior: generated palette families, palette styles, tone stops,LocalTonalPalettes, anddynamicColorScheme()role mappings. - Changes under
color/src/main/java/io/material/hct/**belong to HCT/CAM16 conversion and solving behavior. Route them as color-science engine changes even when no Compose API changes. - Changes under
color/src/main/java/io/material/utils/**belong to shared color/math conversion support for the HCT engine; route them with HCT/CAM16 changes because small matrix, gamma, L*, or clamping changes alter generated palettes globally. - Changes to color/build.gradle.kts or
color/proguard-rules.probelong to the module packaging surface: Android namespace, Compose/Material dependencies, min/compile SDK, Java/Kotlin toolchain, and release minification.
Coupling
- If color/…/TonalPalettes.kt changes its tone arrays, also verify color/…/Monet.kt:
dynamicColorScheme()requests non-standard surface tones such as4.n1,6.n1,12.n1,17.n1,22.n1,24.n1,87.n1,92.n1,94.n1,96.n1, and98.n1. Missing precomputed entries still work through lazytransform(), but repeated theme reads will recompute those tones instead of using the cached map. - If a
PaletteStylepreset in color/…/PaletteStyle.kt changes chroma or hue-shift rules, verify callers that expect Android/Material-style Monet behavior fromColor.toTonalPalettes()in color/…/TonalPalettes.kt; the preset rules feed every generated accent and neutral family. - If color/…/Monet.kt changes role-to-tone mappings in
dynamicColorScheme(), verify both light and dark branches together. The sameNumber.a*/n*extensions read fromLocalTonalPalettes, so mismatched tones can break contrast across all consumers using the generatedColorScheme. - If color/…/Hct.kt changes mutation order or
HctSolver.solveToInt()usage, verifyColor.transform()in color/…/TonalPalettes.kt. It currently sets tone, then chroma, then hue on a singleHctinstance; each setter may reduce chroma to fit the sRGB gamut, so ordering affects generated colors. - If matrices, gamma functions, L*/Y conversions, or clamping change in color/…/ColorUtils.kt or color/…/MathUtils.kt, verify color/…/Cam16.kt, HctSolver.kt, and ViewingConditions.kt together. Those files share scientific constants and assume the same sRGB/XYZ/Lab scaling conventions.
- If dependencies in color/build.gradle.kts change away from
apior remove Compose Material 3, verify downstream app modules still seeColorScheme,Color, and the composable extensions exposed by color/…/Monet.kt and TonalPalettes.kt.
Risks
- color/…/HctSolver.kt, color/…/ColorUtils.kt, and color/…/MathUtils.kt are marked as automatically generated. Manual edits to constants or conversion formulas can shift every generated palette color, and the failure mode is visual drift rather than a compile error.
- color/…/TonalPalettes.kt accepts arbitrary
Doubletone values throughaccent1(tone),accent2(tone),accent3(tone),neutral1(tone), andneutral2(tone). Values outside the expected HCT tone range rely onHctSolver.solveToInt()falling back to grayscale for invalid/extreme tone inputs, so unchecked caller-provided tones can produce surprising colors.