Custom color elevation effect in Compose
Article Summary
Alexandre Bruneau from Medium Engineering discovered that Jetpack Compose's elevation overlay only works with default Material theme colors. His workaround unlocks custom color elevation for any design system.
While migrating Medium's Android app to Compose, Bruneau hit a frustrating limitation: elevated surfaces weren't showing the expected illumination effect in dark mode. Diving into the Surface composable source code revealed that Material Design's elevation overlay is hardcoded to only apply when using MaterialTheme.colors.surface—breaking custom themes entirely.
Key Takeaways
- Compose elevation overlay ignores custom colors, only works with MaterialTheme.colors.surface
- Custom extension function uses compositeOver with alpha based on elevation value
- Solution applies LocalAbsoluteElevation for consistent multi-level elevation effects
A simple Composable extension function bypasses Material's elevation restrictions, enabling custom color overlays that respect your design system while maintaining proper elevation hierarchy.
About This Article
Alexandre Bruneau found that Material Design's DefaultElevationOverlay uses the formula ((4.5f * ln(elevation.value + 1)) + 2f) / 100f to compute alpha, but only when the Surface color matches MaterialTheme.colors.surface exactly. This breaks custom design systems that need different colors.
Bruneau built a custom Color.applyElevationOverlay() extension function that swaps the default formula for a simpler one: (absoluteElevation.value) / 100f. It composites a custom theme color over the target surface using LocalAbsoluteElevation to keep elevation consistent across multiple levels.
The extension function worked well for dark theme surfaces, creating proper illumination effects on elevated elements. The before-and-after screenshots show better visual hierarchy without changing how Compose's Surface component works.