I am trying to write some code that modifies the alpha value in every pixel in a bitmap such that if the pixels alpha is < threshold the alpha will be set to 0f. I am starting by creating a bitmap which consists of 2 circles which are drawn with a radial gradient shader, going from fully opaque to transparent. This means the edges of the 2 circles are fuzzy and transparent. I then want to have a threshold that produces an image where the fuzzy edges are “cleaned up” (i.e. there is a definitive edge to them where when the alpha goes below a threshold there is just transparency). After that, I want to finally make all the pixels that remain within this newly defined area be fully opaque (some remove the transparency from the original rendered gradient circles).
I am struggling however to achieve the first part of this (the thresholding) with a ColorMatrix.
I am setting up the identity matrix which should effectively do no transforms to the bitmap:
val colorMatrix = ColorMatrix(
floatArrayOf(
1f, 0f, 0f, 0f, 0f, // Red
0f, 1f, 0f, 0f, 0f, // Green
0f, 0f, 1f, 0f, 0f, // Blue
0f, 0f, 0f, 1f, 0f // Alpha channel
)
)
As I understand it, each row is used to calculate one of the output channels (i.e. 4 rows for RGBA) and each column relates to the RGBA value of each pixel in the painted image when the matrix operation is carried out (provided to the Paint via colorFilter property). There is a fifth column for a shift value as well.
So I don’t want to affect the RGB channels at all, therefore I will just keep the first 3 rows as they are and I need to look only at the 4th row. In addition, when calculating the alpha I don’t need to factor in the RGB channels so I can also ignore the first 3 columns when figuring out what values need to go in the 4th row to achieve my aim. Therefore I only have 2 numbers to play with, those being the last 2 values in the matrix. However, I have tried plugging in lots of different numbers to achieve what I want and it’s not working.
Could anyone provide an explanation of how to achieve what I want using a ColorMatrix?
Thomas Cook is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.