最近做的一个flash拍照 的东西需要添加色彩调节功能。查了下帮助可以使用ColorMatrixFilter滤镜实现。不过需要在他的变换矩阵上下点功夫。
网上有一个类实现了这样的图形算法,不过那个类的一个BUG是当调整了某属性,比如亮度,其他的属性将被复位。于是,搬用了里面的亮度,对比度等转换算法。
封装成下面这个类:
/**//*
*MC颜色、亮度、对比度、色相、饱和度调整模块
*作者:lulustray
*/
import flash.filters.ColorMatrixFilter;
class common.MCColorTransform ...{
//色彩
private var _red:Number = 1;
private var _green:Number = 1;
private var _blue:Number = 1;
//亮度Brightness(取值范围为:-255~255)
private var _brightness:Number = 0;
//对比度(0~0.2)
private var _contrast:Number = 0.1;
//色相hum(取值范围为:-180~180)
private var _hum:Number = 0;
//饱和度Saturation
private var _saturation:Number = 1.5;
//目标MC
private var mc:MovieClip;
//事件管理
private var EventManager:Object;
private static var _nRed:Number = 0.3086;
private static var _nGreen:Number = 0.6094;
private static var _nBlue:Number = 0.0820;
//变换矩阵
private var Contrast_Matrix:Array;
private var Saturation_Matrix:Array;
private var valMatrix:Array;
public function MCColorTransform(mc:MovieClip) ...{
this.mc = mc;
//注册事件对象
EventManager = new Object();
AsBroadcaster.initialize(EventManager);
EventManager.addListener(this);
}
//颜色属性
public function set red(val:Number):Void ...{
_red = val;
EventManager.broadcastMessage("onColorChange");
}
public function get red():Number ...{
return _red;
}
public function set green(val:Number):Void ...{
_green = val;
EventManager.broadcastMessage("onColorChange");
}
public function get green():Number ...{
return _green;
}
public function set blue(val:Number):Void ...{
_blue = val;
EventManager.broadcastMessage("onColorChange");
}
public function get blue():Number ...{
return _blue;
}
//亮度
public function set brightness(val:Number):Void ...{
_brightness = val;
EventManager.broadcastMessage("onColorChange");
}
public function get brightness():Number ...{
return _brightness;
}
//对比度
public function set contrast(val:Number):Void ...{
_contrast = val;
var Scale:Number = val*11;
var Offset:Number = 63.5-(val*698.5);
Contrast_Matrix = [Scale, 0, 0, 0, Offset, 0, Scale, 0, 0, Offset, 0, 0, Scale, 0, Offset, 0, 0, 0, 1, 0];
EventManager.broadcastMessage("onColorChange");
}
public function get contrast():Number ...{
return _contrast;
}
//饱和度
public function set saturation(val:Number):Void ...{
_saturation = val;
var srcRa:Number = (1-val)*_nRed+val;
var srcGa:Number = (1-val)*_nGreen;
var srcBa:Number = (1-val)*_nBlue;
var srcRb:Number = (1-val)*_nRed;
var srcGb:Number = (1-val)*_nGreen+val;
var srcBb:Number = (1-val)*_nBlue;
var srcRc:Number = (1-val)*_nRed;
var srcGc:Number = (1-val)*_nGreen;
var srcBc:Number = (1-val)*_nBlue+val;
Saturation_Matrix = [srcRa, srcGa, srcBa, 0, 0, srcRb, srcGb, srcBb, 0, 0, srcRc, srcGc, srcBc, 0, 0, 0, 0, 0, 1, 0];
EventManager.broadcastMessage("onColorChange");
}
public function get saturation():Number ...{
return _saturation;
}
//色相
public function set hum(val:Number):Void ...{
_hum = val;
val = Math.min(180, Math.max(-180, val))/180*Math.PI;
var cosVal:Number = Math.cos(val);
var sinVal:Number = Math.sin(val);
var lumR:Number = 0.213;
var lumG:Number = 0.715;
var lumB:Number = 0.072;
valMatrix = [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];
EventManager.broadcastMessage("onColorChange");
}
public function get hum():Number ...{
return _hum;
}
//重置颜色
public function reset():Void ...{
_red = 1;
_green = 1;
_blue = 1;
_brightness = 0;
_brightness = 0;
_contrast = 0.1;
_hum = 0;
_saturation = 1.5;
//var filterarr = new Array();
this.mc.filters = null;
}
function onColorChange():Void ...{
var myElements_array:Array = [_red, 0, 0, 0, _brightness, 0, _green, 0, 0, _brightness, 0, 0, _blue, 0, _brightness, 0, 0, 0, 1, 0];
var ColorMatrix_filter = new ColorMatrixFilter(Contrast_Matrix);
var ColorMatrix_filter2 = new ColorMatrixFilter(myElements_array);
var ColorMatrix_filter3 = new ColorMatrixFilter(Saturation_Matrix);
var Colormatrix_filter4 = new ColorMatrixFilter(valMatrix);
this.mc.filters = [ColorMatrix_filter, ColorMatrix_filter2, ColorMatrix_filter3, Colormatrix_filter4];
}
}
效果: