You seem to be thinking of numbers as if they are stored like strings.
They aren't.
RGB colors are stored as 3-byte (24-bit) numbers.
For example, red looks like this in binary:
111111110000000000000000b
...which is the same thing as this in hexadecimal:
0xFF0000
... which is the same thing as this in decimal:
16711680
To isolate, for example the red portion, you can use SHIFT RIGHT (>>) to
shift all bits to the "right" by 16 bits. Binary:
111111110000000000000000b >> 16 = 11111111b
Hexadecimal:
0xFF0000 >> 16 = 0xFF
Decimal:
16711680 >> 16 = 255
Generally it's a good idea not to presume that there may not be more
bits to the left, so you can filter them out using a bitwise AND (&). To
explain, this better, here's how to extract the green value from bright
cyan (0xFFFF7F):
The binary value of the color:
111111111111111101111111
Split into colors:
11111111 11111111 01111111
Shift right 8 bits:
1111111111111111101111111b >> 8 = 1111111111111111
In hexadecimal, this result is:
0xFFFF
In decimal, it is:
65535
Clearly this is too large, because it includes the red value. To remove
it, we use a bitwise AND.
1111111111111111b & 0xFF = 11111111b = 0xFF
To illustrate, we are taking this value:
1111111111111111b (=0xFFFF; =65535)
...and doing a bitwise AND with this value:
0000000011111111b (=0x00FF; =255)
This compares each bit in the first number to each bit in the second
number. If both bits are 1 (on), that bit is 1 (on) in the result. If
both bits are 0 (off), both bits are 0 (off) in the result. So the
result is:
0000000011111111b (=0xFF; =255)
...which is, indeed, the green value of the color.
So Martin Wood's example (slightly edited):
var r:Number = color >> 16 & 0xFF;
var g:Number = color >> 8 & 0xFF;
var b:Number = color & 0xFF;
... is how to retrieve the red, green, and blue values from a single RGB
color number.
--
T. Michael Keesey
_______________________________________________
[email protected]
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com