package { import flash.display.Bitmap; import flash.display.BitmapData; import flash.events.MouseEvent; import flash.geom.Rectangle; [SWF(width=1200, height=600, backgroundColor=0x000000)] /* * Draws background using color below mouse and draws box over middle image * using the region in which the color can be found, demonstrating getColorsBoundRect() and fillRect. */ public class graphic_flex_image_effects_03_Flex_ColorRegionTest extends graphic_flex_image_effects_03_Flex_DualImageTest { /** * Copies first image and disposes the second, then sets up listener for mouse movement. */ override protected function operateOnImages():void { // the second image will now be a copy of the first _bitmap1.bitmapData = _bitmapData0.clone(); _bitmapData1.dispose(); stage.addEventListener(MouseEvent.MOUSE_MOVE, onStageMouseMove); } /** * Redraws the application background using the specified color. * * @param color The color to use to draw the background. */ private function drawBackground(color:uint):void { graphics.clear(); graphics.beginFill(color); graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight); graphics.endFill(); } /** * Finds the region containing the specified color in the second image * then fills that rgion with the specified color. * * @param color The color to serach for and to use to fill the rectangle. */ private function drawColorBoundsRect(color:uint):void { var rect:Rectangle = _bitmapData0.getColorBoundsRect(0xFFFFFFFF, color); _bitmap1.bitmapData.fillRect(rect, color); } /** * Handler for when the mouse moves. When the mouse is over the left image, * the background is redrawn and the color is found and drawn into the second image. * * @param event Event dispatched by Stage. */ private function onStageMouseMove(event:MouseEvent):void { var x:Number = event.localX; var y:Number = event.localY; // only update the drawing if the mouse is over the first image if (_bitmap0.hitTestPoint(x, y)) { var color:uint = _bitmapData0.getPixel32(x, y); drawBackground(color); drawColorBoundsRect(color); } } } }