lerpColor not working on Processing but working on CodePen

This is integer division in Java – probably not so in the CodePen, which I believe is using ProcessingJS / JavaScript.

To test it, open a new sketch and type:

void draw(){
  println(mouseX, width, mouseX/width);
}

Wiggle the mouse around. See what is happening? 99/100 is still 0 in integer division.

However, we can make either of those a floating point number, then division will give you a value between 0.0 and 1.0, which is what you want. So, you could write any one of these lines:

float amt = (float)mouseX/width;
float amt = float(mouseX)/width;
float amt = mouseX/float(width);
float amt = mouseX/(float)width;
float amt = mouseX * 1.0/width;
float amt = mouseX/(1.0 * width);

…and any of those lines would work with:

color newBackColor = lerpColor(fromColor, toColor, amt);
1 Like