skip to content

In Jetpack Compose Material 3, how does Text inside a Surface get a readable color without a color argument, and when does that fail?

level: middleimportance: should knowfreq 46%

answer

  1. background and 'on' pairs
  2. a composition local for text
  3. Surface's default content color
  4. lookup by color value

basics

~20 s

Surface sets LocalContentColor to contentColorFor(its color), which maps a scheme background such as primary to its on-role, onPrimary. Text and Icon default to LocalContentColor. It fails when the background is not a scheme color, or is painted with Modifier.background.

solid answer

~40 s

`Surface` defaults `contentColor` to `contentColorFor(color)` and provides it as `LocalContentColor` to its content. `contentColorFor` looks the background up in the current `ColorScheme`: `primary` gives `onPrimary`, `surface` and the `surfaceContainer*` roles give `onSurface`, and so on. `Text` uses its `color` argument, else its style's color, else `LocalContentColor.current`; `Icon`'s `tint` defaults to it too. The lookup compares color values, so a literal brand color that is not in the scheme finds no match and the inherited content color is kept, which can be unreadable. `Modifier.background` paints but provides no content color at all. Fix both by using scheme roles, passing `contentColor` explicitly, or wrapping content in `CompositionLocalProvider(LocalContentColor provides ...)`.

go deeper

for a junior

Remember that text inside a Surface or Button takes its color from the container, so you usually omit the color argument.

for a middle

Explain Surface's contentColorFor default, LocalContentColor, and Text's order: argument, then style color, then content color.

for a senior

Diagnose unreadable text quickly: an off-scheme background literal, a bare Modifier.background, or a color baked into a typography style.

for a principal

Set a rule that backgrounds come from scheme roles or token classes, so content color resolution never meets a literal.

## Content color in Material 3 Material's color system works in **pairs**: a background role and the "on" role designed to be readable on it (`primary` and `onPrimary`, `surface` and `onSurface`, `primaryContainer` and `onPrimaryContainer`). Jetpack Compose's Material 3 library carries the "on" half down the tree as the **content color**, held in the composition local **`LocalContentColor`**. Its documentation describes it as the preferred color for text and iconography at a given position, so that content adjusts when the background changes. If nothing ever provides it, its default is `Color.Black`. ## How a `Surface` sets it `Surface` has these defaults: - `color = MaterialTheme.colorScheme.surface` - `contentColor = contentColorFor(color)` and it provides `LocalContentColor provides contentColor` to its content. **`contentColorFor(backgroundColor)`** looks the background up in the current `ColorScheme`: `primary` maps to `onPrimary`, `secondaryContainer` to `onSecondaryContainer`, `surface` and every `surfaceContainer*` role to `onSurface`, and so on. The lookup compares **color values**, not role names, so the background has to equal one of the scheme's colors. ## How `Text` and `Icon` pick it up - `Text` resolves its color in order: the `color` argument if specified, else the `style`'s color if it has one, else `LocalContentColor.current`. - `Icon`'s `tint` defaults to `LocalContentColor.current`. So in `Surface(color = MaterialTheme.colorScheme.primary) { Text("Pay now") }` the text is `onPrimary` without any color argument. Components like `Button` and `Card` work the same way: their colors objects carry a container and a content color, and the component provides the content color to its slot. ```kotlin @Composable fun TransferBanner() { Surface(color = MaterialTheme.colorScheme.primary) { Row { Icon(painterResource(R.drawable.ic_info), contentDescription = null) // tinted onPrimary Text("Transfers over the limit need approval") // colored onPrimary } } } ``` ## When it fails | Situation | What happens | Fix | |---|---|---| | `Surface(color = Color(0xFF0B5CAD))`, a literal not in the scheme | `contentColorFor` finds no match and returns the inherited `LocalContentColor`, often `onSurface`, which may be unreadable on dark blue | use a scheme role, or pass `contentColor` explicitly | | `Box(Modifier.background(primary))` | `Modifier.background` paints but provides no content color; text keeps the outer value | use `Surface`, or wrap with `CompositionLocalProvider(LocalContentColor provides ...)` | | a `TextStyle` in the typography with a color baked in | the style's color wins over the content color everywhere that style is used | keep colors out of typography styles | | `Text` under `MaterialTheme` but with no `Surface` or component above it | `MaterialTheme` itself does not provide a content color, so `LocalContentColor` is still `Color.Black`, often unreadable in dark theme | put a `Surface` at the root of each screen | The non-composable variant, `ColorScheme.contentColorFor(color)`, returns `Color.Unspecified` when there is no match instead of falling back to `LocalContentColor`; use it when you want to detect the miss. ## Setting it yourself Use `CompositionLocalProvider(LocalContentColor provides MaterialTheme.colorScheme.error) { ... }` to recolor every `Text` and `Icon` in a region, for instance an error banner, without passing `color` to each one. Providers nest: the innermost value wins for its subtree. ## Content color in your own components The same mechanism serves custom components, and using it keeps them consistent with Material ones: - A custom component that paints its own background should provide a content color for its slot, either by building on `Surface` or with `CompositionLocalProvider(LocalContentColor provides contentColorFor(background))`. - A component that draws its own glyphs or lines on a `Canvas` should read `LocalContentColor.current` during composition and use that color, so it adapts to whatever container hosts it. - A component that accepts a `content` slot should not force a color on the slot's `Text`; let the content color flow so callers can still override it with a provider. - Pairs that are not in the scheme, such as a brand "caution" color and its readable partner, need both halves defined as tokens so the component can provide the partner explicitly. ## What an interviewer is checking 1. That you know text color in Material 3 is usually implicit, coming from the nearest surface or component. 2. That you can name `LocalContentColor` and `contentColorFor` and the role pairing behind them. 3. That you can explain the two classic bugs: an off-scheme background color and a bare `Modifier.background`. The habit that prevents both is to build backgrounds from color roles, not literals, and to let `Surface` or a Material component set the content color for you.

  • What does ColorScheme.contentColorFor return when there is no match, compared with contentColorFor?
    The extension `ColorScheme.contentColorFor(color)` returns `Color.Unspecified` when the color is not one of the scheme's backgrounds. The composable `contentColorFor(color)` uses that result and falls back to `LocalContentColor.current`. Use the extension when you need to detect the miss.
  • How do you recolor every Text and Icon in an error banner without passing color to each?
    Wrap the banner's content in `CompositionLocalProvider(LocalContentColor provides MaterialTheme.colorScheme.onErrorContainer)` over an `errorContainer` background, or simply use `Surface(color = MaterialTheme.colorScheme.errorContainer)`, which provides the matching on-color for you.

  • Text color always comes from the typography style, never from the surface.
  • Modifier.background also sets the content color for the text inside it.
  • contentColorFor matches by role name, so any custom blue maps to onPrimary.
  • LocalContentColor defaults to the theme's onSurface even with no provider.