4

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?

skaffman
  • 398,947
  • 96
  • 818
  • 769
mackenir
  • 10,801
  • 16
  • 68
  • 100
  • You don't mean converting to a colour image, but converting to an alpha mask. – ChrisF Jun 08 '10 at 12:28
  • I suppose I would be converting a monochrome [white->black] image to a monochrome [transparent->%COLOR%] image. So another way of looking at it is to see the input image as an alpha mask which gets applied to a solid color. But the output isn't an alpha mask. – mackenir Jun 08 '10 at 12:56

1 Answers1

2

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
  • 134,786
  • 31
  • 255
  • 325
  • Thanks! I'll give that a try. I suppose I should just look at matrix multiplication in general - having forgotten the details :-/. – mackenir Jun 08 '10 at 12:57
  • @mackenir - it's been a while since I did it in anger, so treat what I've posted as a starting point! – ChrisF Jun 08 '10 at 12:59