topical media & game development

talk show tell print

#graphic-flex-image-effects-04-Flex-GradientMapTest.ax

#graphic-flex-image-effects-04-Flex-GradientMapTest.ax [swf] [flash] flex


  package {
  
          import flash.display.BitmapData;
          import flash.display.GradientType;
          import flash.display.Shape;
          import flash.filters.ColorMatrixFilter;
          import flash.geom.Matrix;
          import flash.geom.Point;
  
          [SWF(width=300, height=330, backgroundColor=0x000000)]
  
          
Demonstrates the use of BitmapData's paletteMap() to remap an image's color palette to a custom gradient.

  
          public class @ax-graphic-flex-image-effects-04-Flex-GradientMapTest extends graphic_flex_image_effects_04_Flex_AbstractImageLoader {
  
                  
Constructor. Passes path of image to load to super class.

  
                  public function @ax-graphic-flex-image-effects-04-Flex-GradientMapTest() {
                          super("graphic-flex-image-effects-04-assets-goat.jpg");
                  }
  
                  
Run after the image loads in super class. This creates a shape and draws into it a gradient which can then be used to populate color arrays with values for use a palette remapping of the loaded image.

  
                  override protected function runPostImageLoad():void {
                          var bitmapData:BitmapData = _loadedBitmap.bitmapData;
                          var width:uint = stage.stageWidth;
                          var height:uint = stage.stageHeight;
                          // gradient values to use for the custom gradient remapping
                          var colors:Array = [0x0066FD, 0xFFFFFF, 0xFFFFFF, 0x996600, 0xFFCC00, 0xFFFFFF];
                          var ratios:Array = [0, 95, 124, 130, 165, 255];
                          var alphas:Array = [1, 1, 1, 1, 1, 1];
                          var matrix:Matrix = new Matrix();
                          matrix.createGradientBox(256, height);
                          // draw gradient into shape so that its data can be captured
                          var shape:Shape = new Shape();
                          shape.graphics.beginGradientFill(GradientType.LINEAR, colors, alphas, ratios, matrix);
                          shape.graphics.drawRect(0, 0, 256, height);
                          shape.graphics.endFill();
                          var gradient:BitmapData = new BitmapData(256, height);
                          gradient.draw(shape);
                          // run through all 256 horizontal pixels of gradient to determine the colors
                          var red:Array = [];
                          var zeros:Array = [];
                          var color:uint;
                          for (var i:uint = 0; i < 256; i++) {
                                  color = gradient.getPixel(i, 0);
                                  // color only needs to be pushed into one channel array since
                                  // all three channels are combined in final palette remapping
                                  red.push(color);
                                  // a 0 is pushed into the other array, which will be used for both green and blue arrays
                                  zeros.push(0);
                          }
                          gradient.dispose();
                          // add the shape to the stage so that gradient can be seen and compared
                          shape.scaleX = width/256;
                          addChild(shape);
                          // desaturate the image before its palette is remapped
                          desaturate(bitmapData);
                          bitmapData.paletteMap(
                                  bitmapData,
                                  bitmapData.rect,
                                  new Point(),
                                  red,
                                  zeros,
                                  zeros
                          );
                          addChild(_loadedBitmap);
                  }
  
                  
Fully desaturates the specified image using the ColorMatrixFilter.
parameter: bitmapData The image ot desaturate.

  
                  private function desaturate(bitmapData:BitmapData):void {
                          var matrix:Array = [
                                  0.3, 0.59, 0.11, 0, 0,
                                  0.3, 0.59, 0.11, 0, 0,
                                  0.3, 0.59, 0.11, 0, 0,
                                  0,   0,    0,    1, 0
                          ];
                          bitmapData.applyFilter(
                                  bitmapData,
                                  bitmapData.rect,
                                  new Point(),
                                  new ColorMatrixFilter(matrix)
                          );
                  }
  
          }
  
  }
  


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