Expand description

Perform a color matrix operation

A color matrix is a type of operation where the colors of an RGBA image are multiplied by an arbitrary 4*5 matrix.

The matrix is equivalent to the operation

red   = m[0][0]*r + m[0][1]*g + m[0][2]*b + m[0][3]*a + m[0][4]
green = m[1][0]*r + m[1][1]*g + m[1][2]*b + m[1][3]*a + m[1][4]
blue  = m[2][0]*r + m[2][1]*g + m[2][2]*b + m[2][3]*a + m[2][4]
alpha = m[3][0]*r + m[3][1]*g + m[3][2]*b + m[3][3]*a + m[3][4]

This is most similar to Android’s ColorMatrix operation with the difference being that matrix values are always between 0 and 1 and the library will do appropriate scaling

This is similar to imagemagick’s color-matrix operator with some examples provided in the website at Color matrix operator

A playground to build color matrices can be found here (external link, not affiliated)

§Examples of color matrix

  • An identity color matrix that does nothing
[[1.0,0.0,0.0,0.0,0.0],
 [0.0,1.0,0.0,0.0,0.0]
 [0.0,0.0,1.0,0.0,0.0]
 [0.0,0.0,0.0,1.0,0.0]]
  • A matrix that converts an RGB image to grayscale in the ratio .2,.5,.3
[[0.2, 0.5, 0.3, 0.0, 0.0],
[0.2, 0.5, 0.3, 0.0, 0.0],
[0.2, 0.5, 0.3, 0.0, 0.0],
[0.0, 0.0, 0.0, 1.0, 0.0]]
  • A Matrix that inverts it’s color
[[-1.0, 0.0, 0.0, 0.0, 1.0],

Structs§