topical media & game development

talk show tell print

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

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


  package {
  
          import flash.display.Bitmap;
          import flash.display.BitmapData;
          import flash.events.MouseEvent;
          import flash.geom.ColorTransform;
          import flash.geom.Matrix;
  
          [SWF(width=800, height=500, backgroundColor=0x000000)]
  
          
Demonstrates the use of BitmapData's colorTransform() by presenting a number of variations on a a single image that the user can select from to brighten, darken, and increase the effects of a color channel.

  
          public class @ax-graphic-flex-image-effects-03-Flex-ColorTransformTest extends graphic_flex_image_effects_03_Flex_AbstractImageLoader {
  
                  private var _bitmaps:Vector.<Bitmap>;
  
                  
Constructor. Passes path of image to load to super class.

  
                  public function @ax-graphic-flex-image-effects-03-Flex-ColorTransformTest() {
                          super("graphic-flex-image-effects-assets-harbor.jpg");
                  }
  
                  
Called after image is loaded by super class. This creates the bitmaps in the grid and the initial variations. A mouse listener is also set up to handle when the stage is clicked.

  
                  override protected function runPostImageLoad():void {
                          _bitmaps = new Vector.<Bitmap>();
                          var imageSize:Number = stage.stageWidth/3;
                          // creates 2 rows, 3 columns of bitmaps
                          for (var row:uint = 0; row < 2; row++) {
                                  for (var column:uint = 0; column < 3; column++) {
                                          createBitmap(column*imageSize, row*imageSize);
                                  }
                          }
                          var sourceData:BitmapData = _loadedBitmap.bitmapData;
                          // scales down loaded image to fit within the cells of the grid
                          var matrix:Matrix = new Matrix();
                          var scale:Number = imageSize/sourceData.width;
                          matrix.scale(scale, scale);
                          var copiedData:BitmapData = new BitmapData(sourceData.width*scale, sourceData.height*scale);
                          copiedData.draw(sourceData, matrix);
                          // creates the multiple variants of the same image
                          createVariations(copiedData);
                          stage.addEventListener(MouseEvent.CLICK, onMouseClick);
                  }
  
                  
Creates a Bitmap instanec at the specified location.
parameter: x The x position for the bitmap.
parameter: y The y position for the bitmap.

  
                  private function createBitmap(x:Number, y:Number):void {
                          var bitmap:Bitmap = new Bitmap();
                          bitmap.x = x;
                          bitmap.y = y;
                          addChild(bitmap);
                          _bitmaps.push(bitmap);
                  }
  
                  
Creates the five variations of the current image by using colorTransform().
parameter: original The original image from which to create the variations.

  
                  private function createVariations(original:BitmapData):void {
                          var brighten:Number = 1.3;
                          var darken:Number = 0.7;
                          _bitmaps[0].bitmapData = original;
                          _bitmaps[1].bitmapData = makeVariation(original, new ColorTransform(brighten, brighten, brighten));
                          _bitmaps[2].bitmapData = makeVariation(original, new ColorTransform(darken, darken, darken));
                          _bitmaps[3].bitmapData = makeVariation(original, new ColorTransform(brighten));
                          _bitmaps[4].bitmapData = makeVariation(original, new ColorTransform(1, brighten));
                          _bitmaps[5].bitmapData = makeVariation(original, new ColorTransform(1, 1, brighten));
                  }
  
                  
Alters the colors of the specified image through the use of colorTransform().
parameter: original The image from which to create the variation.
parameter: transform The ColorTransform instance to apply to make the variation.
returns: The transformed image data.

  
                  private function makeVariation(original:BitmapData, transform:ColorTransform):BitmapData {
                          var variation:BitmapData = original.clone();
                          variation.colorTransform(variation.rect, transform);
                          return variation;
                  }
  
                  
Handles the stage mouse click. This sets the clicked image as the new original and generates five new variations in the other cells of the grid.
parameter: event The event dispatched by the stage.

  
                  private function onMouseClick(event:MouseEvent):void {
                          for each (var bitmap:Bitmap in _bitmaps) {
                                  if (bitmap.hitTestPoint(event.localX, event.localY)) {
                                          createVariations(bitmap.bitmapData);
                                          break;
                                  }
                          }
                  }
  
          }
  
  }
  


(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.