其他分享
首页 > 其他分享> > c – 如何插入颜色序列?

c – 如何插入颜色序列?

作者:互联网

我需要逐渐插入或改变一系列颜色,因此它从colorA到colorB再到colorC到colorD,然后再返回到colorA,这需要基于经过的时间(以毫秒为单位),任何帮助都将非常受欢迎(算法,伪代码会很棒).

请注意,我正在使用RGB,它可能是0-255或0.0-1.0范围.

这就是我到目前为止,我需要改变每个“timePeriod”的颜色,我们计算经过的时间百分比并改变颜色,这段代码的问题是当它从A到A时有一个跳跃B到B到C等等

int millisNow = ofGetElapsedTimeMillis();
int millisSinceLastCheck = millisNow - lastTimeCheck;
if ( millisSinceLastCheck > timePeriod ) {
    lastTimeCheck = millisNow;
    millisSinceLastCheck = 0;
    colorsIndex++;
    if ( colorsIndex == colors.size()-1 ) colorsIndex = 0;
    cout << "color indes: " << colorsIndex << endl;
    cout << "color indes: " << colorsIndex + 1 << endl;
}
timeFraction = (float)(millisSinceLastCheck) / (float)(timePeriod);
float p = timeFraction;
colorT.r = colors[colorsIndex].r * p + ( colors[colorsIndex+1].r * ( 1.0 - p ) );
colorT.g = colors[colorsIndex].g * p + ( colors[colorsIndex+1].g * ( 1.0 - p ) );
colorT.b = colors[colorsIndex].b * p + ( colors[colorsIndex+1].b * ( 1.0 - p ) );
colorT.normalize();

提前致谢

解决方法:

你的代码大多是正确的,但是你正在向后插值:即你正在插值B-> A,然后是C-> B,然后是D-> C等.这会在切换颜色时导致不连续.

你应该替换这个:

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

有:

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

和其他行相同.

此外,正如其他人所说,使用与RGB不同的色彩空间可以提供更好看的效果.

标签:c,interpolation,colors,sequence,openframeworks
来源: https://codeday.me/bug/20190929/1830043.html