views:

27

answers:

1

How do I create a custom colour scale ideally in Hex? say from yellow to red, depending on the height of an object? is this a correct way to achieve this or is there a better way without having to convert it at the end?:

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  A: 

Hi there,

Here is a function that allows you to find a colour value between two others based on a range of 0-1. I think it will meet your needs

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);
}
Tyler Egeto
great thanks - I just figured out a way of doing it and just literary updated my original question. But this looks like it could be a great way of achieving the desired effect. cheers!
davivid