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 to Anonymous or [Cancel 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.

You can follow the action, which gets you good pictures. You can follow your instincts, which’ll probably get you in trouble. Or, you can follow the money, which nine times out of ten will get you closer to the truth.

— Jake Glittes