Skip to content

Instantly share code, notes, and snippets.

@NikolaDespotoski
Last active June 28, 2023 21:49
Show Gist options
  • Save NikolaDespotoski/03d3c9853502d07adb3dd287ace14f3f to your computer and use it in GitHub Desktop.
Save NikolaDespotoski/03d3c9853502d07adb3dd287ace14f3f to your computer and use it in GitHub Desktop.
Compose tint modifier that will apply color tint over composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.DrawModifier
import androidx.compose.ui.geometry.Rect
import androidx.compose.ui.graphics.BlendMode
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.graphics.Paint
import androidx.compose.ui.graphics.drawscope.ContentDrawScope
import androidx.compose.ui.graphics.drawscope.drawIntoCanvas
internal class TintModifier(
private val blendMode: BlendMode = BlendMode.Overlay,
private val color: () -> Color
) : DrawModifier {
override fun ContentDrawScope.draw() {
val saturationFilter = ColorFilter.tint(color(), blendMode)
val paint = Paint().apply {
colorFilter = saturationFilter
}
drawIntoCanvas {
it.saveLayer(Rect(0f, 0f, size.width, size.height), paint)
drawContent()
it.restore()
}
}
}
fun Modifier.tint(blendMode: BlendMode = BlendMode.Overlay, color: () -> Color) =
this.then(TintModifier(color = color, blendMode = blendMode))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment