translating hex values from ByteArray in AS3
I'm new to using ByteArray, and am trying to parse hex color values from an .ase (Adobe Swatch Exchange) using ByteArray in AS3. I can see position of the hex values using a hex editor, but don't know which methods to use to parse the hex value from there. Here are the values copied from the hex editor. The 2 color values are #ffffff and #cdcdcd:
ASEF........¿..........$...#.f.f.f.f.f.f..RGB.?Ä..?Ä..?Ä....¿..........$...#.c.d.0.0.c.d..RGB.?MÕŒ....?MÕŒ
I've made a weak initial attempt to get the first color, but am stuck here :
var byteA:ByteArray = _fr.data;
byteA.position=29;
var tb:ByteArray = new ByteArray()
var tA:Array= new Array(); //temp array
byteA.readBytes(tb,0,11);
trace("TB "+ tb[0]+":" +tb.toString())
Can somebody please show me how to parse out the color value, so it can be added to the temp array tA? As a bonus answer, since there can be multiple c开发者_运维技巧olors in a swatch, advice on a method to parse out all colors in a given .ase file would be greatly appreciated. Thanks in advance for helping me through this!
The ASEF defines colors using RGB, assuming each color is 8 bits (called a byte). That's 24 bits of information. Unfortunately, Flash's ByteArray doesn't have a method that reads just 24 bits. So, we'll read each byte individually and combine them later.
var byteArray:ByteArray = _fr.data;
//Skip past the garbage that isn't the color.
for (var index:int = 0; index < numberOfBytesUntilFirstColor)
{
byteArray.readByte();
}
var redValue:int = byteArray.readByte();
var greenValue:int = byteArray.readByte();
var blueValue:int = byteArray.readByte();
I'll let you calculate numberOfBytesUntilFirstColor as I'm not familiar with ASE file format.
If you want to store the value as an RGB integer, usable by Flash, do this:
var color:int = redValue;
color = color << 8;
color = color | greenValue;
color = color << 8;
color = color | blueValue;
The above code combines the individual color bytes into one 32 bit int (note, the high 8 bits are left as 0. Sometimes Flash uses the high 8 bits for the alpha, sometimes not.)
精彩评论