AS类:颜色属性ColorProperty

所属分类: 网页制作 / Flash 阅读数: 1361
收藏 0 赞 0 分享
用AS来调整图片的色调、亮度、灰度、饱和度、对比度、反相虽然不难,但因为涉及到ColorMatrixFilter的颜色矩阵的应用,使用起来有点麻烦,因此写了这个类ColorProperty。

这个类是对MovieClip类扩展,为MovieClip增加了这些属性:

色调:_color 
亮度:_brightness 
灰度:_grayscale 
饱和度:_saturation 
对比度:_contrast 
反相:_invert
当然,你也可以改写这个类,使之成为一个新类,而不是扩展MovieClip类。

用法(与_width,_height用法一样):

import ColorProperty;
ColorProperty.init();
//色调,用如0xFF0000的16进制
//img._color=0x333399;
//trace(img._color);
//亮度,取值范围为:-255~255
img._brightness = 100;
//trace(img._brightness)
//灰度,布尔值,true为灰度,false则反之。
//img._grayscale = true;
//trace(img._grayscale);
//饱和度,一般范围为:0~3为宜
//img._saturation = 3;
//trace(img._saturation);
//对比度,取值范围为:0~1
//img._contrast = 0.15;
//反相,布尔值,true为反相,false则反之。
//trace(img._contrast);
//img._invert=true;

代码如下:

复制代码 代码如下:

/** 
* @Name:ColorProperty(MovieClip颜色属性) 
* 色调:_color,亮度:_brightness,灰度:_grayscale,饱和度:_saturation,对比度:_contrast,反相:_invert 
* @author:Flashlizi 
* @version:1.0 
*/ 
import flash.filters.ColorMatrixFilter; 
class ColorProperty 

    //_matrix是ColorMatrixFilter类的默认恒等矩阵 
    //_nRed,_nGreen,_nBlue是计算机图形颜色亮度的常量 
    //private static var _matrix : Array = [1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0]; 
    private static var _nRed : Number = 0.3086; 
    private static var _nGreen : Number = 0.6094; 
    private static var _nBlue : Number = 0.0820; 
    function ColorProperty () 
    { 
    } 
    public static function init () 
    { 
        setColorProperty (); 
        //色调Color 
        MovieClip.prototype.addProperty ("_color", MovieClip.prototype.getColor, MovieClip.prototype.setColor); 
        //亮度Brightness(取值范围为:-255~255) 
        MovieClip.prototype.addProperty ("_brightness", MovieClip.prototype.getBrightness, MovieClip.prototype.setBrightness); 
        //灰度Grayscale 
        MovieClip.prototype.addProperty ("_grayscale", MovieClip.prototype.getGrayscale, MovieClip.prototype.setGrayscale); 
        //饱和度Saturation(饱和度级别一般范围为:0~3) 
        MovieClip.prototype.addProperty ("_saturation", MovieClip.prototype.getSaturation, MovieClip.prototype.setSaturation); 
        //对比度Contrast(取值范围为:0~1) 
        MovieClip.prototype.addProperty ("_contrast", MovieClip.prototype.getContrast, MovieClip.prototype.setContrast); 
        //反相Invert 
        MovieClip.prototype.addProperty ("_invert", MovieClip.prototype.getInvert, MovieClip.prototype.setInvert); 
    } 
    private static function setColorProperty () 
    { 
        //色调Color,getter&setter 
        MovieClip.prototype.getColor = function () : Number 
        { 
            return MovieClip.prototype._color; 
        } 
        MovieClip.prototype.setColor = function (nColor : Number) : Void 
        { 
            var colorStr : String = nColor.toString (16); 
            var nRed : Number = Number ("0x" + colorStr.slice (0, 2)); 
            var nGreen : Number = Number ("0x" + colorStr.slice (2, 4)); 
            var nBlue : Number = Number ("0x" + colorStr.slice (4, 6)); 
            var Color_Matrix : Array = [1, 0, 0, 0, nRed, 0, 1, 0, 0, nGreen, 0, 0, 1, 0, nBlue, 0, 0, 0, 1, 0]; 
            this.filters = [new ColorMatrixFilter (Color_Matrix)]; 
            MovieClip.prototype._color = nColor; 
        } 
        //亮度Brightness,getter&setter 
        MovieClip.prototype.getBrightness = function () : Number 
        { 
            return MovieClip.prototype._brightness; 
        } 
        MovieClip.prototype.setBrightness = function (offset : Number) : Void 
        { 
            var Brightness_Matrix : Array = [1, 0, 0, 0, offset, 0, 1, 0, 0, offset, 0, 0, 1, 0, offset, 0, 0, 0, 1, 0]; 
            this.filters = [new ColorMatrixFilter (Brightness_Matrix)]; 
            MovieClip.prototype._brightness = offset; 
        } 
        //灰度Grayscale,getter&setter 
        MovieClip.prototype.getGrayscale = function () : Boolean 
        { 
            return MovieClip.prototype._grayscale; 
        } 
        MovieClip.prototype.setGrayscale = function (yes : Boolean) : Void 
        { 
            if (yes) 
            { 
                var Grayscale_Matrix : Array = [_nRed, _nGreen, _nBlue, 0, 0, _nRed, _nGreen, _nBlue, 0, 0, _nRed, _nGreen, _nBlue, 0, 0, 0, 0, 0, 1, 0]; 
                this.filters = [new ColorMatrixFilter (Grayscale_Matrix)]; 
                MovieClip.prototype._grayscale = true; 
            } else 
            { 
                MovieClip.prototype._grayscale = false; 
            } 
        } 
        //饱和度Saturation,getter&setter 
        MovieClip.prototype.getSaturation = function () : Number 
        { 
            return MovieClip.prototype._saturation; 
        } 
        MovieClip.prototype.setSaturation = function (nLevel : Number) : Void 
        { 
            var srcRa : Number = (1 - nLevel) * _nRed + nLevel; 
            var srcGa : Number = (1 - nLevel) * _nGreen; 
            var srcBa : Number = (1 - nLevel) * _nBlue; 
            var srcRb : Number = (1 - nLevel) * _nRed; 
            var srcGb : Number = (1 - nLevel) * _nGreen + nLevel; 
            var srcBb : Number = (1 - nLevel) * _nBlue; 
            var srcRc : Number = (1 - nLevel) * _nRed; 
            var srcGc : Number = (1 - nLevel) * _nGreen; 
            var srcBc : Number = (1 - nLevel) * _nBlue + nLevel; 
            var Saturation_Matrix : Array = [srcRa, srcGa, srcBa, 0, 0, srcRb, srcGb, srcBb, 0, 0, srcRc, srcGc, srcBc, 0, 0, 0, 0, 0, 1, 0]; 
            this.filters = [new ColorMatrixFilter (Saturation_Matrix)]; 
            MovieClip.prototype._saturation = nLevel; 
        } 
        //对比度Contrast,getter&setter 
        MovieClip.prototype.getContrast = function () : Number 
        { 
            return MovieClip.prototype._contrast; 
        } 
        MovieClip.prototype.setContrast = function (nLevel : Number) : Void 
        { 
            var Scale : Number = nLevel * 11; 
            var Offset : Number = 63.5 - (nLevel * 698.5); 
            var Contrast_Matrix : Array = [Scale, 0, 0, 0, Offset, 0, Scale, 0, 0, Offset, 0, 0, Scale, 0, Offset, 0, 0, 0, 1, 0]; 
            this.filters = [new ColorMatrixFilter (Contrast_Matrix)]; 
            MovieClip.prototype._contrast = nLevel; 
        } 
        //反相Invert,getter&setter 
        MovieClip.prototype.getInvert = function () : Boolean 
        { 
            return MovieClip.prototype._invert; 
        } 
        MovieClip.prototype.setInvert = function (yes : Boolean) : Void 
        { 
            if (yes) 
            { 
                var Invert_Matrix : Array = [ - 1, 0, 0, 0, 255, 0, - 1, 0, 0, 255, 0, 0, - 1, 0, 255, 0, 0, 0, 1, 0]; 
                this.filters = [new ColorMatrixFilter (Invert_Matrix)]; 
                MovieClip.prototype._invert = true; 
            } else 
            { 
                MovieClip.prototype._invert = false; 
            } 
        } 
    } 


下载ColorProperty.rar
更多精彩内容其他人还在看

AS3 中的package(包)应用实例代码

初学者在学习AS3时会遇到什么样的问题呢?只有从初学的角度来实践,才能知道,package 这个高手们必玩的内容,对初学者来说或许就有一些困惑。
收藏 0 赞 0 分享

swfupload使用代码说明

终于在天哪joyous的帮助下,花了一天的时间,搞明白大部分内容. swfupload(以下简称su)遇到的主要问题就是,版本不同造成的极大差异, 现在的版本已经到2.1beta。我用的是2.02版,天哪用的是1.xx版。
收藏 0 赞 0 分享

Flex中最好的MVC框架Mate框架

个人感觉Mate框架非常适合开发一般运用程序,可以提高开发效率,节约成本,同时也比较好维护。但如果要做游戏或是其它控制更强的程序并不适合。
收藏 0 赞 0 分享

Flex 创建复数行文本内容的List

效果不错的flex多行文本
收藏 0 赞 0 分享

flash/flex/air的一个大问题

Close Advertisement 对HTML标签的支持得不够好。现在普遍的做法是浮一个iframe。
收藏 0 赞 0 分享

flex编程动态生成图像

程序需求:动态生成柱形图,柱形条数不确定,柱形字段不确定
收藏 0 赞 0 分享

帮助你学习 Flash / ActionScript的12个网站

Adobe Flash是一个很好的技术,该技术允许开发人员加入的互动性和平稳的动画网页。 其受欢迎程度是如此巨大,您可以看到许多网站,致力于帮助开发者有兴趣在闪光。
收藏 0 赞 0 分享

入门简单的FLEX验证码一例

入门简单的FLEX验证码一例
收藏 0 赞 0 分享

ActionScript3禁止构造请求标头Referer

ActionScript3禁止构造请求标头Referer
收藏 0 赞 0 分享

做了一个flash视频墙[附源文件与xml文件]

这篇文章主要介绍了做了一个flash视频墙[附源文件与xml文件]
收藏 0 赞 0 分享
查看更多