https://kotlinlang.org logo
#compose
Title
# compose
d

David Corrado

02/17/2022, 10:23 PM
Kind of hard to describe but in Compose is it possible to detect the background color. So on a surface you can for example set the color to MaterialTheme.colors.surface or MaterialTheme.colors.primary. So I have a custom composable function wrapper for Button. I want it to be certain colors onSurface and another color onPrimary.
m

mkrussel

02/17/2022, 10:26 PM
If the code for the background updates the
LocalContentColor
then that will provide the correct color for the background.
Surface
takes it's color and looks up to see if it matches colors in the
MaterialTheme
if it does it sets the
LocaleContentColor
to match the
on
version for that. See
Colors.contentColorFor
But the general rule is that you don't look up the tree to see what to do, but let the parent view tell you what you should be doing.
c

Chris Sinco [G]

02/18/2022, 2:36 AM
You might consider getting the value of a known color key, e.g. MaterialTheme.colors.surface (or whatever key you know is being used for the background), in a composition, and then do a calculation to figure out what color to use. It sounds like you are making color choices based on contrast of the button with a background it sits on, versus the previous comment above is more mapping keys together semantically (<x> with on<x>).
d

David Corrado

02/18/2022, 8:26 PM
Thanks both of you. I got it working and understand what both of you are saying. Helped me learn how compose works more with colors. Thanks again
👍 1
c

Chris Sinco [G]

02/18/2022, 8:31 PM
Happy to help!