Packageflash.filters
Classpublic class ColorMatrixFilter
InheritanceColorMatrixFilter Inheritance BitmapFilter Inheritance Object

Player version: Flash Player 8

The ColorMatrixFilter class lets you apply a 4 x 5 matrix transformation on the RGBA color and alpha values of every pixel on the input image to produce a result with a new set of RGBA color and alpha values. It allows saturation changes, hue rotation, luminance to alpha and various other effects. You can apply this filter to bitmaps and MovieClip instances.

The use of filters depends on the object to which you apply the filter:

You can also apply filter effects to images and video at authoring time. For more information, see your authoring documentation.

If you apply a filter to a movie clip or button, the cacheAsBitmap property of the movie clip or button is set to true. If you clear all filters, the original value of cacheAsBitmap is restored.

The following formulas are used, where a[0] through a[19] correspond to entries 0 through 19 in the 20-element array property matrix:

 
     redResult   = a[0]  * srcR + a[1]  * srcG + a[2]  * srcB + a[3]  * srcA + a[4]
     greenResult = a[5]  * srcR + a[6]  * srcG + a[7]  * srcB + a[8]  * srcA + a[9]
     blueResult  = a[10] * srcR + a[11] * srcG + a[12] * srcB + a[13] * srcA + a[14]
     alphaResult = a[15] * srcR + a[16] * srcG + a[17] * srcB + a[18] * srcA + a[19]
  

This filter separates each source pixel into its red, green, blue, and alpha components as srcR, srcG, srcB, srcA. As a final step, it combines each color component back into a single pixel and writes out the result.

The calculations are performed on unmultiplied color values. If the input graphic consists of premultiplied color values, those values are automatically converted into unmultiplied color values for this operation.

The following two optimized modes are available.

Alpha only. When you pass to the filter a matrix that adjusts only the alpha component, as shown here, the filter optimizes its performance:

      1 0 0 0 0
      0 1 0 0 0 
      0 0 1 0 0 
      0 0 0 N 0  (where N is between 0.0 and 1.0)
  

Faster version. Available only with SSE/Altivec accelerator-enabled processors such as Pentium 3 and later, and Apple G4 and later). The accelerator is used when the multiplier terms are in the range -15.99 to 15.99 and the adder terms a[4], a[9], a[14], and a[19] are in the range -8000 to 8000.

A filter is not applied if the resulting image would exceed 2880 pixels in width or height. For example, if you zoom in on a large movie clip with a filter applied, the filter is turned off if the resulting image reaches the 2880-pixel limit.


Example
The following example uses BitmapFilter to manipulate the color saturation of an image based on the location of the mouse pointer. If you position the pointer in the upper-left corner (0,0), the image should be unmodified. As you move the pointer to the right, the green and blue channels together are removed from the image. As you move the pointer down, the red channel is removed. If the pointer is positioned at the lower right of the Stage, the image should be completely black. This example assumes that you have an image in your library with its Linkage Identifier set to "YourImageLinkage".
import flash.filters.BitmapFilter;
import flash.filters.ColorMatrixFilter;

var image:MovieClip = this.attachMovie("YourImageLinkage", "YourImage", this.getNextHighestDepth());
image.cacheAsBitmap = true;

var listener:Object = new Object();
listener.image = image;
listener.onMouseMove = function() {
    var xPercent:Number = 1 - (_xmouse/Stage.width);
    var yPercent:Number = 1 - (_ymouse/Stage.height);
    var matrix:Array = new Array();
    matrix = matrix.concat([yPercent, 0, 0, 0, 0]); // red
    matrix = matrix.concat([0, xPercent, 0, 0, 0]); // green
    matrix = matrix.concat([0, 0, xPercent, 0, 0]); // blue
    matrix = matrix.concat([0, 0, 0, 1, 0]); // alpha

    var filter:BitmapFilter = new ColorMatrixFilter(matrix);
    image.filters = new Array(filter);
}

Mouse.addListener(listener);
listener.onMouseMove();

See also

flash.display.BitmapData.getPixel()
flash.display.BitmapData.applyFilter()
MovieClip.filters
MovieClip.cacheAsBitmap


Public Properties
 Property
  matrix : Array
An array of 20 elements for 4 x 5 color transform.
 Properties inherited from class Object
 __proto__, __resolve, constructor, prototype
Public Methods
 Method
  
Initializes a new ColorMatrixFilter instance with the specified parameters.
  
Returns a copy of this filter object.
 Methods inherited from class BitmapFilter
 clone
 Methods inherited from class Object
 addProperty, hasOwnProperty, isPropertyEnumerable, isPrototypeOf, registerClass, toString, unwatch, valueOf, watch
Property detail
matrixproperty
public var matrix:Array

Player version: Flash Player 8

An array of 20 elements for 4 x 5 color transform.


Example
The following example creates a new ColorMatrixFilter instance and then changes its matrix property. The matrix property cannot be changed by directly modifying its value (for example, clonedFilter.matrix[2] = 1;). Instead, you must get a reference to the array, make the change to the reference, and reset the value using clonedFilter.matrix = changedMatrix.
import flash.filters.ColorMatrixFilter;

var matrix:Array = new Array();
matrix = matrix.concat([1, 0, 0, 0, 0]); // red
matrix = matrix.concat([0, 1, 0, 0, 0]); // green
matrix = matrix.concat([0, 0, 1, 0, 0]); // blue
matrix = matrix.concat([0, 0, 0, 1, 0]); // alpha

var filter:ColorMatrixFilter = new ColorMatrixFilter(matrix);
trace("filter: " + filter.matrix);
var changedMatrix:Array = filter.matrix;
changedMatrix[2] = 1;
filter.matrix = changedMatrix;
trace("filter: " + filter.matrix);

Constructor detail
ColorMatrixFilter()constructor
public function ColorMatrixFilter(matrix:Array)

Player version: Flash Player 8

Initializes a new ColorMatrixFilter instance with the specified parameters.

Parameters
matrix:Array — An array of 20 elements arranged in a 4 x 5 matrix.
Method detail
clone()method
public function clone():ColorMatrixFilter

Player version: Flash Player 8

Returns a copy of this filter object.

Returns
ColorMatrixFilter — A new ColorMatrixFilter instance with all the same properties as the original one.

Example
The following example creates a new ColorMatrixFilter instance and then clones it using the clone method. The matrix property cannot be changed directly (for example, clonedFilter.matrix[2] = 1;). Instead, you must get a reference to the array, make the change, and reset the value using clonedFilter.matrix = changedMatrix.
import flash.filters.ColorMatrixFilter;

var matrix:Array = new Array();
matrix = matrix.concat([1, 0, 0, 0, 0]); // red
matrix = matrix.concat([0, 1, 0, 0, 0]); // green
matrix = matrix.concat([0, 0, 1, 0, 0]); // blue
matrix = matrix.concat([0, 0, 0, 1, 0]); // alpha

var filter:ColorMatrixFilter = new ColorMatrixFilter(matrix);
trace("filter:       " + filter.matrix);

var clonedFilter:ColorMatrixFilter = filter.clone();
matrix = clonedFilter.matrix;
matrix[2] = 1;
clonedFilter.matrix = matrix;
trace("clonedFilter: " + clonedFilter.matrix);