Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Material3 Color Harmonization in Jetpack Compose

While using the view-based system, we can harmonize a color using the below code.

MaterialColors.harmonizeWithPrimary(context, colorToHarmonize)

In a project that is fully migrated to Jetpack Compose and all the color values are stored in the Color.kt file, how do you apply harmonization to color at runtime?

like image 795
Mehul Kanzariya Avatar asked Apr 11 '26 06:04

Mehul Kanzariya


1 Answers

You can use the method

MaterialColors.harmonize(colorToHarmonize, colorToHarmonizeWith)

And harmonize your custom color with the primaryColor from the dynamic color scheme

MaterialColors.harmonize(customColor.toArgb(), MaterialTheme.colorScheme.primary.toArgb())

If you want to go the extra step you can provide the harmonized value(s) through CompositionLocalProvider to provide it down the composition, to match the way dynamic colors are provided by MaterialTheme.colorScheme.

I explored the issue a little more in this project: https://github.com/acolombo25/harmonized-colors-compose

like image 66
Andrea Avatar answered Apr 12 '26 19:04

Andrea



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!