Introducing a new way to review your PRs.

Read the announcement

junkfood02 / seal

🦭 Video/Audio Downloader for Android, based on yt-dlp

View on GitHub

13 documents · Updated Nov 16, 2026

docs / material-color-and-monet-module

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:

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 TonalPalettes value through LocalTonalPalettes from color/…/Monet.kt, then call dynamicColorScheme(). The function chooses light or dark defaults using isSystemInDarkTheme() unless isLight is passed explicitly, and maps tones such as 40.a1/80.a1 to primary roles, 40.a2/80.a2 to secondary roles, 40.a3/80.a3 to 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.a1 reads tone 40 from accent1, and 98.n1 reads tone 98 from neutral1. These extensions are @Composable because they read LocalTonalPalettes.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, and dynamicColorScheme() 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.pro belong 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 as 4.n1, 6.n1, 12.n1, 17.n1, 22.n1, 24.n1, 87.n1, 92.n1, 94.n1, 96.n1, and 98.n1. Missing precomputed entries still work through lazy transform(), but repeated theme reads will recompute those tones instead of using the cached map.
  • If a PaletteStyle preset in color/…/PaletteStyle.kt changes chroma or hue-shift rules, verify callers that expect Android/Material-style Monet behavior from Color.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 same Number.a*/n* extensions read from LocalTonalPalettes, so mismatched tones can break contrast across all consumers using the generated ColorScheme.
  • If color/…/Hct.kt changes mutation order or HctSolver.solveToInt() usage, verify Color.transform() in color/…/TonalPalettes.kt. It currently sets tone, then chroma, then hue on a single Hct instance; 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 api or remove Compose Material 3, verify downstream app modules still see ColorScheme, 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 Double tone values through accent1(tone), accent2(tone), accent3(tone), neutral1(tone), and neutral2(tone). Values outside the expected HCT tone range rely on HctSolver.solveToInt() falling back to grayscale for invalid/extreme tone inputs, so unchecked caller-provided tones can produce surprising colors.