3.4. Setting the RGB Relative to Its Current Value
Problem
You want to modify the RGB value of a movie clip relative to its current value.
Solution
Use Color.getRGB( )
to retrieve the current
value, then perform bitwise operations to modify the value, and set
the new value using Color.setRGB( )
.
Discussion
We saw in Recipe 3.3 how to retrieve the
current RGB value of a movie clip using Color.getRGB(
)
and then use bitwise operations to extract the red,
blue, and green components. To perform a relative color adjustment,
simply modify the individual color components and reapply the new
color using Color.setRGB( )
. For example, you
could brighten a movie clip’s color by increasing
the red, green, and blue components by a certain amount:
// Create theColor
object. my_color = new Color(myMovieClip); // Retrieve the current RGB setting. rgb = my_color.getRGB( ); red = (rgb >> 16); green = (rgb >> 8) & 0xFF; blue = rgb & 0xFF; // Brighten the colors by increasing their magnitude. This assumes that thered
, //green
, andblue
values are no more than 245 prior to the operation. red += 10; green += 10; blue += 10; // Combine the components into a single RGB value and apply it withColor.setRGB( )
. rgb = (red << 16) | (green << 8) | blue; my_color.setRGB(rgb);
Get Actionscript Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.