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..

  • 1
    $\begingroup$ Should the colors be equally spaced in terms of t? Or do you need some more general solution for other distributions also? $\endgroup$
    – Dragonseel
    Commented Jul 29, 2016 at 23:07
  • $\begingroup$ 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? $\endgroup$ Commented Jul 30, 2016 at 13:50
  • $\begingroup$ @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.. $\endgroup$
    – lokstok
    Commented Aug 1, 2016 at 18:58
  • $\begingroup$ @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? $\endgroup$
    – lokstok
    Commented Aug 1, 2016 at 18:59
  • $\begingroup$ @lokstok good point - I think that covers it so it shouldn't make a difference to the solution. $\endgroup$ Commented Aug 1, 2016 at 19:08

1 Answer 1


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.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.