Mixing Colors | Add 10 Percent of One Color to Another | ActionScript 3.0 (AS3)
Last Updated on Wednesday, 09 June 2010 17:30 Written by Nicholas Dunbar
If you want to add one color to another and then set a movie clip to that tint I have provided the function below. The idea is to sort of mix colors in the way paint is mixed here is how you do it:
{codecaption width="800"}
function addColorToLayer(layerToTint:DisplayObject, hexVal:String):void{
var colorTransform:ColorTransform = new ColorTransform();
var newColor:uint;
var rgb:RGB = new RGB();
var rgb2:RGB = new RGB();
var rgb3:RGB = new RGB();
rgb.r = parseInt( "0x"+hexVal.charAt(0)+hexVal.charAt(1) );
rgb.g = parseInt( "0x"+hexVal.charAt(2)+hexVal.charAt(3) );
rgb.b = parseInt( "0x"+hexVal.charAt(4)+hexVal.charAt(5) );
rgb2.r = layerToTint.transform.colorTransform.redOffset;
rgb2.g = layerToTint.transform.colorTransform.greenOffset;
rgb2.b = layerToTint.transform.colorTransform.blueOffset;
rgb3.r = int( (rgb.r*.1)+(rgb2.r*.9) );
rgb3.g = int( (rgb.g*.1)+(rgb2.g*.9) );
rgb3.b = int( (rgb.b*.1)+(rgb2.b*.9) );
// convert the new number to a hex and then an int and transform the mc
newColor = (rgb3.r << 16) | (rgb3.g << 8) | rgb3.b;
layerToTint.changeColor(newColor);
}
{/codecaption}
layerToTint.transform.colorTransform has to initialized to some color before this will work.


