uint to 6 digit RGB hex | tostring(16) | ActionScript 3.0 AS3

What if you want to get that 6 digit color value that is so commonly used in CSS/HTML, design programs, etc from the native way ActionScript 3.0 (AS3) stores an octal (0x00000F is stored as uint 15). I am talking about the number that has the pound or hash sign (#) infront of it, like the following for examples:
# FFFFFF - white,
# FF0000 - red,
# 00FF00 - green,
# 0000FF - blue, or
# 844244 - puce.
Don't be fooled. Everywhere you go on google AS3 programmers simply say use someUint.toString(16) to get the 6 digit hexadecimal representation of that uint. This is incorrect, it gets the hexadecimal, but not the 6 digit representation. A simple hexadecimal is NOT the values we are talking about above. You will not always get the correct value using someUint.toString(16) when someUint is the value returned by movieClip.transform.colorTransform.color. But you will get it often enough to be fooled into thinking that this works suitably as a conversion method to what you really want -- the 6 character long hexadecimal representation of the color.
So if they are different why do we bother with the 6 digit representation? The reason is that it's more human readable than the pure hexadecimal equivalent. For example take a virtually pure blue like #0003FE. This color contains 00 of red, 03 of green, FE of blue. FE hexadecimal = 254 decimal. 03 hexadecimal = 3 decimal and 00 hexadecimal = 0 decimal, which means in plain English that we have blue with a hint of green added. In this format you can tell alot about the color.

If you set the above blue with an unperceivable amount of green to the following flash.geom.ColorTransfer object:
colorTransform.redOffset = 00;
colorTransform.greenOffset = 3;
colorTransform.blueOffset = 254;
Then you trace(colorTransform.color) you will get 1022. So what is this value? It's the uint version of those red, green, blue values. In short it is the index value from the computer's color list from 0 to (256*256*256) colors
Instead of explaining how this number is computed, I have provided an ActionScript 3.0 class used to convert the 6 digit representation to its uint and back.


comments powered by Disqus