关闭

flex根据两个颜色返回任意多个渐变颜色

437人阅读 评论(0) 收藏 举报
分类:
//根据2个颜色返回多个渐变色
	public class ColorGradient
	{
		public function ColorGradient()
		{
		}
		/**  
		 * 输入一个颜色,将它拆成三个部分:  
		 * 红色,绿色和蓝色  
		 */  
		public static function retrieveRGBComponent( color:uint ):Array   
		{   
			var r:Number = color >> 16;   
			var g:Number = (color >> 8) & 0xff;   
			var b:Number = color & 0xff;   
			
			return [r, g, b];   
		}   
		/**  
		 * 红色,绿色和蓝色三色组合  
		 */  
		public static function generateFromRGBComponent( rgb:Array ):int  
		{   
			if( rgb == null || rgb.length != 3 ||    
				rgb[0] < 0 || rgb[0] > 255 ||   
				rgb[1] < 0 || rgb[1] > 255 ||   
				rgb[2] < 0 || rgb[2] > 255 )   
				return 0xFFFFFF;   
			return rgb[0] << 16 | rgb[1] << 8 | rgb[2];   
		}   
		
		/**  
		 * color1是浅色,color2是深色,实现渐变  
		 * steps是指在多大的区域中渐变,  
		 */  
		public static function generateTransitionalColor( color1:uint, color2:uint, steps:int):Array   
		{   
			if( steps < 3 )   
				return [];   
			
			var color1RGB:Array = retrieveRGBComponent( color1 );   
			var color2RGB:Array = retrieveRGBComponent( color2 );   
			
			var colors:Array = [];   
			colors.push( color1 );   
			//steps = steps - 2;   
			
			var redDiff:Number = color2RGB[0] - color1RGB[0];   
			var greenDiff:Number = color2RGB[1] - color1RGB[1];   
			var blueDiff:Number = color2RGB[2] - color1RGB[2];   
			for( var i:int = 1; i < steps - 1; i++)   
			{   
				var tmpRGB:Array = [   
					color1RGB[0] + redDiff * i / steps,   
					color1RGB[1] + greenDiff * i / steps,   
					color1RGB[2] + blueDiff * i / steps   
				];   
				colors.push( generateFromRGBComponent( tmpRGB ) );   
			}   
			colors.push( color2 );   
			
			return colors;
		} 
	}

使用方法:只需调用generateTransitionalColor方法,例如:

 var c1:uint=0xFF0000;

var c2:uint=0x000000;

var colors:Array=ColorGradient.generateTransitionalColor(c1,c2,10);

最后效果图如下:


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:33881次
    • 积分:422
    • 等级:
    • 排名:千里之外
    • 原创:1篇
    • 转载:44篇
    • 译文:0篇
    • 评论:2条
    文章分类
    最新评论