Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

alpha + RGB -> ARGB?

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.

like image 210
jedierikb Avatar asked Jul 19 '09 14:07

jedierikb


2 Answers

var argb  : int = (alpha<<24)|rgb;
var rgb   : int = 0xFFFFFF & argb;
var alpha : int = (argb>>24)&0xFF;

like image 67
Michael Aaron Safyan Avatar answered Oct 26 '22 17:10

Michael Aaron Safyan


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;
}
like image 43
NoMoreZealots Avatar answered Oct 26 '22 17:10

NoMoreZealots