Avg. Rating 4.3

Problem

You want to display a color image as grayscale (black and white) using ActionScript.

Solution

Create a ColorFilterMatrix instance, assign it the matrix values below, and apply it to any DisplayObject. [0.3086, 0.6094, 0.082, 0, 0, 0.3086, 0.6094, 0.082, 0, 0, 0.3086, 0.6094, 0.082, 0, 0, 0, 0, 0, 1, 0]

Detailed explanation

The function below will apply a grayscale filter to any display object.

 

 

private function convertToGrayscale(image:DisplayObject):void

{

var matrix:Array = [0.3, 0.59, 0.11, 0, 0,

           0.3, 0.59, 0.11, 0, 0,

           0.3, 0.59, 0.11, 0, 0,

           0, 0, 0, 1, 0];

           

var grayscaleFilter:ColorMatrixFilter = new ColorMatrixFilter(matrix);

image.filters = [grayscaleFilter];

}

 

Report abuse

Related recipes