Unexpected Text colour alpha in Jetpack Compose Material Theme
T

2

7

I discovered today that MaterialTheme applies an alpha to Text's colour. As you can see from the example attached, when I change the background colour, the text's colour appears to be different because it has a transparency value. I can force set a colour (Text(color = MaterialTheme.colors.onBackground, ....)) and this works correctly but I don't want to have to do this for every single Text.

Why does MaterialTheme do this? How do I override this behaviour?

Compose and Material Compose Material version: 1.2.1

@Preview
@Composable
private fun Preview_Playground() {
    MaterialTheme {
        Box(Modifier.background(Color.Green)) {
            Text("Test", fontWeight = FontWeight.ExtraBold, modifier = Modifier.alpha(1f))
        }
    }
}

enter image description here enter image description here

Terrazzo answered 13/10, 2022 at 2:9 Comment(0)
I
6

With M2 (androidx.compose.material) the color of the Text is defined by the color parameter or applying a TextStyle.
The default value is Color.Unspecified.
If color = Color.Unspecified and style has no color set, this will be LocalContentColor mixed with LocalContentAlpha.current.

In the Text.kt you can find:

val textColor = color.takeOrElse {
        style.color.takeOrElse {
            LocalContentColor.current.copy(alpha = LocalContentAlpha.current)
        }
    }

With M3 (androidx.compose.material3) it doesn't happen since LocalContentColor.current is not mixed:

val textColor = color.takeOrElse {
    style.color.takeOrElse {
        LocalContentColor.current
    }
}

If you have to use M2, you can define a custom composable for your Text, or you can change the LocalContentAlpha in the theme for the whole application (not only the Text):

MaterialTheme(
    colors = colors,
    typography = Typography,
    shapes = Shapes
){

    CompositionLocalProvider(LocalContentAlpha provides 0.5f) {
        content()
    }
}
Irrevocable answered 13/10, 2022 at 5:50 Comment(1)
That does it correctly. Thank you. Mixing colour with the current alpha, and the default alpha value < 1 in M2 are real head scratchers.Terrazzo
O
0
 Text(
              text = header,
                    modifier = Modifier.padding(start = 8.dp, top = 8.dp)
                        .background(
                            color = MaterialTheme.colorScheme.primary.copy(alpha = .7f),
                            shape = RoundedCornerShape(8.dp)
                        )
                        .clip(RoundedCornerShape(8.dp))
                        .padding(start = 8.dp, end = 8.dp, top = 4.dp, bottom = 4.dp),
                    fontWeight = FontWeight.ExtraBold,
                    fontSize = 14.sp,
                    fontFamily = displayFontFamily,
                    color = MaterialTheme.colorScheme.onPrimaryContainer.copy(alpha = 1f)
                )

We can use like this,i.e provide seperately alpha to background and text

Obsolete answered 9/9 at 6:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.