Converting a RGB color and Alpha to ARGB in ActionScript 3

So I was using the color picker component that comes with the Flash IDE. When the user selects a color, the color picker fire a ColorPickerEvent.CHANGE event and from that event you can access the new color that was chosen. The format of this color is a uint; when working with colors I am more used to the hexadecimal format.  From the event you can also access to the hexadecimal value, but as a String which is not that useful if you want to do operation on the color.

The color you get from the color picker component is in the RGB format, so no transparency. If you want to add an alpha channel to that color (converting it to ARGB), let’s say to use the setPixel32 method of the BitmapData class¬† and produce Bitmap with transparency, you’re going to have to modify the uint you can do so using this simple function (AS3):

private function returnARGB(rgb:uint, newAlpha:uint):uint{
  //newAlpha has to be in the 0 to 255 range
  var argb:uint = 0;
  argb += (newAlpha<<24);
  argb += (rgb);
  return argb;

What this function does is that it bit shift the value of the Alpha you input and adds it to the original RGB color making it ARGB. You see, RGB format uses 8 bits for each color for each pixel (so 24 bits for each pixel), when you add transparency, the alpha channel takes up another 8 bits for each pixels (making it 32 bits for each pixel thus the 32 in setPixel32). That also explains why newAlpha has to be from 0 to 255, because 255 is the highest number you can store in 8 bits. The Blue color takes the bits from 1 to 8, the Green color takes the bits from 9 to 16, Red from 17 to 24 and finally Alpha from 25 to 32. This is why we bit shift alpha from 24 bits (newAlpha<<24);

I don’t use the color picker component often, but there seem to be missing the end of the sentence here.

, , , , , , ,

  1. #1 by Aksel - October 19th, 2008 at 06:41

    The code is weird in some way, I dont know why, but it shows a error like this:

    1084: Syntax error: expecting rightparen before semicolon.
    1084: Syntax error: expecting rightbrace before rightparen.

    Maybe its just a noob error Ive done, who knows.

  2. #2 by - October 20th, 2008 at 11:51

    There was a problem in wordpress with the “lesser than signs” I have fixed it and now the code displays well.

  3. #3 by Adriano - February 19th, 2010 at 14:46

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

  4. #4 by 8bitjeff - February 23rd, 2010 at 17:01

    Genius! This is exactly what I was looking for today!

  5. #5 by rand - October 18th, 2010 at 11:10

    @Adriano: 20% alpha is 51 (0 to 255 range), so the code is as follows:
    var argb:uint = rgb | (51<<24);

    Note that the bitwise OR (|) operator is faster than addition and performs the same operation.

  6. #6 by pushpa - October 22nd, 2010 at 11:10


    returnARGB(FILL_COLOR,255) where FILL_COLOR is hex value from xml. i m using the returned value to set the argb color of bitmapdata. in windows its proper where as in mac its showing black. can u guide me as to what is done wrong…..

  7. #7 by rakesh - June 21st, 2011 at 14:15

    how to support rendering unclabrated profiles in flash with out color loss

    help on this is great

    Thanks In advance.

(will not be published)
Subscribe to comments feed

Parse error: syntax error, unexpected ';' in /homepages/25/d169645162/htdocs/wp-content/themes/fusion/footer.php on line 13