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()];
Share Article

Comments: 4

Leave a reply

Please fill in the form fields and improve Skynet by completing the captcha!

Thank you! All comments are moderated so yours will appear shortly.
  • 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.

Jack, having you as a friend is a little bit like owning a pet disease!

— Macgyver about Jack Dalton