Change Image from Color to Grayscale


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

With this snippet you can change your color image from the library to grayscale (or it can be augmented to use any other color transform)


Copy this code and paste it in your HTML
  1. // GRAYSCALE AND IMAGE snippet
  2.  
  3. // "Bit" is the linkage ID of the bitmap in the Library
  4. var colorMC : Sprite = new Bit();
  5.  
  6. // sendToGray takes 3 parameters width, height, and Sprite
  7. addChild( sendToGray( 306, 176, colorMC ));
  8.  
  9. function sendToGray( w:int, h:int, drawItem:Sprite ) : Bitmap
  10. {
  11. var myBitmapData : BitmapData = new BitmapData( w, h );
  12. myBitmapData.draw( drawItem );
  13.  
  14. var bmp : Bitmap = new Bitmap( setGrayScale(myBitmapData) );
  15.  
  16. return bmp;
  17. }
  18.  
  19. function setGrayScale( obj:BitmapData ) : BitmapData
  20. {
  21. var rLum : Number = 0.2225;
  22. var gLum : Number = 0.7169;
  23. var bLum : Number = 0.0606;
  24.  
  25. var matrix:Array = [ rLum, gLum, bLum, 0, 0,
  26. rLum, gLum, bLum, 0, 0,
  27. rLum, gLum, bLum, 0, 0,
  28. 0, 0, 0, 1, 0 ];
  29.  
  30. var filter:ColorMatrixFilter = new ColorMatrixFilter( matrix );
  31. obj.applyFilter( obj, new Rectangle( 0,0,obj.width,obj.height ), new Point(0,0), filter );
  32.  
  33. return obj;
  34. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.