AS3:How to change a colored Bitmap's BitmapData to black and white?

Yens picture Yens · Jul 8, 2009 · Viewed 24.6k times · Source

How can I change the bitmapdata of a coloured Bitmap to Black and White in AS3 ? I'm developing a simple image editor tool for a CMS in flash.

People should be able to switch the colour of the uploaded Bitmap to black and white. I want the bitmapdata itself to change So I can write it to a ByteArray with Adobe's JPGEncoder Class afterwards.

Answer

back2dos picture back2dos · Jul 8, 2009

this would be the most elegant solution i presume (with source being you BitmapData):

const rc:Number = 1/3, gc:Number = 1/3, bc:Number = 1/3;
source.applyFilter(source.bitmapData, source.bitmapData.rect, new Point(), new ColorMatrixFilter([rc, gc, bc, 0, 0,rc, gc, bc, 0, 0, rc, gc, bc, 0, 0, 0, 0, 0, 1, 0]));

with flash.geom::Point and flash.filters::ColorMaxtrixFilter ...

ColorMatrixFilter allows many things, such as hue shifts, colorisation, lightening, darkening and desaturation and so on ... otherwise BitmapData::paletteMap and BitmapData::colorTransform are good complements ...

just wanted to note, that using the following

const rc:Number = 1/3, gc:Number = 1/2, bc:Number = 1/6; 

looks a little more natural, since subjectively, #00FF00 is brighter than #FF0000, which in turn is brighter than #0000FF

good luck then ... ;)

greetz

back2dos