views:

27

answers:

1

I'd like to try using ColorMatrix, but am only able to find examples that convert an image to grayscale. And even then, they tend to be presented as a chunk of 'magic numbers' code with no explanation.

Does anyone know of a 'tutorial' on how to use ColorMatrix? For example I'd be interested in converting a grayscale image to a color image, where white == transparent, and black = a solid color, with gray pixels somewhere in between. Could ColorMatrix do that?

+2  A: 

I don't know about any documentation, but as a ColorMatrix transforms one RGBAW value into another, so you can set the matrix to take the input RGB values and apply them to the output Alpha value. (The W is only there to make the matrix maths work).

OK, I think the following matrix should do what you want:

[1.0  0.0  0.0  0.333  0.0]
[0.0  1.0  0.0  0.333  0.0]
[0.0  0.0  1.0  0.333  0.0]
[0.0  0.0  0.0  0.0    0.0]
[0.0  0.0  0.0  0.0    1.0]

This will leave the current RGB values unchanged and then set the alpha to be R/3 + G/3 + B/3, i.e. the average of the RGB values. Change the 1.0 values along the diagonal to 0.0 to remove the RGB values altogether.

If you know that the image is a greyscale where R == G == B then you could replace any one of the 0.333 (recurring) with 1.0 and set the other two to 0.0

ChrisF
Thanks! I'll give that a try. I suppose I should just look at matrix multiplication in general - having forgotten the details :-/.
mackenir
@mackenir - it's been a while since I did it in anger, so treat what I've posted as a starting point!
ChrisF