474 Animation, Filters, and Drawings
Adding color and brightness effects with code
In addition to using ActionScript to set and animate alpha fades (see “Fading objects with
code” on page 472), you can animate various color and brightness effects by using code
instead of using the Filters panel in the Property inspector.
The following procedure loads a JPEG image and applies a color transform filter, which
modifies the red and green channels as the mouse pointer moves along the x-axis and y-axis.
To change an object’s color channels by using ActionScript:
1. Create a new Flash document called colorTrans.fla.
2. Select Frame 1 of the Timeline, and add the following code to the Actions panel:
import flash.geom.Transform;
import flash.geom.ColorTransform;
var imageClip:MovieClip = this.createEmptyMovieClip("imageClip", 1);
var clipLoader:MovieClipLoader = new MovieClipLoader();
clipLoader.loadClip("http://www.helpexamples.com/flash/images/
image1.jpg", imageClip);
var mouseListener:Object = new Object();
mouseListener.onMouseMove = function():Void {
var transformer:Transform = new Transform(imageClip);
var colorTransformer:ColorTransform = transformer.colorTransform;
colorTransformer.redMultiplier = (_xmouse / Stage.width) * 1;
colorTransformer.greenMultiplier = (_ymouse / Stage.height) * 1;
transformer.colorTransform = colorTransformer;
}
Mouse.addListener(mouseListener);
3.
Select Control > Test Movie to test the document, and then move the mouse pointer
around the Stage.
The image file that loads transforms colors as you move the mouse.
You can also use the ColorMatrixFilter class to convert a color image to a black and white
image, as the following procedure shows.