Reputation: 6979
In my program, I am trying to go from a start Color-> end Color -> start color and so on.
The question is not specific to any language though I am writing this snippet from my own framework code.
Note: lerp modifies the color
while getLerped returns new value
startProgram(){
ofColor startColor; //set as 152,219,255
ofColor endColor; //set as 132,152,184
ofColor lerpedColor = startColor;
float colorShift = 0.01f;
}
//inside the function in my program that is fired continuously per frame
if(lerpedColor == endColor){
endColor.set(startColor);
startColor.set(lerpedColor);
lerpedColor.set(startColor);
cout<<"Swapping end color"<<"\n";
cout<<"Start Color: "<<ofToString(startColor)<<"\n";
cout<<"End Color: "<<ofToString(endColor)<<"\n";
cout<<"Lerped Color: "<<ofToString(lerpedColor)<<"\n";
}
lerpedColor.lerp(endColor,colorShift);
cout<<"Lerped color"<<ofToString(lerpedColor)<<"\n";
My color interpolation from the initial start Color to the end color is happening fine but this doesn't go back from the end Color to the star Color back.
The if condition
for lerpedColor == endColor
is fired only once when I get the following couts:
startColor: 132,152,184
endColor: 152,219,255
lerpedColor: 132,152,184
What's going wrong in here that the interpolation from happens only once and not back?
Upvotes: 0
Views: 762
Reputation: 20087
The correct uniform linear interpolation could be implemented as:
int interp(int src, int dst, float time) {
return src*(1.0-time)+dst*time;
} // where 0.0<=time<=1.0
When there are 3 colors to be interpolated at once, one possibility is to take the max color difference:
Color diff = abs(end - start); // diff.x = abs(end.x - start.x) etc.
int steps = max(diff);
int a=0;
lerp(src, end, (float)a/(float)steps);
a++;
if (a==steps) { // swap start/end
}
In this case there largest color difference will be decremented/incremented by one and the other components will be actually interpolated.
Upvotes: 2