views:

3500

answers:

5

In as3, is there a utility or function to convert an RGB color (e.g. 0xFF0000) and an alpha value (e.g. .5) into a A 32-bit ARGB value?

And from ARGB to RGB + alpha?

Some explanation: a bitmapdata can take an ARGB value in its constructor, but filling shapes in sprites usually takes RGB values. I would like the aforementioned utilities to ensure colors match up.

+7  A: 
Michael Aaron Safyan
A: 

If alpha can be represented with 8 bits, it is possible. Just map each region of your 32-bit ARGB value for the corresponding variable, for instance

0x0ABCDEF1

alpha - 0x0A

red - 0xBC

green - 0xDE

blue - 0xF1

Samuel Carrijo
+1  A: 

A,R,G,B is a common format used by video cards to allow for "texture blending" and transparency in a texture. Most systems only use 8bits for R,G,and B so that leaves another 8bits free out of a 32bit word size which is common in PCs.

The mapping is:

For Bits:
33222222 22221111 11111100 00000000
10987654 32109876 54321098 76543210

AAAAAAAA RRRRRRRR GGGGGGGG BBBBBBBB
00000000 RRRRRRRR GGGGGGGG BBBBBBBB

Something like this:

private function toARGB(rgb:uint, newAlpha:uint):uint{
  var argb:uint = 0;
  argb = (rgb);
  argb += (newAlpha<<24);
  return argb;
}

private function toRGB(argb:uint):uint{
  var rgb:uint = 0;
  argb = (argb & 0xFFFFFF);
  return rgb;
}
NoMoreZealots
A: 

I can't really add anything more to the excellent answers above, however put more simplistically, the alpha values are the last two digits in the uint, for example

//white
var white:uint = 0xFFFFFF;

//transparent white
var transparent:uint = 0xFFFFFFFF;

The scale is as usual, 00 = 1 FF = 0.

Hope this helps

Sorry, but you are a bit confused :)... transparent white is 0x00FFFFFF. The alpha values are the two first digits after the "0x", as in 0xAARRGGBB (Alpha, Red, Green, Blue)."00" (0x00) is 0, while "FF" (0xFF) is 255, which would be equivalent to alpha=1.Cheers...
Cay
Ah, that will be why ;) Cheers
A: 

Please, you could write an example of this color (0xF47900) with alpha of 20%?

Adriano
An answer is not a place to ask a question.
Alex