AS3 Getting colours blended between two other colours

Here is another function that can be added to my ColourUtil class. This one gets a specified number of colour steps between two blended colours.

I thought it would be much harder to achieve than it is, luckily the AS3 Color class has the static method interpolateColor which calculates a single colour at a specified position using a ratio value.

The function below is a simplified version of the function found at AS3 Adventure. This blog post also has a much better explanation of the interpolateColor method, and a great demo of the function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
function getColoursBetween(colour1:uint, colour2:uint, steps:int):Array
{
    var arr = [],
        i:uint,
        ratio:Number;
   
    arr.push(colour1);
   
    for (i = 1; i < steps; i++)
    {
        ratio = i / steps;
        arr.push(Color.interpolateColor(colour1,colour2,ratio));
    }
   
    arr.push(colour2);
   
    return arr;
}
Posted toActionScript Flash