Skip to content

Instantly share code, notes, and snippets.

@victory316
Created May 7, 2024 00:32
Show Gist options
  • Save victory316/cb4b03a724022622036e54eb0028f2f1 to your computer and use it in GitHub Desktop.
Save victory316/cb4b03a724022622036e54eb0028f2f1 to your computer and use it in GitHub Desktop.
Jetpack compose colorFilter sample to make image dimmed in condition.
/**
* Reference: https://stackoverflow.com/a/78230014
*/
@Composable
fun ColorFilterSample(isActive: Boolean, iconResId: Int) {
val brightness = if (isActive) 0f else 0f
val contrast = if (isActive) 1f else 0.5f
val saturation = if (isActive) 1f else 0.7f
val filter = ColorFilter.colorMatrix(
ColorMatrix(
floatArrayOf(
contrast, 0f, 0f, 0f, brightness,
0f, contrast, 0f, 0f, brightness,
0f, 0f, contrast, 0f, brightness,
0f, 0f, 0f, 1f, 0f
)
).apply {
val saturationMatrix = ColorMatrix()
saturationMatrix.setToSaturation(saturation)
timesAssign(saturationMatrix)
}
)
Image(
painter = painterResource(iconResId),
colorFilter = filter,
contentDescription = null
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment