In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

flash on 2011-4-6

Testing ColorMatrixFilter for RGB-based blob detection.
@author makc
Get Adobe Flash player
by makc3d 06 Apr 2011
package  {
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.ColorMatrixFilter;
	import flash.geom.Vector3D;
	import flash.media.Camera;
	import flash.media.Video;
	/**
	 * Testing ColorMatrixFilter for RGB-based blob detection.
	 * @author makc
	 */
	public class CMFTest extends Sprite {
		private var video:Video;
		private var frame:BitmapData;
		private var filter:ColorMatrixFilter = new ColorMatrixFilter;
		public function CMFTest () {
			addChild (video = new Video (465, 232)).y = 233;
			var cam:Camera = Camera.getCamera ();
			cam.setMode (video.width, video.height, stage.frameRate);
			video.attachCamera (cam);

			addChild (new Bitmap (frame = new BitmapData (video.width, video.height, false, 0)));

			stage.addEventListener (Event.ENTER_FRAME, loop);
			stage.addEventListener (MouseEvent.CLICK, click);
		}
		private function loop (e:Event):void {
			video.filters = [];
			frame.draw (video);
			video.filters = [filter];
		}
		private function click (e:MouseEvent):void {
			setColor (frame.getPixel (mouseX, mouseY));
		}

		/**
		 * Init ColorMatrixFilter with color.
		 * @param	color
		 */
		public function setColor (color:uint):void {

			var rgb:Vector3D = new Vector3D (
				(color >> 16) & 255,
				(color >>  8) & 255,
				(color      ) & 255
			);

			// project on ortho-white plane
			var proj:Vector3D = rgb.clone ();

			var sqrt3:Number = 0.57735027;
			var white:Vector3D = new Vector3D (sqrt3, sqrt3, sqrt3);

			white.scaleBy (white.dotProduct (proj));
			proj.decrementBy (white);

			// update filter matrix
			proj.normalize ();
			proj.scaleBy (255 / rgb.dotProduct (proj));

			filter.matrix = [
				proj.x, proj.y, proj.z, 0, 0,
				proj.x, proj.y, proj.z, 0, 0,
				proj.x, proj.y, proj.z, 0, 0,
				0, 0, 0, 1, 0
			];
		}
	}
}