-
-
Save KatieBarnett/9a7313e497d3c9d0d9a2cef3c4603fee to your computer and use it in GitHub Desktop.
GreyScaleModifier
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
class GreyScaleModifier : DrawModifier { | |
override fun ContentDrawScope.draw() { | |
val saturationMatrix = ColorMatrix().apply { setToSaturation(0f) } | |
val saturationFilter = ColorFilter.colorMatrix(saturationMatrix) | |
val paint = Paint().apply { | |
colorFilter = saturationFilter | |
} | |
drawIntoCanvas { | |
it.saveLayer(Rect(0f, 0f, size.width, size.height), paint) | |
drawContent() | |
it.restore() | |
} | |
} | |
} | |
fun Modifier.greyScale() = this.then(GreyScaleModifier()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is it not possible to create the color matrix and the paint instance as a field, so that it doesn't get created each time draw is called? 🤔