用AS来调整图片的色调、亮度、灰度、饱和度、对比度、反相虽然不难,但因为涉及到ColorMatrixFilter的颜色矩阵的应用,使用起来有点麻烦,因此写了这个类ColorProperty。
这个类是对MovieClip类扩展,为MovieClip增加了这些属性:
色调:_color
亮度:_brightness
灰度:_grayscale
饱和度:_saturation
对比度:_contrast
反相:_invert
当然,你也可以改写这个类,使之成为一个新类,而不是扩展MovieClip类。
用法(与_width,_height用法一样):
importColorProperty;
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
*/
importflash.filters.ColorMatrixFilter;
classColorProperty
{
//_matrix是ColorMatrixFilter类的默认恒等矩阵
//_nRed,_nGreen,_nBlue是计算机图形颜色亮度的常量
//privatestaticvar_matrix:Array=[1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0];
privatestaticvar_nRed:Number=0.3086;
privatestaticvar_nGreen:Number=0.6094;
privatestaticvar_nBlue:Number=0.0820;
functionColorProperty()
{
}
publicstaticfunctioninit()
{
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);
}
privatestaticfunctionsetColorProperty()
{
//色调Color,getter&setter
MovieClip.prototype.getColor=function():Number
{
returnMovieClip.prototype._color;
}
MovieClip.prototype.setColor=function(nColor:Number):Void
{
varcolorStr:String=nColor.toString(16);
varnRed:Number=Number("0x"+colorStr.slice(0,2));
varnGreen:Number=Number("0x"+colorStr.slice(2,4));
varnBlue:Number=Number("0x"+colorStr.slice(4,6));
varColor_Matrix:Array=[1,0,0,0,nRed,0,1,0,0,nGreen,0,0,1,0,nBlue,0,0,0,1,0];
this.filters=[newColorMatrixFilter(Color_Matrix)];
MovieClip.prototype._color=nColor;
}
//亮度Brightness,getter&setter
MovieClip.prototype.getBrightness=function():Number
{
returnMovieClip.prototype._brightness;
}
MovieClip.prototype.setBrightness=function(offset:Number):Void
{
varBrightness_Matrix:Array=[1,0,0,0,offset,0,1,0,0,offset,0,0,1,0,offset,0,0,0,1,0];
this.filters=[newColorMatrixFilter(Brightness_Matrix)];
MovieClip.prototype._brightness=offset;
}
//灰度Grayscale,getter&setter
MovieClip.prototype.getGrayscale=function():Boolean
{
returnMovieClip.prototype._grayscale;
}
MovieClip.prototype.setGrayscale=function(yes:Boolean):Void
{
if(yes)
{
varGrayscale_Matrix:Array=[_nRed,_nGreen,_nBlue,0,0,_nRed,_nGreen,_nBlue,0,0,_nRed,_nGreen,_nBlue,0,0,0,0,0,1,0];
this.filters=[newColorMatrixFilter(Grayscale_Matrix)];
MovieClip.prototype._grayscale=true;
}else
{
MovieClip.prototype._grayscale=false;
}
}
//饱和度Saturation,getter&setter
MovieClip.prototype.getSaturation=function():Number
{
returnMovieClip.prototype._saturation;
}
MovieClip.prototype.setSaturation=function(nLevel:Number):Void
{
varsrcRa:Number=(1-nLevel)*_nRed+nLevel;
varsrcGa:Number=(1-nLevel)*_nGreen;
varsrcBa:Number=(1-nLevel)*_nBlue;
varsrcRb:Number=(1-nLevel)*_nRed;
varsrcGb:Number=(1-nLevel)*_nGreen+nLevel;
varsrcBb:Number=(1-nLevel)*_nBlue;
varsrcRc:Number=(1-nLevel)*_nRed;
varsrcGc:Number=(1-nLevel)*_nGreen;
varsrcBc:Number=(1-nLevel)*_nBlue+nLevel;
varSaturation_Matrix:Array=[srcRa,srcGa,srcBa,0,0,srcRb,srcGb,srcBb,0,0,srcRc,srcGc,srcBc,0,0,0,0,0,1,0];
this.filters=[newColorMatrixFilter(Saturation_Matrix)];
MovieClip.prototype._saturation=nLevel;
}
//对比度Contrast,getter&setter
MovieClip.prototype.getContrast=function():Number
{
returnMovieClip.prototype._contrast;
}
MovieClip.prototype.setContrast=function(nLevel:Number):Void
{
varScale:Number=nLevel*11;
varOffset:Number=63.5-(nLevel*698.5);
varContrast_Matrix:Array=[Scale,0,0,0,Offset,0,Scale,0,0,Offset,0,0,Scale,0,Offset,0,0,0,1,0];
this.filters=[newColorMatrixFilter(Contrast_Matrix)];
MovieClip.prototype._contrast=nLevel;
}
//反相Invert,getter&setter
MovieClip.prototype.getInvert=function():Boolean
{
returnMovieClip.prototype._invert;
}
MovieClip.prototype.setInvert=function(yes:Boolean):Void
{
if(yes)
{
varInvert_Matrix:Array=[-1,0,0,0,255,0,-1,0,0,255,0,0,-1,0,255,0,0,0,1,0];
this.filters=[newColorMatrixFilter(Invert_Matrix)];
MovieClip.prototype._invert=true;
}else
{
MovieClip.prototype._invert=false;
}
}
}
}
下载:ColorProperty.rar