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.
public class RGB
{
//r the red channel
public var r:uint;
//the green channel
public var g:uint;
//the blue channel
public var b:uint;
/**
* RGB class stores a color as rgb values and allows one to output that value in various formats
* @param r the red channel
* @param g the green channel
* @param b the blue channel
*
*/
public function RGB(r:uint = 0, g:uint = 0, b:uint = 0)
{
this.r = r;
this.g = g;
this.b = b;
}
/**
* sets the RGB value based on a hex/oct value hex as uint
* @param hex the hex/oct value of the RGB object.
*
*/
public function setByHex(hex:uint):void{
var colorTransform:ColorTransform = new ColorTransform();
colorTransform.color = hex;
r = colorTransform.redOffset;
g = colorTransform.greenOffset;
b = colorTransform.blueOffset;
}
/**
* gets the octal value of the RGB object as a string
* @return the octal value of the RGB object as a string
*
*/
public function getOctalString():String{
return "0x"+getRGBHex();
}
/**
* gets the hex/oct value of the RGB object as uint
* @return the hex/oct value of the RGB object as uint
*
*/
public function getUint():uint{
var colorTransform:ColorTransform = new ColorTransform();
colorTransform.redOffset = r;
colorTransform.greenOffset = g;
colorTransform.blueOffset = b;
return colorTransform.color;
}
/**
* gets the 6 character string representing the RGB
* @return a 6 character string representing the RGB
*
*/
public function getRGBHex():String{
var hexStr:String = RGB._convertChannelToHexStr(r)+RGB._convertChannelToHexStr(g)+RGB._convertChannelToHexStr(b);
var strLength:uint;
strLength = hexStr.length;
if (strLength < 6){
for (var j:uint; j < (6-strLength); j++){
hexStr += "0";
}
}
return hexStr;
}
/**
* gets a 6 character string representing the RGB
* @param hex RGB hex/oct value as uint
* @return a 6 character string representing the RGB hex/oct value in inputed in uint for as hex
*
*/
public static function uintToRGBHex(hex:uint):String{
var colorTransform:ColorTransform = new ColorTransform();
colorTransform.color = hex;
var rgb:RGB = new RGB(colorTransform.redOffset, colorTransform.greenOffset, colorTransform.blueOffset);
return rgb.getRGBHex();
}
/**
* converts the uint chanel representation of a color to a hex string
* @param hex
* @return the string representation of the hex, this does not return the RGB format of a hex
* like 00ff00 use uintToRGBHex getting a string that is always 6 characters long
*/
private static function _convertChannelToHexStr(hex:uint):String {
if (hex > 255){
hex = 255;
trace("hex val overloaded");
}
var hexStr:String = hex.toString(16);
if (hexStr.length < 2){
hexStr = "0"+hexStr;
}
return hexStr;
}
}
view raw RGB.as hosted with ❤ by GitHub