Skip to content

Instantly share code, notes, and snippets.

@KatieBarnett
Created August 27, 2023 10:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KatieBarnett/9a7313e497d3c9d0d9a2cef3c4603fee to your computer and use it in GitHub Desktop.
Save KatieBarnett/9a7313e497d3c9d0d9a2cef3c4603fee to your computer and use it in GitHub Desktop.
GreyScaleModifier
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())
@Zhuinden
Copy link

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? 🤔

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