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 Nick 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.

Now listen up! Back in my day, we didn’t have fancy tanks! We had sticks. Two stick and a rock for the entire platoon! And we had to share the rock! You should consider yourself very lucky marines!

— Sgt. Johnson, Halo 2