最近在项目中需要用到渐变色来表示一些数值,本来以为这种方法网上估计一搜一箩筐,后来却发现几乎没有解决这个问题的方法,不过最后功夫不负有心人,终于在javaEye中找到了关于在AS3下生成颜色渐变值的办法,下面我把代码贴出来,供大家学习使用,同时也感谢javaEye中的“蒙太奇”大哥,不多说了,以下是代码:
package Library.Tools
{
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);
最后效果图如下:
来源:https://www.cnblogs.com/missna/archive/2010/08/19/1803512.html