Source Code: Tracing ARGB (32 Bit) Hex Values

Many of the new features in Flash 8 use ARGB color values. This is great because it allows you to store your color value (RGB) and alpha (A) in a single 32bit integer. Unfortunately, debugging can be tricky because Flash’s built in Number.toString(16) method treats all numbers as signed integers. This can result in some unexpected results when tracing an ARGB value:

var RGB:Number = 0x99FFCC;
trace(RGB.toString(16)); // works fine, traces "99ffcc"
var ARGB:Number = 0xFF99CC66;
trace(ARGB.toString(16)); // not so good, traces "7fffffff"

Because Flash is treating the value as a signed integer, it can only use 31bits to represent the value (1bit represents the sign + or -). This forces Flash to truncate it to the maximum 31bit value, which is 7FFFFFFF.

To work around this, you need to split the number into smaller pieces, then concatenate them as strings. This is pretty easy to do (if a little convoluted looking), and results in the following function which you can copy and paste into your own projects:

function hexToString(p_hex:Number):String {
// this should be all one line:
return (((p_hex>>24) != 0)?Number(p_hex>>>24).toString(16):"")+
Number(p_hex&0xFFFFFF).toString(16);
}
var ARGB:Number = 0xFF99CC66;
trace(hexToString(ARGB)); // yay, traces "ff99cc66"

If you’d like to learn more about bitwise operators, you can check out my article on “Using Bitwise Operators to Manipulate Bits and Color” on Adobe DevNet, or check out my Flash 8 Bootcamp (coming to London, UK and Edmonton, Canada – details coming soon).

Grant Skinner

The "g" in gskinner. Also the "skinner".

@gskinner

8 Comments

  1. “Adobe DevNet” — that will take getting used to.

    Grant — I really enjoyed the workshop in LA and I got a lot out of it. It was a bit chilly though.

  2. Number.toString() Overriding

    Can use in 2~32 radix

    Number.prototype.__toString = Number.prototype.toString;

    Number.prototype.toString = function(radix) {

    return (Number(this) > radix ? (this / radix)[“__toString”](radix) : “”) + (this % radix)[“__toString”](radix);

    };

  3. hi~

    i wonder to the ‘& 0xFF’ operation before toString(16)

    can you tell me why? thanks

  4. Nice little tool!

    I just noticed that your examples on page 4 of the devnet article don’t work.

    var argb:Number = 0xFFC97B33;

    var alpha:Number = (argb & 0xFF000000)>>24;

    trace(“Alpha: “+alpha.toString(16));

    result is -1 instead of FF. What’s going wrong here?

  5. Dorian,

    That’s due to this same issue. Flash is treating the number as a signed int. The easy solution is to use an unsigned right shift operator instead (>>>). I sent MM a little edit to that article, just waiting for it to be added to the live site.

    hbbalfred,

    You’re right, that’s not necessary (updated to remove). It was a leftover from a more generic implementation I built earlier.

  6. Hi Grant,

    thanks a lot for this paper ! It’s really good.

    BWT..in the table 1. “Combinational Operators” the example for the XOR are the same as the ones for the OR …

    thanks again….

    and I hope to see you one day in Montreal !

    //h

  7. Hugues,

    Ach, dang, you’re right. looks like I need a tech editor. I guess this is why I haven’t written a book yet. 😉

    Thanks for pointing that out.

  8. Good Addition

Leave a Reply

Your email address will not be published. Required fields are marked *