Search code examples
flashactionscript-3

Inverse black and white / monochrome bitmapdata in AS3


I'm wondering if there are any nice ways of inversing a black and white / monochrome bitmapdata in AS3 which don't involve going through and setting each pixel by pixel?


Solution

  • Look up bitmapData.colorTransform() [docs] and the ColorTransform class [docs]

    You'll probably want to apply something like:

    var bd:BitmapData;
    var invertTransform:ColorTransform = new ColorTransform(-1,-1,-1,1,255,255,255,0)
    db.colorTransform(db.rect, invertTransform)
    

    Which will multiply each pixel by -1 and then add 255. so 255 will become 0 and 0 will become 255.