Sunday, 20 April 2008

Filter effects: Color Matrix

This is part of my filter effects tutorial series.

Color Matrix is an interesting filter, that can mix up the colours in your image in many ways. Blanc and white images, sepia toning, hue rotation and removing one of the basic colours from image are a couple examples, what kind of modifications this filter can do.

The mathematical idea behind Color Matrix is, that it multiplies the colour of each pixel by a 5×5 matrix. SVG won't let you to specify the whole matrix, but only the first four rows. The last row is always "0 0 0 0 1", which means that the imagined fifth colour value will always be one. (red, green, blue and alpha are the four actual colour values)

Nevertheless, you don't need to know much about the mathematics to be able to use this filter. Let's take an example image from Inkscape Filter Effects user interface:
Here I've specified an sepia tone filter. Not too good one, but it'll make for an good example about how these work.

So, what are all these numbers?
The basic concept here, is that each column represents an input colour: red, green, blue, alpha and constant addition. Each row in turn represents an output colour: red, green, blue and alpha. For example, the first row means "Take 0.14 times the red value of a pixel, 0.45 times the green value a pixel, 0.05 times the blue value of a pixel and zero times the alpha value of a pixel, sum these and add zero. The result is the red value of output pixel"This same process is applied to all primary colours of a pixel for every pixel in the filtered area.

The fourth row of this example filter is also noteworthy. Not only it defines the modification applied on the alpha channel, it also is an identity modification. Here on the alpha row, alpha is multiplied by one and all other inputs by zero, thus preserving the original alpha channel. With the same idea, you could for example swap the contents of green and blue channels:

Besides the matrix mode, the Color Matrix filter provides also three convenience modes, so you won't have to create the whole matrix every time:
Saturate: this is a bit funny name, as this Saturate actually desaturates the image. You are provided with a slider allowing you to choose from full scale between full saturation and zero saturation (black and white).
Hue Rotate: this does just what it says, it rotates the hues of your image. If you rotated the colour wheel in Fill and Stroke dialog same amount for every colour in filtered object, the effect would be just about the same.
Luminance to Alpha: this can be handy as part of bigger filter, it turns the luminance of input image as opacity of output image. White areas become totally opaque, black areas totally transparent and everything other becomes something in between.

I've found Color Matrix especially handy for adjusting the output from Turbulence filter, as Color Matrix can change the colour of an image, make areas with low alpha values completely transparent etc. The following examples are from turbulence_effects.svg included in Inkscape distribution and they use both Color Matrix and Turbulence:


Anonymous said...


I urgently need to ask you a question related to this tutorial. It is for a new magazine article on inkscape (I already wrote one, see

Please contact me as soon as possible at marco, at


Marco Fioretti

Anonymous said...

This is the only useful explanation of the SVG color matrix I've found. Thanks for posting it.

Sam said...

I've been searching so clear an explanation for years... Thank you !