Last active
June 28, 2023 21:49
-
-
Save NikolaDespotoski/03d3c9853502d07adb3dd287ace14f3f to your computer and use it in GitHub Desktop.
Compose tint modifier that will apply color tint over composable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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