Decoding an RGB Value

written by: Elis Frugalo; article published: year 2007, month 06;


In: Root » Computers and technology » Flash » Decoding an RGB Value

Dutch French Spanish Portuguese Italian German Japanese Chinese Korean Russian Arabic Bookmark and Share this Article

You want to extract the red, green, and blue components from an RGB value returned by Color.getRGB( ).

Use the bitshift right and bitwise AND operators.

You can extract the red, green, and blue components from the single RGB value returned by Color.getRGB( ) using the bitshift right (>>) and bitwise AND (&) operators. You can extract one or more of the colors individually as follows:

// Create the Color object.
   my_color = new Color(myMovieClip);
// Get the current RGB color.
   rgb = my_color.getRGB(  );
// rgb contains an RGB color value in decimal form, such as 14501017 (rosy pink), 
   // which is stored internally as its hex equivalent, such as 0xDD4499.
   red   = (rgb >> 16);
   green = (rgb >> 8) & 0xFF;
   blue  =  rgb & 0xFF;

Although displayed as a decimal number, remember that each color is stored internally in its hexadecimal form: 0xRRGGBB. For example, the color value 14501017 (which is rosy pink) is stored internally as 0xDD4499. In this format, it is easy to see that the red component is DD in hex (221 in decimal), the green component is 44 in hex (68 in decimal), and the blue component is 99 in hex (153 in decimal).

The preceding transformation effectively separates a 24-bit value into its three 8-bit components (the leftmost eight bits represent red, the middle eight bits represent green, and the rightmost eight bits represent blue). The bitshift right operator is used to shift the eight bits of interest to the rightmost position. Using the bitwise AND operator with 0xFF retains the rightmost eight bits only, effectively masking off any unwanted bits on the left.

In practice, it is often easier to use Color.getTransform( )—in which the red, green, and blue components are returned as separate properties of a transform object—to determine a clip's color. Furthermore, getTransform( ) also returns the alpha value for a color, which getRGB( ) does not

Disclaimer

1) E-articles is not responsible for the information contained by this article as well for any and all copyright infringements by authors and writers. E-articles is a free information resource. If you suspect this article for any copyright infringement, please read the terms of service and contact us to investigate the problem.
2) E-articles is not responsible for inaccuracies, falsehoods, or any other types of misinformation this article may contain and will not be liable for any loss or damage suffered by a user through the user's reliance on the information gained here.

link to this article