⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gradienttween.as

📁 GradientTween.as,flash渐变填充
💻 AS
字号:
// Andreas Weber, webweber@motiondraw.com

class com.motiondraw.GradientTween{
	
	public var width:Number;
	public var height:Number;
	public var multiGradient:Array;
	
	private var r:Array;
	private var r1c:Array;
	private var c1c:Array;
	private var a1c:Array;
	private var r2c:Array;
	private var c2c:Array;
	private var a2c:Array;
	private var rLen:Number;
	
	
	private var rotation1:Number;
	private var rotation2:Number;
	
	private var draw:Boolean;
	
	private var rT:Array;

	
	function GradientTween(
			   colors1:Array,
			   alphas1:Array,
			   ratios1:Array,
			   rotation1:Number,
			   
			   colors2:Array,
			   alphas2:Array,
			   ratios2:Array,
			   rotation2:Number,
			   
			   gradModeRotation:String,
			   
			   width:Number,
			   height:Number,
			   canvas:MovieClip,
			   draw:Boolean){
	
		var aC,alphas,c,cC,colors,endC,eI,eV,hex,i,j,k,len,matrix,rC,rotation,sI,searchV,startC,sV,t;
		
		if(draw == undefined){
			draw = true;
		}
		
		this.width = width;
		this.height = height;
		this.rotation1 = rotation1;
		this.rotation2 = rotation2;
		
		r = ratios1.concat(ratios2);
		r.sort(16);
		
		multiGradient = new Array();
		
		for(i=1, len=r.length; i<len; i++){
			if(r[i] == r[i-1]){
				r.splice(i,1);
				len--;
				i--;
			}
		}
		
		rLen = r.length;
		
		
		r1c = ratios1.slice(0);
		c1c = colors1.slice(0);
		a1c = alphas1.slice(0);
		
		r2c = ratios2.slice(0);
		c2c = colors2.slice(0);
		a2c = alphas2.slice(0);
		
		for(var i=0, len=c1c.length; i<len; i++){
			//c1c[i] =  hex2rgbObj(c1c[i]);
			hex = c1c[i];
			c1c[i] = {r:(hex & 0xFF0000) >> 16, g:(hex & 0x00FF00) >> 8, b:hex & 0x0000FF}
		}
		
		for(var i=0, len=c2c.length; i<len; i++){
			//c2c[i] =  hex2rgbObj(c2c[i]);
			hex = c2c[i];
			c2c[i] = {r:(hex & 0xFF0000) >> 16, g:(hex & 0x00FF00) >> 8, b:hex & 0x0000FF}
		}
		
		for(var k=0; k<2; k++){
			aC = k ? a1c : a2c;
			rC = k ? r1c : r2c;
			cC = k ? c1c : c2c;
			
			// Find the new ratios and push in a flag for the new colors
			for(var i=0; i<rLen; i++){
				if(r[i] != rC[0]){
					cC.splice(i,0,'new');
					aC.splice(i,0,'new');
				}
				if(rC[0] <= r[i]){
					rC.splice(0,1);
				}
			}
			
			for(var i=0; i<rLen; i++){
				if(cC[i] == 'new'){
					// find the neighbours which already have a color value
					sI = i-1;
					for(var j=i+1; j<rLen; j++){
						if(cC[j] != 'new'){
							eI = j;
							break;
						}
					}
					// find t of the new ratio, relative to the neighbouring 'old' ratios	
					sV = r[sI]; 
					searchV = r[i];
					eV = r[eI];
					t = (searchV-sV)/(eV-sV);
					
					// linear
					startC = cC[sI];
					endC   = cC[eI];
					cC[i] ={r: (1-t)*startC.r + t*endC.r, g: (1-t)*startC.g + t*endC.g, b: (1-t)*startC.b + t*endC.b};
								
					aC[i] = (1-t)*aC[sI] + t*aC[eI];
				}
			}
		}
		matrix = {matrixType:"box", x:0, y:0, w:width, h:height, r:0};
		
		/*
		(not implemented)
		http://ericlin2.tripod.com/grad2/gradient2.html
		a=scaleX;
		b=rotateSkew0;
		d=rotateSkew1;
		e=scaleY;
		g=translateX;
		h=translateY;
		*/

		//matrix = {a:width, b:0, c:0, d:0, e:height, f:0, g:width/2, h:height/2, i:1};

		colors = new Array();
		alphas = new Array();
		
		// draw for each pixel of the height 1 gradient
		for(var i=0; i<height; i++){
			// linear
			t = i/height;
			
			for(var j=0; j<rLen; j++){
				startC = c1c[j];
				endC   = c2c[j];
				colors[j] = ((1-t)*startC.r + t*endC.r << 16) + ((1-t)*startC.g + t*endC.g << 8) + (1-t)*startC.b + t*endC.b;
				alphas[j] = (1-t)*a1c[j] + t*a2c[j];
				rotation = (1-t)*rotation1 + t*rotation2;
			}
			// these sub-clips are really necessary only if one of the alphas is reduced
			c = canvas.createEmptyMovieClip('c'+i, i);
			
			multiGradient[i] = {};
			multiGradient[i].gradModeRotation = gradModeRotation;
			multiGradient[i].colors = colors.slice(0);
			multiGradient[i].alphas = alphas.slice(0);
			multiGradient[i].rotation = r;
			
// !!!! temporary  !!! - must be cloned
			multiGradient[i].matrix = matrix;
			
			if(draw){
				matrix.r = rotation;
				
				c.beginGradientFill(gradModeRotation, colors, alphas, r, matrix);
				c.moveTo(0, i);
				c.lineTo(width, i);
				c.lineTo(width, 1+i);
				c.lineTo(0, 1+i);
				c.lineTo(0, i);
				c.endFill();
			}
		}
	}
	
	// can not calculate the color if one of the rotation parameters is not 0
	public function getColorOfPoint(p:Object):Number{
		if(this.rotation1 != 0 || this.rotation2 != 0 || p.x > width || p.x < 0 || p.y > height || p.y < 0){
			return -1;
		}
		var eC,endC, i,sC,startC, xInd,xT,yT,yInd;
		
		xT = p.x/width;
		yT = p.y/height;
		
		if(!rT){
			rT = new Array();
			for(i=1; i<rLen; i++){
				rT[i-1] = r[i]/255;
			}
		}
		
		for(i=0; i<rLen-1; i++){
			if(xT < rT[i]){
				xInd = i;
				break;
			}
		}

		// linear
		xT =(xT*255 - r[xInd])/(r[xInd+1]-r[xInd]);

		startC = c1c[xInd];
		endC   = c1c[xInd+1];
		sC ={r: (1-xT)*startC.r + xT*endC.r, g: (1-xT)*startC.g + xT*endC.g, b: (1-xT)*startC.b + xT*endC.b};
		
		startC = c2c[xInd];
		endC   = c2c[xInd+1];
		eC ={r: (1-xT)*startC.r + xT*endC.r, g: (1-xT)*startC.g + xT*endC.g, b: (1-xT)*startC.b + xT*endC.b};

		return 	Math.round(((1-yT)*sC.r + yT*eC.r << 16)+((1-yT)*sC.g + yT*eC.g << 8)+ ((1-yT)*sC.b + yT*eC.b));
	}
	
	public function getSides(){
		
	}
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -