Medium May 3, 2022

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

Critical Insight

A simple Composable extension function bypasses Material's elevation restrictions, enabling custom color overlays that respect your design system while maintaining proper elevation hierarchy.

The implementation uses a clever alpha calculation formula that scales with elevation depth—and it's only three lines of actual code.

About This Article

Problem

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.

Solution

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.

Impact

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.