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
movie clip loader instance with the instance name
is loaded into the
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
original color of the image.
You can also set an image's brightness by using the ActionScript code in the following
procedure.
movie clip on the Stage. When the image is successfully loaded,
img_mc
img_mc
. Finally, the source movie clip
img_mcl
Scripting animation with ActionScript 2.0
, as well as a new
) to see the
475
Need help?
Do you have a question about the FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH and is the answer not in the manual?