package 
{
	import flash.display.Sprite;
	import flash.display.BitmapData;
	import flash.display.Bitmap;
	import flash.events.Event;
	import flash.media.SoundMixer;
	import flash.geom.Point;
	import flash.filters.BlurFilter;
	import flash.filters.ColorMatrixFilter;
	import flash.filters.DisplacementMapFilter;
	import flash.utils.ByteArray;
	import flash.geom.Matrix;
	import flash.geom.ColorTransform;
	

	import flash.display.GradientType;
	import flash.display.BlendMode;

	public class FiltersEffect extends Sprite
	{
		var bitmapData:BitmapData;
		var cloneBD:BitmapData;
		var bitmap:Bitmap;
		var sprite:Sprite;
		var byteArray:ByteArray;
		var thisW:Number;
		var thisH:Number;
		var float:Number;


		var matrix:Matrix;
		var _matrix:Matrix;


		private var h:Number = 0;
		private var s:Number = 1;
		private var v:Number = 1;

		var i:int = 0;
		public function FiltersEffect(w:Number=550,h:Number=400,isBack:Boolean=true)
		{
			thisW = w;
			thisH = h;
			init();
			if (isBack)
			{
				this.blendMode = BlendMode.SCREEN;
			}
		}

		function init():void
		{
			sprite=new Sprite();
			byteArray=new ByteArray();
			cloneBD = new BitmapData(thisW,thisH,false,0);
			bitmapData = new BitmapData(thisW,thisH,false,0);
			bitmap = addChild(new Bitmap(bitmapData)) as Bitmap;



			this.addEventListener(Event.ENTER_FRAME, onEnterFrameHandler);
		}

		private function onEnterFrameHandler(event:Event):void
		{
			//h s v
			var color:uint = HSVtoRGB(h,s,v);
			h +=  1;

			bitmapData.lock();
			drawSprite(false);

			//applyDisplace(bitmapData);

			//matrixEffect()
			//matrixEffectA();
			
			//matrixEffectB()
			
			matrixEffectC()

			//  bitmapData.applyFilter(bitmapData,bitmapData.rect,new Point(),new ColorMatrixFilter([1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0.95,0]));
			//bitmapData.applyFilter(bitmapData,bitmapData.rect, new Point(), new BlurFilter(8,8,2));


			//bitmapData.applyFilter(bitmapData,bitmapData.rect,new Point(),new BlurFilter(6,5,9));
			//bitmapData.draw(_sprite,null,null,"add");

			bitmapData.unlock();

		}

		private function applyDisplace(bitmapData:BitmapData):void
		{
			cloneBD = bitmapData.clone();
			var _bd:DisplacementMapFilter = new DisplacementMapFilter(cloneBD,new Point(0,0),1,2,10,10,"clamp");
			bitmapData.applyFilter(cloneBD, bitmapData.rect, new Point(0,0), _bd);

			bitmapData.applyFilter(bitmapData,bitmapData.rect, new Point(), new BlurFilter(8,8,2));
			bitmapData.draw(sprite);
		}

		function matrixEffect():void
		{
			matrix = new Matrix(1,0,0,1,0,0);
			bitmapData.draw(sprite,matrix);
			bitmapData.applyFilter(bitmapData,bitmapData.rect,new Point(),new ColorMatrixFilter([1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0.80,0]));
			bitmapData.applyFilter(bitmapData,bitmapData.rect, new Point(), new BlurFilter(2,2,0));
		}



		function matrixEffectA():void
		{

			matrix = new Matrix();
			matrix.a = (thisW + thisW / 40) / thisW;
			matrix.d = (thisH + thisH / 80) / thisH;
			matrix.tx =  -  thisW / 80;
			matrix.ty =  -  thisH / 240;
			_matrix = new Matrix(1,0,0,1,(thisW - thisW * 0.7) / 2,(thisH - thisH * 0.7) / 2);
			_matrix.scale(0.7, 0.7);

			cloneBD = bitmapData.clone();
			bitmapData.draw(cloneBD, matrix, new ColorTransform(0.91,0.92,0.95), null, null, true);
			bitmapData.draw(sprite, _matrix, null, null, null, true);
			bitmapData.applyFilter(bitmapData, bitmapData.rect, new Point(), new BlurFilter(0,0,1));
		}

		function matrixEffectB():void
		{

			matrix=new Matrix();
			matrix.translate( -  thisW/2, -  thisH/2);
			matrix.scale(1.14,1.01);
			matrix.translate(thisW/2,thisH/2);
			bitmapData.draw(bitmapData,matrix,new ColorTransform(0.92,0.92,0.92));
			bitmapData.applyFilter(bitmapData,bitmapData.rect,new Point(),new ColorMatrixFilter([1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0]));
			bitmapData.applyFilter(bitmapData, bitmapData.rect, new Point(), new BlurFilter(8, 8,2));
			bitmapData.draw(sprite, null, null, "add");
		}
		
		
		function matrixEffectC():void
		{

			matrix=new Matrix();
			matrix.translate( -  thisW/2, -  thisH/2);
			matrix.scale(1.1,1.1);
			matrix.translate(thisW/2,thisH/2);
			bitmapData.draw(bitmapData,matrix,new ColorTransform(0.96,0.96,0.96));
			bitmapData.applyFilter(bitmapData,bitmapData.rect,new Point(0,0),new BlurFilter(4,4,1));

			bitmapData.draw(sprite);
		}



		function drawSprite(value:Boolean=true):void
		{
			SoundMixer.computeSpectrum(byteArray,value);
			//float = byteArray.readFloat();

			/*var colors:Array = new Array(0x00ff00,0x00ffff,0xffff00,0xff00ff);
			var alphas:Array = new Array(1,1,1,1);
			var ratios:Array = new Array(0,80,160,255);
			var matrix:Matrix = new Matrix();
			matrix.createGradientBox(thisW, thisH, 0, 0, 0);
			
			sprite.graphics.beginGradientFill(GradientType.LINEAR, colors, alphas, ratios, matrix);
			*/


			sprite.graphics.clear();
			sprite.graphics.lineStyle(0,0xff9900,1);
			var n:Number = thisW / 256;
			sprite.graphics.moveTo(0,thisH/2);
			for (i=0; i <256; i++)
			{
				float = byteArray.readFloat() / 2;
				if (i>0)
				{
					sprite.graphics.lineTo(i*n, thisH/2 - float*thisH/4);
				}
			}
			sprite.graphics.lineStyle(0,0xff00ff,1);
			sprite.graphics.moveTo(thisW, thisH/4*3);
			for (i =256; i > 0; i--)
			{
				float = byteArray.readFloat() / 2;
				sprite.graphics.lineTo(i*n,thisH/4*3 - float*thisH/4);
			}

		}



		private function HSVtoRGB(h:Number, s:Number, v:Number):uint
		{
			var r:Number = 0,g:Number = 0,b:Number = 0;
			var i:Number,x:Number,y:Number,z:Number;

			if (s < 0)
			{
				s = 0;
			}
			if (s > 1)
			{
				s = 1;
			}
			if (v < 0)
			{
				v = 0;
			}
			if (v > 1)
			{
				v = 1;
			}
			h = h % 360;
			if (h < 0)
			{
				h +=  360;
			}
			h /=  60;
			i = h >> 0;
			x = v * (1 - s);
			y = v * (1 - s * (h - i));
			z = v * (1 - s * (1 - h + i));
			switch (i)
			{
				case 0 :
					r = v;
					g = z;
					b = x;
					break;
				case 1 :
					r = y;
					g = v;
					b = x;
					break;
				case 2 :
					r = x;
					g = v;
					b = z;
					break;
				case 3 :
					r = x;
					g = y;
					b = v;
					break;
				case 4 :
					r = z;
					g = x;
					b = v;
					break;
				case 5 :
					r = v;
					g = x;
					b = y;
					break;
			}
			var c:uint;
			c = (r * 255 >> 0) << 16 | (g * 255 >> 0) << 8 | (b * 255 >> 0);
			return c;
		}











	}

}

  

posted on 2021-10-10 20:24  疯子_wu  阅读(61)  评论(0编辑  收藏  举报