Getting color of the clicked point

by Fumio
♥0 | Line 43 | Modified 2010-07-31 13:23:55 | MIT License
play

ActionScript3 source code

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

package {
	import flash.display.Sprite;
	import flash.display.Shape;
	import flash.display.Graphics;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
        import flash.display.GradientType;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.geom.Matrix;
	import flash.events.MouseEvent;
	[SWF(width = "240",height = "180")]
	public class GetPixelColor extends Sprite {
		private var myShape:Shape = new Shape();
		private var myGraphics:Graphics = myShape.graphics;
		private var my_txt:TextField = new TextField();
		public function GetPixelColor() {
			addChild(myShape);
			myGraphics.beginFill(0x0000FF);
			myGraphics.drawCircle(stage.stageWidth / 2, stage.stageHeight / 2, 50);
			myGraphics.endFill();
			stage.addEventListener(MouseEvent.CLICK, getColor);
			//
			createTextField();
		}
		private function getColor(eventObject:MouseEvent):void {
			var nX:Number = eventObject.stageX;
			var nY:Number = eventObject.stageY;
			var myBitmapData:BitmapData = new BitmapData(1,1,false);
			var myMatrix:Matrix = new Matrix();
			myMatrix.translate(-nX, -nY);
			myBitmapData.draw(stage, myMatrix);
			var nColor:uint = myBitmapData.getPixel(0,0);
			xTrace(nColor.toString(16));
		}
		private function createTextField():void {
			addChild(my_txt);
			my_txt.autoSize = TextFieldAutoSize.LEFT;
		}
		private function xTrace(_str:String):void {
			my_txt.text = _str + " ";
		}
	}
}