Monday, August 16, 2010

Transformations in Actionscript 3: Part 1

Actionscript lets us apply different kinds of transformations to our objects. In this tutorial, you will learn how to apply colors to objects.

To change an object's color, we will need to apply a flash.geom.ColorTransform object to the object's property transform.colorTransform. All displayed objects have that property. Create an object with an instance name of "sample". Here we create a new ColorTransform object:

var color:ColorTransform = sample.transform.colorTransform
color.color = 0xFF0000
sample.transform.colorTransform = color

ColorTransform class has 2 ways of changing pure color of the displayed object. We can set an RGB color, like in the previous example. Color property has an uint value. Usually it is easier to use the 0xRRGGBB format.

We can also change the color with redOffset, greenOffset and blueOffset properties. Each property has a value from -255 to 255. Example:

var color:ColorTransform = sample.transform.colorTransform
color.redOffset=255
color.greenOffset=0
color.blueOffset=0
sample.transform.colorTransform = color

There is also a property callled alphaOffset, which represents the transparency level of our object (value ranges from 0 to 255).

But what if we don't want to fully paint our object with one pure color? It is possible to change the shade of an object.

Use redMultiplier, greenMultiplier, blueMultiplier and alphaMultiplier properties, which can have values from 0 to 1. Multiplier values represent the displayed red, blue, green and alpha components of the pixel's initial color. By default the values are 1, that means the pixel has 100% of red, blue, green and alpha of the initial pixel color.

Setting the values isn't too different from the previous examples:

var color:ColorTransform = sample.transform.colorTransform
color.redMultiplier = 1;
color.greenMultiplier = 0;
color.blueMultiplier = 0;
sample.transform.colorTransform = color

Thanks for reading.

Related:

Transformations in Actionscript 3: Part 2
Transformations in Actionscript 3: Part 3

No comments:

Post a Comment