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:

  • To apply filters to movie clips at runtime, use the filters property. Setting the filters property of an object does not modify the object and can be undone by clearing the filters property.

  • To apply filters to BitmapData instances, use the BitmapData.applyFilter() method. Calling applyFilter() on a BitmapData object takes the source BitmapData object and the filter object and generates a filtered image as a result.

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 (Object.constructor property), __proto__ (Object.__proto__ property), prototype (Object.prototype property), __resolve (Object.__resolve property)


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 (BitmapFilter.clone method)


Methods inherited from class Object

addProperty (Object.addProperty method), hasOwnProperty (Object.hasOwnProperty method), isPropertyEnumerable (Object.isPropertyEnumerable method), isPrototypeOf (Object.isPrototypeOf method), registerClass (Object.registerClass method), toString (Object.toString method), unwatch (Object.unwatch method), valueOf (Object.valueOf method), watch (Object.watch method)


clone (ColorMatrixFilter.clone method)

public clone() : ColorMatrixFilter

Returns a copy of this filter object.

Availability: ActionScript 1.0; Flash Player 8

Returns

flash.filters.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);

ColorMatrixFilter constructor

public ColorMatrixFilter(matrix:Array)

Initializes a new ColorMatrixFilter instance with the specified parameters.

Availability: ActionScript 1.0; Flash Player 8

Parameters

matrix:Array - An array of 20 elements arranged in a 4 x 5 matrix.

matrix (ColorMatrixFilter.matrix property)

public matrix : Array

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

Availability: ActionScript 1.0; Flash Player 8

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);



ActionScript 2.0 Language Reference for Macromedia Flash 8
ActionScript 2.0 Language Reference for Macromedia Flash 8
ISBN: 0321384040
EAN: 2147483647
Year: 2004
Pages: 113

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net