How to interpolate a color sequence?

后端 未结 6 1182
有刺的猬
有刺的猬 2020-12-10 09:49

I need to interpolate or change gradually a sequence of colors, so it goes from colorA to colorB to colorC to colorD and them back to colorA, this need to be based on time e

6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-10 10:10

    Your code is mostly correct, but you are doing the interpolation backwards: i.e. you are interpolating B->A, then C->B, then D->C, etc. This causes the discontinuity when switching colors.

    You should replace this:

    colorT.r = colors[colorsIndex].r * p + ( colors[colorsIndex+1].r * ( 1.0 - p ) );
    

    with:

    colorT.r = colors[colorsIndex].r * (1.0 - p) + ( colors[colorsIndex+1].r * p );
    

    and the same for the other lines.

    Also, as others have said, using a different color space than RGB can provide better looking results.

提交回复
热议问题