Table of Contents

Color.setRGB( ) Method Flash 5

assign new offset values for Red, Green, and Blue
colorObj.setRGB(offset);

Arguments

offset

A number in the range 0 to 16777215 (0xFFFFFF), representing the new RGB offsets of colorObj's target clip. Can be a decimal integer or a hexadecimal integer. Numbers outside the allowed range are converted to numbers within the allowed range (using the rules of twos-complement binary notation). Therefore, setRGB( ) cannot be used to set negative offset values (as setTransform( ) can).

Description

The setRGB( ) method assigns new transformation offsets for a movie clip's RGB components. The new offset is most easily specified as a six-digit hexadecimal number of the form 0xRRGGBB, where RR, GG, and BB are two-digit numbers between 00 and FF, representing the Red, Green, and Blue components. For example, the RGB triplet (R:51, G:51, B:102) is equivalent to the hexadecimal value:

0x333366

Hence, to assign a gray RGB offset to a clip named menu, we can use:

var menuColor = new Color("menu");
menuColor.setRGB(0x999999);

Web developers comfortable with six-digit hexadecimal color values in HTML will have an easy time using setRGB( ) with the preceding hexadecimal format. For a primer on decimal, octal, and hexadecimal numbers, see http://www.moock.org/asdg/technotes/basePrimer.

Note that in addition to setting offsets, setRGB( ) also automatically sets the Red, Green, and Blue percentages of a clip's color transformation to 0, meaning that color changes performed via setRGB( ) behave as direct color assignments (not adjustments of the original colors in the clip). To adjust the color of a movie clip in relation to the clip's original colors, use the setTransform( ) method.

Example

Here's a handy technique for generating a number to use with the setRGB( ) method. Our custom combineRGB( ) function shifts the red and green numbers into the proper position in a 24-bit number and then combines the red, green, and blue values using the bitwise OR operator (|). We use the result to assign a color value to the box movie clip. For more information on bitwise operations, see the online technote at http://www.moock.org/asdg/technotes/bitwise.

function combineRGB (red, green, blue) {
  // Combine the color values into a single number
  var RGB = (red<<16) | (green<<8) | blue;
  return RGB;
}
// Create the Color object
var boxColor = new Color("box");
// Set the color of box to the RGB triplet (R:201, G:160, B:21)
boxColor.setRGB(combineRGB(201, 160, 21));

The following example adds a convenient setRGB( ) method to all movie clips. It automatically creates the necessary Color object and then calls the Color.setRGB( ) method:

MovieClip.prototype.setRGB = function (colorValue) {
  new Color(this).setRGB(colorValue);
};
   
// Usage:
theClip_mc.setRGB(0xFF0000);  // Set theClip_mc's color to red

See Also

Color.getRGB( ), Color.setTransform( )


Table of Contents