Color picking in as3

Someone recently wondered how to pick a color of a pixel from the screen in as3. If you have a bitmap image you can simply grab the color value using BitmapData.getPixel(). But if you want to capture a pixel color value in the stage as the user sees it, including vector graphics, you need another technique.

The way I found was to make a 1x1 bitmap, and then drawing the stage to the bitmap using a translation matrix for the offset. The result is then in the single pixel of the bitmap.


public function pickColorFromStage(x:int, y:int):uint {
   var bmd:BitmapData = new BitmapData(1, 1, false, 0x000000);
   var matrix:Matrix = new Matrix();
   matrix.translate(-x, -y);
   bmd.draw(stage, matrix);
   return bmd.getPixel(0, 0);
}

Archived comments (4)

This blog is now a static archive; new comments are closed.

Felipe Rodrigues ·

it returns RGB?

Danik ·

Yes

Seemo ·

This is very useful, Thank you Danik :)

Danik ·

You're welcome :)