# How can you interpolate over an array of say 5 colors?

I have an array of color values. I have an interpolating parameter, t, which varies from 0 to 1.

I can't for the life of me figure out how to smoothly interpolate between these color values based on t, such that I get a smooth blended color for any t value.

Any ideas? Its probably something simple that I am blanking out on..

• Should the colors be equally spaced in terms of t? Or do you need some more general solution for other distributions also? Jul 29, 2016 at 23:07
• Do you require the same colour value for t=0 and t=1? That is, must the colour change wrap smoothly at the end points? Jul 30, 2016 at 13:50
• @Dragonseel.. I think i can make them to be equally spaced out.. but I would definitely be interested in hearing about a more general solution as well.. Aug 1, 2016 at 18:58
• @trichoplax.. i don't really need them to be wrapped around.. would it be different than having the same colors at the end points of the array? Aug 1, 2016 at 18:59
• @lokstok good point - I think that covers it so it shouldn't make a difference to the solution. Aug 1, 2016 at 19:08

Say you have colors

$[c_1, c_2, c_3, c_4, c_5]$

And $t$ values at which each color should be purely displayed.

$[t_1, t_2, t_3, t_4, t_5]$

Now your problem is given $t$ which color do I have to display?

1. Find the t-values $t_i$ and $t_{(i+1)}$ between which $t$ lies.
2. Calculate a 0 to 1 ratio where $t$ lies between them

$param = (t - t_i) / (t_{(i+1)} - t_i)$

1. Interpolate the color via

$c = param * c_i + (1-param) * c_{(i+1)}$

If I haven't messed up something in my mind, this should work for any piecwise linear function, and not only if your $t_i$ values lie between 0 and 1.