ofColor lerp problem

Hey guys! Hope you have a good 2022!

Im trying to get the steps between 2 colors using ofLerp Function, but it seems to have a precision problem since it does not return the indicated values. Look at this:

ofColor A(255,0,0);
ofColor B(255,255,0);

    for(int j = 0; j< 5; j+=1){
        ofColor color_temp = A.lerp(B,factor);
        gamma.push_back(color_temp);
        factor+=0.2;
    }

What i expect to get (since 0.2 is added to the factor in every step) are this values:

[255, 0, 0] 
[255, 51, 0] 
[255, 102, 0] 
[255, 153, 0] 
[255, 204, 0] 
[255, 255, 0]

but instead, i get this:

255, 0, 0
255, 51, 0
255, 132, 0
255, 205, 0
255, 245, 0
255, 255, 0

If in the lerp function I put a non-variable value (like 0.4 for example) it returns the indicated value (255,102,0). But if I use a variable like it is in this case, it seems to get out of adjustment every turn of the loop. Any idea?

EDIT: OK, what i think is happen is that previous lerp is affecting the next one.

EDIT 2: solve creating this function. By sure its a better way to fix that:

lerp_color(ofColor A, ofColor B, float factor){
    ofColor r;
    
    ofVec3f a;
    ofVec3f b;

    a.x = A.r;
    a.y = A.g;
    a.z = A.b;
    
    b.x = B.r;
    b.y = B.g;
    b.z = B.b;
    
    float newR = ofLerp(a.x, b.x,factor);
    float newG = ofLerp(a.y, b.y,factor);
    float newB = ofLerp(a.z, b.z,factor);
    
    r.set(newR,newG,newB);
    
    return r;

}

I am not 100% sure I understand what you are trying to do but in case it’s helpful, the error in the first code is that A.lerp(B,factor) in fact changes A, so every time through the loop, one of the two things you are mixing is changing. here’s another way of writing it which means A doesn’t change:

float factor = 0.0;
    ofColor A(255,0,0);
    ofColor B(255,255,0);

        for(int j = 0; j< 5; j+=1){
            
            
            ofColor color_temp = A;
            color_temp.lerp(B,factor);
            cout << color_temp << endl;
            
            factor+=0.2;
        }

Likewise, you can use the .getLerped() function. It does what @zach suggests, but internally.

So I suspect that

ofColor A(255,0,0);
ofColor B(255,255,0);

    for(int j = 0; j< 5; j+=1){
        ofColor color_temp = A.getLerped(B,factor);
        gamma.push_back(color_temp);
        factor+=0.2;
    }

will do what you want, assuming that factor is initialized to zero somewhere … I’d probably just write it as:

ofColor A(255,0,0);
ofColor B(255,255,0);

for(float factor = 0; factor <= 1; factor += 0.2){
    gamma.push_back(A.getLerped(B,factor));
}