Color.getRGB( ) Method

ActionScript for Flash MX: The Definitive Guide, 2nd Edition
By Colin Moock
Chapter 18.  ActionScript Language Reference
Color.getRGB( ) Method Flash 5

retrieve the current offset values for Red, Green, and Blue
colorObj.getRGB()

Returns

A number representing the current RGB offsets of colorObj's target.

Description

The getRGB( ) method returns a number, ranging from -16777215 to 16777215, that represents the current color offsets for the Red, Green, and Blue components in a clip; to retrieve the color percentages, you must use getTransform( ). Because color offset values normally range from 0 to 255, it's convenient to work with the return value of getRGB( ) in hexadecimal, where each color offset can be represented by a two-digit hex number. To decipher the number returned by getRGB( ), we treat it as a six-digit hex number of the form 0xRRGGBB, where RR is the Red offset, GG is the Green offset, and BB is the Blue offset. For example, if getRGB( ) returns the number 10092339, we convert it to the hex number 0x99FF33, from which we derive the color offsets (R:153, G:255, B:51). Or if getRGB( ) returns the number 255, we convert it to the hex number 0x0000FF, from which we derive the color offsets (R:0, G:0, B:255). The return value of getRGB( ) can be converted to hexadecimal with toString( ), as follows:

// Create a Color object myColor = new Color("myClip"); // Set the Red offset to 255 (FF in hex) myColor.setRGB(0xFF0000); // Retrieve the RGB offset and convert it to hexadecimal hexColor = myColor.getRGB().toString(16); trace(hexColor);  // Displays: ff0000

Hexadecimal color values are familiar to most web developers, as they are often used in HTML tags. For example, here we use a hexadecimal number to specify the background color of an HTML page (equal values for red and blue combine to form pink):

<BODY BGCOLOR="#FF00FF">

The hex color format used in HTML tags is, in fact, the same as the format used by getRGB( ) and setRGB( ). However, it's not mandatory to use hexadecimal to interpret the return value of getRGB( ); we can also extract the individual Red, Green and Blue color offsets from the return value of getRGB( ), using the bitwise operators:

var rgb = myColorObject.getRGB(); var red = (rgb >> 16) & 0xFF;   // Isolate the Red offset and assign it to red var green = (rgb >> 8) & 0xFF;  // Isolate the Green offset and assign it to green var blue  = rgb & 0xFF;         // Isolate the Blue offset and assign it to blue

With the offset values separated into individual variables (one for each primary color), we can examine and manipulate them individually as decimal numbers. However, when we want to apply any offset value changes to a Color object, we must reassemble the offsets into a single number, as shown in the entry for the setRGB( ) method.

Usage

The getRGB( ) and setRGB( ) methods are convenient when we're assigning new colors directly to a clip, without reference to the clip's original color values. However, the getTransform( ) and setTransform( ) methods are better suited to modifying the RGB components of a clip's color transformation in relation to the clip's original colors.

Color offsets are most easily read using getTransform( ), which returns each component separately, rather than as an encoded number containing all three colors as getRGB( ) does. This is especially true when setting negative offsets with setTransform( ), due to the way that negative numbers are represented in binary.

Example

// Create a new Color object for a clip named box boxColor = new Color("box"); // Set a new RGB offset for box boxColor.setRGB(0x333366); // Check the RGB offset for box trace(boxColor.getRGB());      // Displays: 3355494

See Also

Color.getTransform( ), Color.setRGB( )



    ActionScript for Flash MX. The Definitive Guide
    ActionScript for Flash MX: The Definitive Guide, Second Edition
    ISBN: 059600396X
    EAN: 2147483647
    Year: 2002
    Pages: 780
    Authors: Colin Moock

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net