AS3 invert image transparency


/ Published in: ActionScript 3
Save to your folder(s)

This inverts the transparency on a Bitmap (probably supplied a PNG source) that specifically has two colors in it; "#656565" and "transparent" - you can tweak the threshold lines specifically if you'd like to modify that to use a range, or whatever.

Adapted from http://stackoverflow.com/questions/608087/flex-actionscript-white-to-transparent and http://www.sephiroth.it/tutorials/flashPHP/thresold/


Copy this code and paste it in your HTML
  1. private function invert(oldBmp:Bitmap):Bitmap {
  2. var source:BitmapData = oldBmp.bitmapData;
  3. var destin:BitmapData = source;
  4. var rectan:Rectangle = new Rectangle(0, 0, source.width, source.height);
  5.  
  6. // Replace all transparent pixels with a solid color
  7. destin.threshold(source, rectan, new Point(), "==", 0x00000000,0xFFFF0000);
  8. // Replace all the pixels greater than 0xf1f1f1 by transparent pixels
  9. destin.threshold(source, rectan, new Point(), "==", 0xff656565,0x0000FF00);
  10.  
  11. return new Bitmap(destin);
  12. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.