Friday, January 31, 2014

How to convert color (uint) to ColorMatrixFilter in Actionscript 3.0

Lets say the color we want to use is 0x0056aa and apply  ColorMatrixFilter with this color it to an object.

var ct:ColorTransform = new ColorTransform;
ct.color = 0x0056aa;

 var matrix : Array = new Array();
 matrix = matrix.concat([0,0,0,0,ct.redOffset]);// red
 matrix = matrix.concat([0,0,0,0,  ct.greenOffset]);// green
 matrix = matrix.concat([0,0,0,0,  ct.blueOffset]);// blue
 matrix = matrix.concat([0,0,0,1,   0]);// alpha

 object.filters = object.filters.concat(new ColorMatrixFilter(matrix));

You can read  colormatrixfilter API here & there's an example here. Even though all my images are only one color, colortransform wasn't work for me since I needed a glow filter in a specific colour to identify warnings.
ColorTransform changes the color of glow as well, which was not acceptable in my case. If you want to do complex pixel/color manipulation you can look at pixelbender toolkit here and another example relevant to color transforms here