ActionScript 3 Greyscale

Just had to desaturate a clip and then return it to normal. Code wise it’s rather simple, the following turns a MovieClip with an instance name of test to grey:

// Make Grey
var r:Number=0.212671;
var g:Number=0.715160;
var b:Number=0.072169;

var matrix:Array = [];
matrix = matrix.concat([r, g, b, 0, 0]);// red
matrix = matrix.concat([r, g, b, 0, 0]);// green
matrix = matrix.concat([r, g, b, 0, 0]);// blue
matrix = matrix.concat([0, 0, 0, 1, 0]);// alpha
test.filters = [new ColorMatrixFilter(matrix)];

To return it to the default state just clear the color matrix:

// Return to normal
test.filters = [new ColorMatrixFilter()];

Comments: 4

Leave a reply

Your email address will not be published. Required fields are marked *

  • Clark Stevenson
    Clark Stevenson

    Thanks alot for this David, I was attempting to convert to a bitmap and apply thesholds. Now theres no need and its so simple.

    Cheers.

    Clark.

  • Nick
    Nick

    Great tip, but shouldn’t the numbers be red= 0.299, green = 0.587, blue = 0.114 (International Telecommunications Union Standard)?

    This blog mentions it: http://jswidget.com/blog/tag/grayscale-image/

  • Anonymous
    Anonymous

    Ah, that does sound good. I’m not sure where I got the original values from. I did a search for them and it seems there’s alot of people doing the same thing. If I use this again then I’ll use the values you indicated.

  • APasserBy
    APasserBy

    The original values used by David are from the BT709 standard published by ITU-R (International Telecommunication Union). The values provided by Nick are from the NTSC and PAL standards.

Two little mice fell in a bucket of cream. The first mouse quickly gave up and drowned. The second mouse, wouldn’t quit. He struggled so hard that eventually he churned that cream into butter and crawled out. Gentlemen, as of this moment, I am that second mouse.

— Frank Abagnale Sr, Catch Me If You Can