5
$\begingroup$

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

$\endgroup$
  • 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 Jul 29 '16 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$ – trichoplax Jul 30 '16 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 Aug 1 '16 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 Aug 1 '16 at 18:59
  • $\begingroup$ @lokstok good point - I think that covers it so it shouldn't make a difference to the solution. $\endgroup$ – trichoplax Aug 1 '16 at 19:08
3
$\begingroup$

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.

$\endgroup$

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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