使用颜色矩阵滤镜

ColorMatrixFilter 类使您可以将 4 x 5 矩阵转换应用于输入图像上的每个像素的 ARGB 颜色和 Alpha 值,以产生具有一组新的 ARGB 颜色和 Alpha 值的结果。此滤镜允许色相(明显的颜色或阴影)旋转、饱和度(特定色相的浓度)更改、Alpha 的亮度(亮度或颜色浓度),以及其它各种效果。而且,可以为这些滤镜实现动画效果,以便在您的应用程序中形成特定的效果。

注意

可以将颜色矩阵滤镜应用于位图和影片剪辑实例。

有关颜色矩阵滤镜的更多信息,请参见"ActionScript 2.0 语言参考"中的 ColorMatrixFilter (flash.filters.ColorMatrixFilter)。

可以使用颜色矩阵滤镜来修改实例的亮度,如下面的示例所示。

增加影片剪辑的亮度:

  1. 创建一个新的 Flash 文档,并将它保存为 brightness.fla
  2. 将下面的 ActionScript 添加到时间轴中的第 1 帧:
    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 = [1, 0, 0, 0, 100,
                0, 1, 0, 0, 100,
                0, 0, 1, 0, 100,
                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/image2.jpg", img_mc);
    

    此代码通过使用 MovieClipLoader 实例动态加载 JPEG 图像。图像加载完成并放置在舞台上之后,通过使用颜色矩阵滤镜将实例的亮度设置为 100%。

  3. 选择"控制">"测试影片"来测试该文档。

还可以通过结合使用 Tween 类和 ColorMatrixFilter 类来创建动画的亮度效果,如下面的过程所示。

使用 Tween 类为实例的亮度级别添加动画效果:

  1. 创建一个新的 Flash 文档,并将它保存为 brightnesstween.fla
  2. 将下面的 ActionScript 添加到时间轴中的第 1 帧:
    import flash.filters.ColorMatrixFilter;
    import mx.transitions.Tween;
    import mx.transitions.easing.*;
    System.security.allowDomain("http://www.helpexamples.com");
    var mclListener:Object = new Object();
    mclListener.onLoadInit = function(target_mc:MovieClip):Void {
        // 使影片剪辑实例在舞台上居中
        target_mc._x = (Stage.width - target_mc._width) / 2;
        target_mc._y = (Stage.height - target_mc._height) / 2;
        target_mc.watch("brightness", brightnessWatcher, target_mc);
        // 为 target_mc 影片剪辑添加动画效果,使其亮度在 -100 到 +100 之间变化
        var t:Object = new Tween(target_mc, "brightness", Elastic.easeOut, 100, -100, 3, true);
        t.onMotionFinished = function() {
            this.yoyo();
        };
    };
    this.createEmptyMovieClip("img_mc", 10);
    var img_mcl:MovieClipLoader = new MovieClipLoader();
    img_mcl.addListener(mclListener);
    img_mcl.loadClip("http://www.helpexamples.com/flash/images/image1.jpg", img_mc);
    
    function brightnessWatcher(prop:String, oldVal:Number, newVal:Number, target_mc:MovieClip):Number {
        var brightness_array:Array = [1, 0, 0, 0, newVal,
                0, 1, 0, 0, newVal,
                0, 0, 1, 0, newVal,
                0, 0, 0, 1, 0];
        target_mc.filters = [new ColorMatrixFilter(brightness_array)];
        return newVal;
    };
    

    第一部分代码使用 MovieClipLoader 类将 JPEG 图像加载到舞台上。图像加载完成后,将该图像重新定位在舞台的中央。然后使用 Tween 类为图像亮度级别添加动画效果。若要为亮度添加动画效果,可以使用 Object.watch() 方法,该方法可以注册指定的 ActionScript 对象属性更改时将要启动的事件处理函数。无论何时某些 ActionScript 尝试设置 target_mc 实例的自定义亮度属性时,都可以调用 brightnessWatcher 函数。自定义 brightnessWatcher 函数创建一个新数组,该数组使用颜色矩阵滤镜将目标图像的亮度设置为指定值。

  3. 选择"控制">"测试影片"来测试该文档。

    图像加载并放置在舞台上之后,图像的亮度将在 -100 和 100 之间变化。在完成亮度补间后,将使用 Tween.yoyo() 方法使动画反向,从而为补间实现不间断的动画。