Humaira Naeem Pasha
03/09/2023, 12:27 PMSurface(
modifier = modifier.then(Modifier.withGridWidth(gridWidth)).drawWithCache {
onDrawWithContent {
drawContent()
drawRect(backgroundColor, blendMode = BlendMode.Saturation)
}
}.graphicsLayer { alpha = 0.8f }
This does the job but the BlendMode.Saturation is available only API 29 and onwards. Is there any workaround to this please?
(Attaching the screenshot for the component)efemoney
03/10/2023, 7:51 AMChris Sinco [G]
03/10/2023, 7:55 AMromainguy
03/10/2023, 4:36 PMColorMatrixColorFilter
on the layer’s paint to set saturation to 0. Compose does have a ColorFilter
(https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ColorFilter) but unfortunately it seems we don’t let graphicsLayer
hold a color filter. We should fix this. @Nader Jawad Any thoughts?Nader Jawad
03/10/2023, 4:52 PMromainguy
03/10/2023, 5:16 PMNader Jawad
03/10/2023, 5:23 PMromainguy
03/10/2023, 5:37 PMNader Jawad
03/10/2023, 6:23 PMHumaira Naeem Pasha
03/10/2023, 7:14 PMModifier.drawWithCache {
onDrawWithContent {
val saturationMatrix = ColorMatrix(
floatArrayOf(
0.18f, 0.18f, 0.18f, 0f, 118f,
0.18f, 0.18f, 0.18f, 0f, 118f,
0.18f, 0.18f, 0.18f, 0f, 118f,
0f, 0f, 0f, 0.8f, 0f
)
)
val saturationFilter = ColorFilter.colorMatrix(saturationMatrix)
val paint = Paint().apply {
colorFilter = saturationFilter
}
drawIntoCanvas { canvas ->
canvas.saveLayer(Rect(0f, 0f, size.width, size.height), paint)
drawContent()
canvas.restore()
}
}
}
I'm not completely sure about this tho - thoughts on this approach please?romainguy
03/10/2023, 8:02 PMsetSaturation
uses the proper luminance weightsfloatArrayOf(0.213f, 0.715f, 0.072f, 0f, 0f, 0.213f, 0.715f, 0.072f, 0f, 0f, 0.213f, 0.715f, 0.072f, 0f, 0f, 0f, 0f, 0f, 1f, 0f)
(hopefully I typed it correctly 🙂Humaira Naeem Pasha
03/11/2023, 6:41 PMromainguy
03/11/2023, 6:43 PMHumaira Naeem Pasha
03/11/2023, 6:56 PM