alpha + RGB -> ARGB?
-
18-09-2019 - |
Question
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.
Solution
var argb : int = (alpha<<24)|rgb;
var rgb : int = 0xFFFFFF & argb;
var alpha : int = (argb>>24)&0xFF;
OTHER TIPS
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;
}
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
private function combineARGB(a:uint,r:uint,g:uint,b:uint):uint {
return ( (a << 24) | ( r << 16 ) | ( g << 8 ) | b );
}
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