Search code examples
actionscript-3flashmatrixcolormatrixcolormatrixfilter

AS3 - How to remove all black and grey from bitmap, but retain color?


I'd like to be able to alter an image (specifically a bitmap) in order to replace all dark grey and black pixels with white in ActionScript 3, but maintain all other colors in the image. I am familiar with ColorMatrixFilter and bitmapdata.threshold, but I don't know how to use them to either target the colors I want to remove or check within a specific range of colors. Is there any (efficient) way to go about doing this?

Thanks for any help you can offer.


Solution

  • The AS3 API gives pretty good documentation on how to use the treshhold value. You can find it here. Their example actually checks for a certain range of colors. I've modified their example to work for your problem. I haven't tested it, so it might need some tweaking.

    var bmd2:BitmapData = new BitmapData(200, 200, true, 0xFFCCCCCC);
    var pt:Point = new Point(0, 0);
    var rect:Rectangle = new Rectangle(0, 0, 200, 200);
    var threshold:uint =  0x00A9A9A9; //Dark Grey
    var color:uint = 0x00000000; //Replacement color (white)
    var maskColor:uint = 0xFFFFFFFF; //What channels to affect (this is the default).
    bmd2.threshold(bmd1, rect, pt, ">", threshold, color, maskColor, true);
    

    Another option would be to use a double for loop that iterates over all pixels and takes a certain action based on the value of the pixel.

    for(var y:int = 0; y < height; y++){
      for(var x:int = 0; x < width; x++){
        var currentPixel:uint = image.getPixel( x, y );
        if(currentPixel != color){
          image.setPixel( destPoint.x + j, destPoint.y + i, currentPixel );
        }          
      }
    }