forked from: gradient map

by wh0 forked from gradient map (diff: 50)
multipart project 2
part 2 of, I don't know, like 5

Background:
What was I thinking? Of course the built in thing is going
to be faster. Well that would have saved like 2 of 5 parts.

Task:
- paletteMap
- benchmark the performance
♥0 | Line 56 | Modified 2010-05-16 13:45:55 | MIT License
play

ActionScript3 source code

/**
 * Copyright wh0 ( http://wonderfl.net/user/wh0 )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/577l
 */

// forked from wh0's gradient map
/*
multipart project 2
part 2 of, I don't know, like 5

Background:
What was I thinking? Of course the built in thing is going
to be faster. Well that would have saved like 2 of 5 parts.

Task:
- paletteMap
- benchmark the performance
*/

package {
	import net.hires.debug.Stats;
	import flash.display.Sprite;
	import flash.display.BitmapData;
	import flash.utils.getTimer;
	import flash.display.BitmapDataChannel;
	import flash.display.Bitmap;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.events.MouseEvent;
	[SWF(frameRate=15)]
	public class GradMap extends Sprite {
		
		private static const origin:Point = new Point();
		
		private var redArray:Array = new Array();
		private var enabled:Boolean = true;
		private var copy:BitmapData;
		private var src:BitmapData;
		private var srcb:Bitmap;
		private var seed:int;
		private var offsets:Array;
		
		public function GradMap() {
			prepareMap();
			copy = new BitmapData(stage.stageWidth, stage.stageHeight, false);
			src = new BitmapData(stage.stageWidth, stage.stageHeight, false);
			srcb = new Bitmap(src);
			addChild(srcb);
			seed = getTimer();
			offsets = [new Point(), new Point(), new Point()];
			addEventListener(Event.ENTER_FRAME, scroll);
			stage.addEventListener(MouseEvent.CLICK, toggle);
			addChild(new Stats());
		}
		
		private function scroll(e:Event):void {
			offsets[0].x += 3;
			offsets[1].x += 5;
			offsets[2].x += 7;
			if (enabled) {
				copy.perlinNoise(200, 200, 3, seed, false, false, BitmapDataChannel.RED, false, offsets);
				src.paletteMap(copy, copy.rect, origin, redArray);
			} else {
				src.perlinNoise(200, 200, 3, seed, false, false, BitmapDataChannel.RED, false, offsets);
			}
		}
		
		private function toggle(e:MouseEvent):void {
			enabled = !enabled;
		}
		
		private function prepareMap():void {
			for (var x:int = 0; x < 256; x++) {
				var red:uint = x;
				var green:uint = Math.min(0xff, x * 2);
				var blue:uint = Math.min(0xff, x * 3);
				redArray[x] = red << 16 | green << 8 | blue;
			}
		}
		
	}
}