Converting an image to black & white using AS3

Last week I found myself needing to convert a colour image to black and white and here’s how I did it!

import flash.filters.ColorMatrixFilter;
import fl.motion.AdjustColor;
//==========================
var color : AdjustColor;
var colorMatrix : ColorMatrixFilter;
var matrix : Array;
var filterBW : Array;
color = new AdjustColor();
color.brightness = 20;
color.contrast = 20;
color.hue = 0;
color.saturation = -100;
matrix = color.CalculateFinalFlatArray();
colorMatrix = new ColorMatrixFilter(matrix);
filterBW = [colorMatrix];

Now we can set the .filters property of our image to be filterBW, and shazam you have a black and white image.

Tags: , , , ,

Leave a Reply