breath 效果

要做一个呼吸效果,选中一个显示对象之后,这个显示对象先慢慢变暗,然后到达最低值后再慢慢变亮,然后到达最高值好哦再慢慢变暗。如此循环。

大概知道要通过滤镜来实现这个效果,在网上搜了一下,搜到下面这篇文章:

*************************************************************

  1. import fl.motion.ColorMatrix;  
  2. import flash.filters.ColorMatrixFilter;  
  3. //**调整亮度**//  
  4. var ld_Matrix:ColorMatrix=new ColorMatrix();  
  5. var ld_Filter:ColorMatrixFilter=new ColorMatrixFilter();  
  6. ld_Matrix.SetBrightnessMatrix(100);    
  7. //设置亮度值,值的大小是 -255--255  0为中间值,向右为亮向左为暗。  
  8. ld_Filter.matrix = ld_Matrix.GetFlatArray();  
  9. ld_MC.filters = [ld_Filter];  
  10. //ld_MC.filters = [];//去除滤镜  
  11. //**调整对比度**//  
  12. var db_Matrix:ColorMatrix=new ColorMatrix();  
  13. var db_Filter:ColorMatrixFilter=new ColorMatrixFilter();  
  14. db_Matrix.SetContrastMatrix(255);    
  15. //设置对比度值,值的大小是 -255--255  127.5为中间值,  
  16. //向右对比鲜明向左对比偏暗。  
  17. db_Filter.matrix = db_Matrix.GetFlatArray();  
  18. db_MC.filters = [db_Filter];  
  19. //db_MC.filters = [];//去除滤镜  
  20. //**调整饱和度**//  
  21. var bh_Matrix:ColorMatrix=new ColorMatrix();  
  22. var bh_Filter:ColorMatrixFilter=new ColorMatrixFilter();  
  23. bh_Matrix.SetSaturationMatrix(0);    
  24. //设置饱和度值,值的大小是 -255--255  
  25. //1为中间值,0为灰度值(即黑白相片)。  
  26. bh_Filter.matrix = bh_Matrix.GetFlatArray();  
  27. bh_MC.filters = [bh_Filter];  
  28. //bh_MC.filters = [];//去除滤镜  
  29. //**调整色相**//  
  30. var sx_Matrix:ColorMatrix=new ColorMatrix();  
  31. var sx_Filter:ColorMatrixFilter=new ColorMatrixFilter();  
  32. sx_Matrix.SetHueMatrix(1);    
  33. //设置色相值,值的大小是 -255--255    
  34. //0为中间值,向右向左一试便知。  
  35. sx_Filter.matrix = sx_Matrix.GetFlatArray();  
  36. sx_MC.filters = [sx_Filter];  
  37. //sx_MC.filters = [];//去除滤镜

*************************************************************

是通过fl类包中的ColorMatrix来实现的。由于项目中没有fl包,后来搜到一个开源类。

附:纯a3项目如何引用fl包

  1. C:\Program Files\Adobe\Adobe Flash CS5.5\Common\Configuration\ActionScript 3.0\libs\flash.swc 

/**
* ColorMatrix by Grant Skinner. August 8, 2005
* Updated to AS3 November 19, 2007
* Visit www.gskinner.com/blog for documentation, updates and more free code.
*
* You may distribute this class freely, provided it is not modified in any way (including
* removing this header or changing the package path).
*
* Please contact info@gskinner.com prior to distributing modified versions of this class.
*/

package com.gskinner.geom {

    dynamic public class ColorMatrix extends Array {
   
    // constant for contrast calculations:
        private static const DELTA_INDEX:Array = [
            0,    0.01, 0.02, 0.04, 0.05, 0.06, 0.07, 0.08, 0.1,  0.11,
            0.12, 0.14, 0.15, 0.16, 0.17, 0.18, 0.20, 0.21, 0.22, 0.24,
            0.25, 0.27, 0.28, 0.30, 0.32, 0.34, 0.36, 0.38, 0.40, 0.42,
            0.44, 0.46, 0.48, 0.5,  0.53, 0.56, 0.59, 0.62, 0.65, 0.68,
            0.71, 0.74, 0.77, 0.80, 0.83, 0.86, 0.89, 0.92, 0.95, 0.98,
            1.0,  1.06, 1.12, 1.18, 1.24, 1.30, 1.36, 1.42, 1.48, 1.54,
            1.60, 1.66, 1.72, 1.78, 1.84, 1.90, 1.96, 2.0,  2.12, 2.25,
            2.37, 2.50, 2.62, 2.75, 2.87, 3.0,  3.2,  3.4,  3.6,  3.8,
            4.0,  4.3,  4.7,  4.9,  5.0,  5.5,  6.0,  6.5,  6.8,  7.0,
            7.3,  7.5,  7.8,  8.0,  8.4,  8.7,  9.0,  9.4,  9.6,  9.8,
            10.0
        ];
   
        // identity matrix constant:
        private static const IDENTITY_MATRIX:Array = [
            1,0,0,0,0,
            0,1,0,0,0,
            0,0,1,0,0,
            0,0,0,1,0,
            0,0,0,0,1
        ];
        private static const LENGTH:Number = IDENTITY_MATRIX.length;
   
   
    // initialization:
        public function ColorMatrix(p_matrix:Array=null) {
            p_matrix = fixMatrix(p_matrix);
            copyMatrix(((p_matrix.length == LENGTH) ? p_matrix : IDENTITY_MATRIX));
        }
       
       
    // public methods:
        public function reset():void {
            for (var i:uint=0; i<LENGTH; i++) {
                this[i] = IDENTITY_MATRIX[i];
            }
        }
   
        public function adjustColor(p_brightness:Number,p_contrast:Number,p_saturation:Number,p_hue:Number):void {
            adjustHue(p_hue);
            adjustContrast(p_contrast);
            adjustBrightness(p_brightness);
            adjustSaturation(p_saturation);
        }

        public function adjustBrightness(p_val:Number):void {
            p_val = cleanValue(p_val,100);
            if (p_val == 0 || isNaN(p_val)) { return; }
            multiplyMatrix([
                1,0,0,0,p_val,
                0,1,0,0,p_val,
                0,0,1,0,p_val,
                0,0,0,1,0,
                0,0,0,0,1
            ]);
        }
   
        public function adjustContrast(p_val:Number):void {
            p_val = cleanValue(p_val,100);
            if (p_val == 0 || isNaN(p_val)) { return; }
            var x:Number;
            if (p_val<0) {
                x = 127+p_val/100*127
            } else {
                x = p_val%1;
                if (x == 0) {
                    x = DELTA_INDEX[p_val];
                } else {
                    //x = DELTA_INDEX[(p_val<<0)]; // this is how the IDE does it.
                    x = DELTA_INDEX[(p_val<<0)]*(1-x)+DELTA_INDEX[(p_val<<0)+1]*x; // use linear interpolation for more granularity.
                }
                x = x*127+127;
            }
            multiplyMatrix([
                x/127,0,0,0,0.5*(127-x),
                0,x/127,0,0,0.5*(127-x),
                0,0,x/127,0,0.5*(127-x),
                0,0,0,1,0,
                0,0,0,0,1
            ]);
        }
   
        public function adjustSaturation(p_val:Number):void {
            p_val = cleanValue(p_val,100);
            if (p_val == 0 || isNaN(p_val)) { return; }
            var x:Number = 1+((p_val > 0) ? 3*p_val/100 : p_val/100);
            var lumR:Number = 0.3086;
            var lumG:Number = 0.6094;
            var lumB:Number = 0.0820;
            multiplyMatrix([
                lumR*(1-x)+x,lumG*(1-x),lumB*(1-x),0,0,
                lumR*(1-x),lumG*(1-x)+x,lumB*(1-x),0,0,
                lumR*(1-x),lumG*(1-x),lumB*(1-x)+x,0,0,
                0,0,0,1,0,
                0,0,0,0,1
            ]);
        }
   
        public function adjustHue(p_val:Number):void {
            p_val = cleanValue(p_val,180)/180*Math.PI;
            if (p_val == 0 || isNaN(p_val)) { return; }
            var cosVal:Number = Math.cos(p_val);
            var sinVal:Number = Math.sin(p_val);
            var lumR:Number = 0.213;
            var lumG:Number = 0.715;
            var lumB:Number = 0.072;
            multiplyMatrix([
                lumR+cosVal*(1-lumR)+sinVal*(-lumR),lumG+cosVal*(-lumG)+sinVal*(-lumG),lumB+cosVal*(-lumB)+sinVal*(1-lumB),0,0,
                lumR+cosVal*(-lumR)+sinVal*(0.143),lumG+cosVal*(1-lumG)+sinVal*(0.140),lumB+cosVal*(-lumB)+sinVal*(-0.283),0,0,
                lumR+cosVal*(-lumR)+sinVal*(-(1-lumR)),lumG+cosVal*(-lumG)+sinVal*(lumG),lumB+cosVal*(1-lumB)+sinVal*(lumB),0,0,
                0,0,0,1,0,
                0,0,0,0,1
            ]);
        }
   
        public function concat(p_matrix:Array):void {
            p_matrix = fixMatrix(p_matrix);
            if (p_matrix.length != LENGTH) { return; }
            multiplyMatrix(p_matrix);
        }
       
        public function clone():ColorMatrix {
            return new ColorMatrix(this);
        }
   
        public function toString():String {
            return "ColorMatrix [ "+this.join(" , ")+" ]";
        }
       
        // return a length 20 array (5x4):
        public function toArray():Array {
            return slice(0,20);
        }
   
    // private methods:
        // copy the specified matrix's values to this matrix:
        protected function copyMatrix(p_matrix:Array):void {
            var l:Number = LENGTH;
            for (var i:uint=0;i<l;i++) {
                this[i] = p_matrix[i];
            }
        }
   
        // multiplies one matrix against another:
        protected function multiplyMatrix(p_matrix:Array):void {
            var col:Array = [];
           
            for (var i:uint=0;i<5;i++) {
                for (var j:uint=0;j<5;j++) {
                    col[j] = this[j+i*5];
                }
                for (j=0;j<5;j++) {
                    var val:Number=0;
                    for (var k:Number=0;k<5;k++) {
                        val += p_matrix[j+k*5]*col[k];
                    }
                    this[j+i*5] = val;
                }
            }
        }
       
        // make sure values are within the specified range, hue has a limit of 180, others are 100:
        protected function cleanValue(p_val:Number,p_limit:Number):Number {
            return Math.min(p_limit,Math.max(-p_limit,p_val));
        }
   
        // makes sure matrixes are 5x5 (25 long):
        protected function fixMatrix(p_matrix:Array=null):Array {
            if (p_matrix == null) { return IDENTITY_MATRIX; }
            if (p_matrix is ColorMatrix) { p_matrix = p_matrix.slice(0); }
            if (p_matrix.length < LENGTH) {
                p_matrix = p_matrix.slice(0,p_matrix.length).concat(IDENTITY_MATRIX.slice(p_matrix.length,LENGTH));
            } else if (p_matrix.length > LENGTH) {
                p_matrix = p_matrix.slice(0,LENGTH);
            }
            return p_matrix;
        }
    }
}

 

具体实现:在enterframe中循环变化明暗度。

posted @ 2013-10-11 20:28  Do.else  阅读(302)  评论(0编辑  收藏  举报