Recipe 10.1.
Applying Color Changes
Problem
You want to apply a color to a display
object.
Solution
Assign a flash.geom.ColorTransform object
to the display object's transform.colorTransform
property.
Discussion
Every display object has a transform.colorTransform property. The
colorTransform property is a
flash.geom.ColorTransform object that determines which color
transforms are applied to the object. The colorTransform
property always returns a copy of the actual ColorTransform object applied to
the display object. That means that you cannot directly change the
properties of the colorTransform
property. You can, however, retrieve a copy of the actual
ColorTransform object by assigning the value from the
colorTransform property to a
variable. Then you can change the properties of that copy and
reassign it to the colorTransform
property, as shown in the following example:
var color:ColorTransform = sampleSprite.transform.colorTransform;
color.rgb = 0xFFFFFF;
sampleSprite.transform.colorTransform = color;
The ColorTransform class defines two ways
to change the solid color that is applied to a display object. You
can set the value of the rgb property, as in the
preceding example. The rgb property expects a uint value. Generally it's
convenient to specify the value in hexadecimal representation
(0xRRGGBB), as in the preceding example. You can also set
the redOffset,
greenOffset,
blueOffset, and
alphaOffset
properties to work with each of the offsets individually. The
offset values can range from -255 to 255:
var color:ColorTransform = sampleSprite.transform.colorTransform;
color.redOffset = 255;
color.greenOffset = 255;
color.blueOffset = 255;
sampleSprite.transform.colorTransform = color;
The preceding code sets each offset for a color
transform to 255 to create a white fill effect, and it then applies
that transform to sampleSprite. If you changed the
alphaOffset value as well, it would affect the
transparency of the display object to which the transform is
applied. Applying an alphaOffset via a color transform and
changing a display object's alpha property has the same
effect, except that alphaOffset is on a scale from 0 to
255 and alpha is on a scale from 0 to 100.
See Also
Recipe
10.2
|