flash代码改变元件亮度、色调、高级、透明度的方法

通过Color类来改变影片颜色
亮度:(设置影片明暗效果)
import com.common.motion.Color;//继承ColorTransform
var color:Color = new Color();
color.brightness = [-1,1]        //-1~1是取值范围
mc.transform.colorTransform = color;        //mc是影片剪辑的名字

色调:(改变影片颜色)
有四个值:色调百分比,red、green、blue值
import com.common.motion.Color;
var color:Color = new Color();
color.setTint(0x808080,1);     //第一个参数是tintColor  RGB值,第二个参数是tintMultiplier 着色百分比,取值范围0到1。
mc.transform.colorTransform = color;

注:十六进制转十进制 trace(0x808080);   也可以用0x80 高位的8乘以16 再加上低位的值
      十进制转十六进制 trace((128).toString(16));   也可以用128除以16取得高位值,余数为低位值。  
      //128只是个参考,R、G、B各分量的十进制值分别是0~255中的任意数。

高级:(改变影片颜色的另一种方式)
import com.common.motion.Color;
var color:Color = new Color(redMultiplier:Number = 1.0, greenMultiplier:Number = 1.0, blueMultiplier:Number = 1.0, alphaMultiplier:Number = 1.0, redOffset:Number = 0, greenOffset:Number = 0, blueOffset:Number = 0, alphaOffset:Number = 0);
有八个参数,分别是ARGB各分量的填充百分比,取值0~1.,后四个是ARGB各分量的偏移量,取值-255~255。
mc.transform.colorTransform = color;

Alpha值:(改变影片透明度)
mc.alpha = 1;   //取值0~1;



通过ColorTransform类来改变影片颜色值。
import flash.geom.ColorTransform;
var colorInfo:ColorTransform = new ColorTransform();
colorInfo.color = 0x808080;
mc.transform.colorTransform = colorInfo;

当 ColorTransform 对象应用于显示对象时,将按如下方法为每个颜色通道计算新值:

新红色值 = (旧红色值 * redMultiplier) + redOffset
新绿色值 = (旧绿色值 * greenMultiplier) + greenOffset
新蓝色值 = (旧蓝色值 * blueMultiplier) + blueOffset
新 Alpha 值 = (旧 Alpha 值 * alphaMultiplier) + alphaOffset
如果计算后任何一个颜色通道值大于 255,则该值将被设置为 255。 如果该值小于 0,它将被设置为 0。

可以通过下列方式使用 ColorTransform 对象:

在 BitmapData 类的 colorTransform() 方法的 colorTransform 参数中
作为 Transform 对象(此对象可以用作显示对象的 transform 属性)的 colorTransform 属性

注:必须使用 new ColorTransform() 构造函数创建 ColorTransform 对象后,才能调用 ColorTransform 对象的方法。

颜色转换不会应用于影片剪辑(如加载的 SWF 对象)的背景色, 它们仅应用于附加到影片剪辑的图形和元件。


///********************************  Color.as *****************************************/

package com.common.motion
{
    import flash.display.*;
    import flash.geom.ColorTransform;
    
    
    /**
     * The Color class extends the Flash Player ColorTransform class,
     * adding the ability to control brightness and tint.
     * It also contains static methods for interpolating between two ColorTransform objects
     * or between two color numbers.      */
    public class Color extends ColorTransform
    {
        
        /**
         * Constructor for Color instances.
         *
         * @param redMultiplier The percentage to apply the color, as a decimal value between 0 and 1.
         * @param greenMultiplier The percentage to apply the color, as a decimal value between 0 and 1.
         * @param blueMultiplier The percentage to apply the color, as a decimal value between 0 and 1.
         * @param alphaMultiplier A decimal value that is multiplied with the alpha transparency channel value, as a decimal value between 0 and 1.
         * @param redOffset A number from -255 to 255 that is added to the red channel value after it has been multiplied by the <code>redMultiplier</code> value.
         * @param greenOffset A number from -255 to 255 that is added to the green channel value after it has been multiplied by the <code>greenMultiplier</code> value.
         * @param blueOffset A number from -255 to 255 that is added to the blue channel value after it has been multiplied by the <code>blueMultiplier</code> value.
         * @param alphaOffset A number from -255 to 255 that is added to the alpha channel value after it has been multiplied by the <code>alphaMultiplier</code> value. 
         */
        function Color (redMultiplier:Number=1.0, 
                        greenMultiplier:Number=1.0, 
                        blueMultiplier:Number=1.0, 
                        alphaMultiplier:Number=1.0, 
                        redOffset:Number=0, 
                        greenOffset:Number=0, 
                        blueOffset:Number=0, 
                        alphaOffset:Number=0)
        {
            super(redMultiplier, greenMultiplier, blueMultiplier, alphaMultiplier, redOffset, greenOffset, blueOffset, alphaOffset);
        }
        
        
        
        /**
         * The percentage of brightness, as a decimal between <code>-1</code> and <code>1</code>.
         * Positive values lighten the object, and a value of <code>1</code> turns the object completely white.
         * Negative values darken the object, and a value of <code>-1</code> turns the object completely black.
         * 
         * @default 0   
         */
        public function get brightness():Number
        {
            return this.redOffset ? (1-this.redMultiplier) : (this.redMultiplier-1);
        }
        
        /**
         * @private (setter)
         */
        public function set brightness(value:Number):void
        {
            if (value > 1) value = 1;
            else if (value < -1) value = -1;
            var percent:Number = 1 - Math.abs(value);
            var offset:Number = 0;
            if (value > 0) offset = value * 255;
            this.redMultiplier = this.greenMultiplier = this.blueMultiplier = percent;
            this.redOffset     = this.greenOffset     = this.blueOffset     = offset;
        }
        
        
        
        /**
         * Sets the tint color and amount at the same time.
         *
         * @param tintColor The tinting color value in the 0xRRGGBB format.
         *
         * @param tintMultiplier The percentage to apply the tint color, as a decimal value between <code>0</code> and <code>1</code>.
         * When <code>tintMultiplier = 0</code>, the target object is its original color and no tint color is visible.
         * When <code>tintMultiplier = 1</code>, the target object is completely tinted and none of its original color is visible.         */
        public function setTint(tintColor:uint, tintMultiplier:Number):void
        {
            this._tintColor = tintColor;
            this._tintMultiplier = tintMultiplier;
            this.redMultiplier = this.greenMultiplier = this.blueMultiplier = 1 - tintMultiplier;
            var r:uint = (tintColor >> 16) & 0xFF;
            var g:uint = (tintColor >>  8) & 0xFF;
            var b:uint =  tintColor        & 0xFF;
            this.redOffset   = Math.round(r * tintMultiplier);
            this.greenOffset = Math.round(g * tintMultiplier);
            this.blueOffset  = Math.round(b * tintMultiplier);
        }    
        
        /**
         * @private
         */
        private var _tintColor:Number = 0x000000;
        
        
        /**
         * The tinting color value in the 0xRRGGBB format.
         * 
         * 
         * @default 0x000000 (black) 
         */
        public function get tintColor():uint
        {
            return this._tintColor;
        }
        
        /**
         * @private (setter)
         */
        public function set tintColor(value:uint):void
        {
            this.setTint(value, this.tintMultiplier);
        }
        
        
        // Capable of deriving a tint color from the color offsets,
        // but the accuracy decreases as the tint multiplier decreases (rounding issues).
        /**
         * @private 
         */
        private function deriveTintColor():uint
        {
            var ratio:Number = 1 / (this.tintMultiplier); 
            var r:uint = Math.round(this.redOffset * ratio);
            var g:uint = Math.round(this.greenOffset * ratio);
            var b:uint = Math.round(this.blueOffset * ratio);
            var colorNum:uint = r<<16 | g<<8 | b;
            return colorNum; 
        }
        
        
        /**
         * @private
         */
        private var _tintMultiplier:Number = 0;
        
        
        /**
         * The percentage to apply the tint color, as a decimal value between <code>0</code> and <code>1</code>.
         * When <code>tintMultiplier = 0</code>, the target object is its original color and no tint color is visible.
         * When <code>tintMultiplier = 1</code>, the target object is completely tinted and none of its original color is visible.
         * @default 0
         */
        public function get tintMultiplier():Number
        {
            return this._tintMultiplier;
        }
        
        /**
         * @private (setter)
         */
        public function set tintMultiplier(value:Number):void
        {
            this.setTint(this.tintColor, value);
        }
        
        
        /**
         * Creates a Color instance from XML.
         *
         * @param xml An E4X XML object containing a <code><color></code> node from Motion XML.
         *
         * @return A Color instance that matches the XML description.       
         */
        public static function fromXML(xml:XML):Color
        {
            return Color((new Color()).parseXML(xml));
        }
        
        
        /**
         * @private
         */
        private function parseXML(xml:XML=null):Color
        {
            if (!xml) return this;
            
            var firstChild:XML = xml.elements()[0];
            if (!firstChild) return this;
            
             ATTRIBUTES
            for each (var att:XML in firstChild.attributes())
            {
                var name:String = att.localName();
                if (name == 'tintColor')
                {
                    var tintColorNumber:uint = Number(att.toString()) as uint;
                    this.tintColor = tintColorNumber;
                }
                else
                {
                    this[name] = Number(att.toString());
                }
            }
            
            
            return this;
        }
        
        
        /**
         * Blends smoothly from one ColorTransform object to another.
         *
         * @param fromColor The starting ColorTransform object.
         *
         * @param toColor The ending ColorTransform object.
         *
         * @param progress The percent of the transition as a decimal, where <code>0</code> is the start and <code>1</code> is the end.
         *
         * @return The interpolated ColorTransform object.         
         */
        public static function interpolateTransform(fromColor:ColorTransform, toColor:ColorTransform, progress:Number):ColorTransform
        {
            var q:Number = 1-progress;
            var resultColor:ColorTransform = new ColorTransform
                (
                    fromColor.redMultiplier*q   + toColor.redMultiplier*progress
                    , fromColor.greenMultiplier*q + toColor.greenMultiplier*progress
                    , fromColor.blueMultiplier*q  + toColor.blueMultiplier*progress
                    , fromColor.alphaMultiplier*q + toColor.alphaMultiplier*progress
                    , fromColor.redOffset*q       + toColor.redOffset*progress
                    , fromColor.greenOffset*q     + toColor.greenOffset*progress
                    , fromColor.blueOffset*q      + toColor.blueOffset*progress
                    , fromColor.alphaOffset*q     + toColor.alphaOffset*progress
                )
            return resultColor;        
        }
        
        
        /**
         * Blends smoothly from one color value to another.
         *
         * @param fromColor The starting color value, in the 0xRRGGBB or 0xAARRGGBB format.
         *
         * @param toColor The ending color value, in the 0xRRGGBB or 0xAARRGGBB format.
         *
         * @param progress The percent of the transition as a decimal, where <code>0</code> is the start and <code>1</code> is the end.
         *
         * @return The interpolated color value, in the 0xRRGGBB or 0xAARRGGBB format.     
         */
        public static function interpolateColor(fromColor:uint, toColor:uint, progress:Number):uint
        {
            var q:Number = 1-progress;
            var fromA:uint = (fromColor >> 24) & 0xFF;
            var fromR:uint = (fromColor >> 16) & 0xFF;
            var fromG:uint = (fromColor >>  8) & 0xFF;
            var fromB:uint =  fromColor        & 0xFF;
            
            var toA:uint = (toColor >> 24) & 0xFF;
            var toR:uint = (toColor >> 16) & 0xFF;
            var toG:uint = (toColor >>  8) & 0xFF;
            var toB:uint =  toColor        & 0xFF;
            
            var resultA:uint = fromA*q + toA*progress;
            var resultR:uint = fromR*q + toR*progress;
            var resultG:uint = fromG*q + toG*progress;
            var resultB:uint = fromB*q + toB*progress;
            var resultColor:uint = resultA << 24 | resultR << 16 | resultG << 8 | resultB;
            return resultColor;        
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值