ursus
10/26/2022, 8:57 PMonX
colors have some sort of weird tint when used implcitly?
I have a parent surface whose color is set to MaterialTheme.colors.primary
, and then just a simple Text
where its inferred text color should be onPrimary
and it some off-white, when it should be pure white
If I have another Text
on top of it which color explicitly set to onPrimary
then it is white as it should.
compose version 1.2.1
Snippet in the thread
Francesc
10/26/2022, 9:03 PMursus
10/26/2022, 9:05 PM@Composable
fun HeaderRow(item: Header) {
Surface(
color = MaterialTheme.colors.primary,
modifier = Modifier
.fillMaxWidth()
.wrapContentHeight()
) {
Column(
modifier = Modifier
.fillMaxWidth()
.wrapContentHeight()
.padding(
start = DefaultPadding,
end = DefaultPadding,
top = 4.dp,
bottom = SectionPadding / 2,
),
horizontalAlignment = Alignment.Start
) {
val greeting = "Dobrý večer"
Box {
Text(
modifier = Modifier
.wrapContentSize()
.padding(bottom = 4.dp),
text = greeting,
style = MaterialTheme.typography.h2,
)
Text(
modifier = Modifier
.wrapContentSize()
.padding(bottom = 4.dp, start = 4.dp),
text = greeting,
style = MaterialTheme.typography.h2,
color = MaterialTheme.colors.onPrimary <--------------------
)
}
Text(
modifier = Modifier.wrapContentSize(),
text = item.tariffName,
style = MaterialTheme.typography.caption,
)
}
}
}
ursus
10/26/2022, 9:19 PMmattinger
10/27/2022, 1:15 AMLocalContentAlpha
and LocalContentColor
. You might want to see what the values are for those during this composition. The particular content alpha is generally based on luminance of the LocalContentColor.ursus
10/27/2022, 1:24 AMursus
10/27/2022, 1:25 AMmattinger
10/27/2022, 1:26 AMmattinger
10/27/2022, 1:26 AMMaterialTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colors.primary
) {
Column {
Text(text = "Hello Android!", color = MaterialTheme.colors.onPrimary, style = MaterialTheme.typography.h2,)
CompositionLocalProvider(
LocalContentAlpha provides 0.5f
) {
Text(text = "Hello Android2!", color = MaterialTheme.colors.onPrimary, style = MaterialTheme.typography.caption,)
}
}
}
}
mattinger
10/27/2022, 1:28 AMmattinger
10/27/2022, 1:32 AMmattinger
10/27/2022, 1:32 AMmattinger
10/27/2022, 1:33 AMursus
10/27/2022, 1:34 AMursus
10/27/2022, 1:39 AMmattinger
10/27/2022, 2:10 AMColor.Unspecified
. When that happens, the text engine will go through a series of things. It will look at your typography first to see if it has a color (which would generally also be Color.Unspecified
. If that happens it will fall back to the LocalContentColor. And as we've seen when you create a surface with a color, it defaults the content color to this:mattinger
10/27/2022, 2:10 AMcontentColor: Color = contentColorFor(color),
mattinger
10/27/2022, 2:13 AM@Composable
private fun contentAlpha(
/*@FloatRange(from = 0.0, to = 1.0)*/
highContrastAlpha: Float,
/*@FloatRange(from = 0.0, to = 1.0)*/
lowContrastAlpha: Float
): Float {
val contentColor = LocalContentColor.current
val lightTheme = MaterialTheme.colors.isLight
return if (lightTheme) {
if (contentColor.luminance() > 0.5) highContrastAlpha else lowContrastAlpha
} else {
if (contentColor.luminance() < 0.5) highContrastAlpha else lowContrastAlpha
}
}
mattinger
10/27/2022, 2:14 AMmattinger
10/27/2022, 2:24 AMcolor = LocalContentColor.current.copy(alpha=1.0f)