topical media & game development

talk show tell print

#graphic-flex-image-effects-03-Flex-ColorRegionTest.ax

#graphic-flex-image-effects-03-Flex-ColorRegionTest.ax [swf] [flash] flex


  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 @ax-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.
parameter: 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.
parameter: 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.
parameter: 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);
                          }
                  }
  
          }
  
  }
  


(C) Æliens 04/09/2009

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.