Return to Snippet

Revision: 28175
at July 2, 2010 23:02 by IsoJon


Initial Code
// GRAYSCALE AND IMAGE snippet

// "Bit" is the linkage ID of the bitmap in the Library
var colorMC : Sprite = new Bit();

// sendToGray takes 3 parameters width, height, and Sprite
addChild( sendToGray( 306, 176, colorMC ));

function sendToGray( w:int, h:int, drawItem:Sprite ) : Bitmap
{
	var myBitmapData : BitmapData = new BitmapData( w, h );
	myBitmapData.draw( drawItem );
	
	var bmp : Bitmap = new Bitmap( setGrayScale(myBitmapData) );
	
	return bmp;
}

function setGrayScale( obj:BitmapData ) : BitmapData
{
    var rLum : Number = 0.2225;
    var gLum : Number = 0.7169;
    var bLum : Number = 0.0606; 
    
    var matrix:Array = [ rLum, gLum, bLum, 0, 0,
                         rLum, gLum, bLum, 0, 0,
                         rLum, gLum, bLum, 0, 0,
                         0,    0,    0,    1, 0 ];
    
    var filter:ColorMatrixFilter = new ColorMatrixFilter( matrix );
    obj.applyFilter( obj, new Rectangle( 0,0,obj.width,obj.height ), new Point(0,0), filter );
    
    return obj;
}

Initial URL


Initial Description
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)

Initial Title
Change Image from Color to Grayscale

Initial Tags


Initial Language
ActionScript 3