Archie
11/23/2021, 5:25 PMgeorgiy.shur
11/23/2021, 7:05 PMColors.contentColorFor(backgroundColor: Color)
in compose material, not sure if that’s what you’re looking for.georgiy.shur
11/23/2021, 7:07 PMZach Klippenstein (he/him) [MOD]
11/23/2021, 7:12 PMChris Sinco [G]
11/24/2021, 3:32 AMChris Sinco [G]
11/24/2021, 3:33 AMArchie
11/24/2021, 6:11 AMcontentColorFor(...)
But doesn’t that just depend on the defined color in the material theme?
fun Colors.contentColorFor(backgroundColor: Color): Color {
return when (backgroundColor) {
primary -> onPrimary
primaryVariant -> onPrimary
secondary -> onSecondary
secondaryVariant -> onSecondary
background -> onBackground
surface -> onSurface
error -> onError
else -> Color.Unspecified
}
}
Tobias Suchalla
11/24/2021, 6:18 AMfun Color.getContrastColor(): Color = if (luminance() > 0.5) Color.Black else Color.White
But as the others said, this does not necessarily conform to accessability standards. But it works well enough for my usecase.