基于自定义范围的色标?在HEX?

时间:2010-08-25 14:53:56

标签: actionscript-3 math colors hex

如何在十六进制中理想地创建自定义色阶?从黄色到红色说,取决于物体的高度?这是实现这一目标的正确方法,还是有更好的方法而不必在最后转换它?:

var r:int = 255;
var b:int = 0;
var maxHeight:int = 52;
var minHeight:int = 21;

var scale:int = 255 / (maxHeight-minHeight);

var g:int = 255 - ((object.height-minHeight) * scale);

var hexColor:uint = RGBtoHEX(r,g,b);

private function RGBtoHEX(r:int, g:int, b:int) :uint
{
return r << 16 | g << 8 | b;
}

1 个答案:

答案 0 :(得分:1)

这是一个函数,允许您根据0-1的范围在两个其他颜色值之间找到颜色值。我认为它将满足您的需求

private function getBetweenColourByPercent(value:Number = 0.5 /* 0-1 */, highColor:uint = 0xFFFFFF, lowColor:uint = 0x000000):uint {
    var r:uint = highColor >> 16;
    var g:uint = highColor >> 8 & 0xFF;
    var b:uint = highColor & 0xFF;

    r += ((lowColor >> 16) - r) * value;
    g += ((lowColor >> 8 & 0xFF) - g) * value;
    b += ((lowColor & 0xFF) - b) * value;

    return (r << 16 | g << 8 | b);
}