Scripting animation with ActionScript 2.0 475
To use the ColorMatrixFilter class to change an image to a grayscale image:
1. Create a new Flash document called grayscale.fla.
2. Select Frame 1 of the Timeline, and add the following code to the Actions panel:
import flash.filters.ColorMatrixFilter;
System.security.allowDomain("http://www.helpexamples.com");
var mcl_obj:Object = new Object();
mcl_obj.onLoadInit = function(target_mc:MovieClip):Void {
var myElements_array: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 myColorMatrix_filter:ColorMatrixFilter = new
ColorMatrixFilter(myElements_array);
target_mc.filters = [myColorMatrix_filter];
}
this.createEmptyMovieClip("img_mc", this.getNextHighestDepth());
var img_mcl:MovieClipLoader = new MovieClipLoader();
img_mcl.addListener(mcl_obj);
img_mcl.loadClip("http://www.helpexamples.com/flash/images/image1.jpg",
img_mc);
The preceding code begins by importing the ColorMatrixFilter class, and creates a listener
object that will be used with a new MovieClipLoader instance created in some later code.
Next, a new movie clip instance is created with the instance name
img_mc, as well as a new
movie clip loader instance with the instance name
img_mcl. Finally, the source movie clip
is loaded into the
img_mc movie clip on the Stage. When the image is successfully loaded,
the onLoadInit event handler is called and attaches a ColorMatrixFilter to the loaded
image.
3. Select Control > Test Movie to test the document.
The image that you load onto the Stage changes to a grayscale image. View the image
online (
http://www.helpexamples.com/flash/images/image1.jpg) to see the
original color of the image.
You can also set an image’s brightness by using the ActionScript code in the following
procedure.