How to interpolate a color sequence?

后端 未结 6 1171
有刺的猬
有刺的猬 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:19

    What you've got already looks very good, but I'd simplify the math a little bit:

    int millisNow = ofGetElapsedTimeMillis();
    int millisSinceLastCheck = millisNow % timerPeriod;
    int colorsIndex = (millisNow / timerPerod) % (colors.size() - 1);
    
    
    float p = (float)(millisSinceLastCheck) / (float)(timePeriod);
    colorT.r = colors[colorsIndex+1].r * p + ( colors[colorsIndex].r * ( 1.0 - p ) );
    colorT.g = colors[colorsIndex+1].g * p + ( colors[colorsIndex].g * ( 1.0 - p ) );
    colorT.b = colors[colorsIndex+1].b * p + ( colors[colorsIndex].b * ( 1.0 - p ) );
    colorT.normalize();
    

提交回复
热议问题