Posted on December 13, 2005 by Grant SkinnerMany 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).
Follow @gskinner on Twitter for more news and views on interactive media.
|
|
|
8 Comments
"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.
Posted by: John on Dec 13, 2005 2:10pm URL: http://www.yapiodesign.com/blog/
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);
};
Posted by: Ticore on Dec 13, 2005 11:56pm URL: http://blog.xuite.net/ticore/blog2
hi~
i wonder to the '& 0xFF' operation before toString(16)
can you tell me why? thanks
Posted by: hbbalfred on Dec 14, 2005 12:01am
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?
Posted by: Dorian on Dec 14, 2005 3:32pm
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.
Posted by: Grant Skinner on Dec 14, 2005 4:02pm URL: http://www.gskinner.com/
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
Posted by: Hugues on Dec 14, 2005 7:03pm
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.
Posted by: Grant Skinner on Dec 14, 2005 8:09pm URL: http://www.gskinner.com/
Good Addition
Posted by: Rob on Dec 29, 2005 12:40pm