AS3: How to change color Bitmap BitmapData to black and white?

How to change bitmaps of a color bitmap to Black and White in AS3 ? I am developing a simple image editing tool for CMS in flash memory.

People should be able to switch the color of the loaded bitmap to black and white. I want to change bitmapdata strong>. Therefore, I can write it to ByteArray with the Adobe JPGEncoder class.

+5
source share
7 answers

this would be the most elegant solution i guess (with sourcebeing 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]));

flash.geom::Point flash.filters::ColorMaxtrixFilter...

ColorMatrixFilter , , , , ..... BitmapData::paletteMap BitmapData::colorTransform ...

,

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

, #00FF00 , #FF0000, , #0000FF

...;)

Greetz

back2dos

+27

, getPixel setPixel ( , - ):

for(int i = 0; i < bitmapData.height; i++)
{
    for(int j = 0; j < bitmapData.width; j++)
    {
        var color:uint = bitmapData.getPixel(i, j);
        var red:uint = color & 0xFF0000 >> 16;
        var green:uint = color & 0x00FF00 >> 8;
        var blue:uint = color & 0x0000FF >> 0;
        var bwColor:uitn = red + green + blue / 3;
        bwColor = bwColor << 16 + bwColor << 8 + bwColor; // puts the average in each channel

        bitmapData.setPixel(i, j, bwColor);
    }
}
+2

back2dos- :

const rc:Number = 1/3, gc:Number = 1/3, bc:Number = 1/3;
mc.filters = [ new ColorMatrixFilter([rc, gc, bc, 0, 0,rc, gc, bc, 0, 0, rc, gc, bc, 0, 0, 0, 0, 0, 1, 0]) ];

mc - MovieClip Sprite, . back2dos:)

+2

Cookie, .

   function clearColor() {
      colorbmd = new BitmapData(source.width, source.height);
      colorbmd = source.clone();
      //apply filter here    
    }
    function restoreColor() {
      source = colorbmd;
    }

, - ( ). , . - CookieOfFortune

+1

:

//Tweens to black and white
TweenLite.to(DisplayObjectYouWantToTween ,1,{colorMatrixFilter:{matrix:[0.3,0.59,0.11,0, 0,0.3,0.59,0.11,0,0,0.3,0.59,0.11,0,0,0,0,0,1,0]}});

//Tween from black and white to Color
TweenLite.to(DisplayObjectYouWantToTween,1,{colorMatrixFilter:{matrix:[1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0]}});
+1

. , ColorMatrixFilter .

? shoud ColorMatrixFilter?

0

Thanks for the approach, I created a small tool that allows you to play with values: http://bjornson.inhb.de/?p=159

Initial values ​​are those offered by macke: 30%, 59% and 11% for rgb.

You can also upload external images and check the best result for your images.

0
source

All Articles