ColorMatrixFilter (flash.filters.ColorMatrixFilter)


Object
    |
    +-flash.filters.BitmapFilter
        |
        +-flash.filters.ColorMatrixFilter

public class ColorMatrixFilter
extends BitmapFilter

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.

Availability: ActionScript 1.0; Flash Player 8

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

getPixel (BitmapData.getPixel method), applyFilter (BitmapData.applyFilter method), filters (MovieClip.filters property), cacheAsBitmap (MovieClip.cacheAsBitmap property)

Property summary

Modifiers

Property

Description

 

matrix:Array

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

Properties inherited from class Object

constructor, __proto__, prototype, __resolve


Constructor summary

Signature

Description

ColorMatrixFilter(matrix:Array)

Initializes a new ColorMatrixFilter instance with the specified parameters.

Method summary

Modifiers

Signature

Description

 

clone() : ColorMatrixFilter

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



Version 8