Jul 022011
 

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 1×1 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);
}
 Posted by at 13:14
  • http://www.facebook.com/felipewd Felipe Rodrigues

    it returns RGB?

    • http://Danikgames.com Danik

      Yes

  • Seemo

    This is very useful, Thank you Danik :)

    • http://Danikgames.com Danik

      You’re welcome :)