Skip to content

Instantly share code, notes, and snippets.

@venator85
Last active October 17, 2024 17:28
Show Gist options
  • Save venator85/d988dfc3eed3cf3e3049e9c8dc634165 to your computer and use it in GitHub Desktop.
Save venator85/d988dfc3eed3cf3e3049e9c8dc634165 to your computer and use it in GitHub Desktop.
A Composable function designed to showcase all the colors of a Material theme, in both light and dark modes.
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material3.ColorScheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.luminance
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
@Composable
fun MaterialColorsPreview(
darkColorScheme: ColorScheme,
lightColorScheme: ColorScheme,
modifier: Modifier = Modifier,
) {
Column(modifier = modifier) {
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.End
) {
Text(
modifier = Modifier
.padding(horizontal = 8.dp)
.width(56.dp),
text = "Dark",
textAlign = TextAlign.Center
)
Text(
modifier = Modifier
.padding(horizontal = 8.dp)
.width(56.dp),
text = "Light",
textAlign = TextAlign.Center
)
}
LazyColumn(modifier = Modifier.fillMaxSize()) {
item { ColorRow("primary", darkColorScheme.primary, lightColorScheme.primary) }
item { ColorRow("onPrimary", darkColorScheme.onPrimary, lightColorScheme.onPrimary) }
item { ColorRow("primaryContainer", darkColorScheme.primaryContainer, lightColorScheme.primaryContainer) }
item { ColorRow("onPrimaryContainer", darkColorScheme.onPrimaryContainer, lightColorScheme.onPrimaryContainer) }
item { ColorRow("inversePrimary", darkColorScheme.inversePrimary, lightColorScheme.inversePrimary) }
item { ColorRow("secondary", darkColorScheme.secondary, lightColorScheme.secondary) }
item { ColorRow("onSecondary", darkColorScheme.onSecondary, lightColorScheme.onSecondary) }
item { ColorRow("secondaryContainer", darkColorScheme.secondaryContainer, lightColorScheme.secondaryContainer) }
item { ColorRow("onSecondaryContainer", darkColorScheme.onSecondaryContainer, lightColorScheme.onSecondaryContainer) }
item { ColorRow("tertiary", darkColorScheme.tertiary, lightColorScheme.tertiary) }
item { ColorRow("onTertiary", darkColorScheme.onTertiary, lightColorScheme.onTertiary) }
item { ColorRow("tertiaryContainer", darkColorScheme.tertiaryContainer, lightColorScheme.tertiaryContainer) }
item { ColorRow("onTertiaryContainer", darkColorScheme.onTertiaryContainer, lightColorScheme.onTertiaryContainer) }
item { ColorRow("background", darkColorScheme.background, lightColorScheme.background) }
item { ColorRow("onBackground", darkColorScheme.onBackground, lightColorScheme.onBackground) }
item { ColorRow("surface", darkColorScheme.surface, lightColorScheme.surface) }
item { ColorRow("onSurface", darkColorScheme.onSurface, lightColorScheme.onSurface) }
item { ColorRow("surfaceVariant", darkColorScheme.surfaceVariant, lightColorScheme.surfaceVariant) }
item { ColorRow("onSurfaceVariant", darkColorScheme.onSurfaceVariant, lightColorScheme.onSurfaceVariant) }
item { ColorRow("surfaceTint", darkColorScheme.surfaceTint, lightColorScheme.surfaceTint) }
item { ColorRow("inverseSurface", darkColorScheme.inverseSurface, lightColorScheme.inverseSurface) }
item { ColorRow("inverseOnSurface", darkColorScheme.inverseOnSurface, lightColorScheme.inverseOnSurface) }
item { ColorRow("error", darkColorScheme.error, lightColorScheme.error) }
item { ColorRow("onError", darkColorScheme.onError, lightColorScheme.onError) }
item { ColorRow("errorContainer", darkColorScheme.errorContainer, lightColorScheme.errorContainer) }
item { ColorRow("onErrorContainer", darkColorScheme.onErrorContainer, lightColorScheme.onErrorContainer) }
item { ColorRow("outline", darkColorScheme.outline, lightColorScheme.outline) }
item { ColorRow("outlineVariant", darkColorScheme.outlineVariant, lightColorScheme.outlineVariant) }
item { ColorRow("scrim", darkColorScheme.scrim, lightColorScheme.scrim) }
item { ColorRow("surfaceBright", darkColorScheme.surfaceBright, lightColorScheme.surfaceBright) }
item { ColorRow("surfaceDim", darkColorScheme.surfaceDim, lightColorScheme.surfaceDim) }
item { ColorRow("surfaceContainer", darkColorScheme.surfaceContainer, lightColorScheme.surfaceContainer) }
item { ColorRow("surfaceContainerHigh", darkColorScheme.surfaceContainerHigh, lightColorScheme.surfaceContainerHigh) }
item { ColorRow("surfaceContainerHighest", darkColorScheme.surfaceContainerHighest, lightColorScheme.surfaceContainerHighest) }
item { ColorRow("surfaceContainerLow", darkColorScheme.surfaceContainerLow, lightColorScheme.surfaceContainerLow) }
item { ColorRow("surfaceContainerLowest", darkColorScheme.surfaceContainerLowest, lightColorScheme.surfaceContainerLowest) }
}
}
}
@Composable
private fun ColorRow(name: String, darkColor: Color, lightColor: Color) {
Row(
modifier = Modifier
.fillMaxWidth()
.padding(vertical = 2.dp),
verticalAlignment = Alignment.CenterVertically
) {
Text(
modifier = Modifier.weight(1f),
text = name,
textAlign = TextAlign.End,
fontSize = 13.sp
)
Text(
modifier = Modifier
.padding(horizontal = 8.dp)
.width(56.dp)
.height(32.dp)
.background(darkColor),
text = darkColor.toShortString(),
textAlign = TextAlign.Center,
fontSize = 12.sp,
color = if (darkColor.luminance() > 0.5) Color.Black else Color.White,
)
Text(
modifier = Modifier
.padding(horizontal = 8.dp)
.width(56.dp)
.height(32.dp)
.background(lightColor),
text = lightColor.toShortString(),
textAlign = TextAlign.Center,
fontSize = 12.sp,
color = if (lightColor.luminance() > 0.5) Color.Black else Color.White,
)
}
}
private fun Color.toShortString(): String {
val a = if (alpha != 1f) (alpha * 0xff).toInt().toString(16).padStart(2, '0') else ""
val r = (red * 0xff).toInt().toString(16).padStart(2, '0')
val g = (green * 0xff).toInt().toString(16).padStart(2, '0')
val b = (blue * 0xff).toInt().toString(16).padStart(2, '0')
return "$a$r$g$b"
}
@venator85
Copy link
Author

1
2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment