I have n number of ball objects (each starting with a random velocity (0,4)) on the screen (in 2D) moving about colliding with each other. What I want to do is to assign each ball a colour based on their velocity, such that the balls with the highest velocity (say v) have the colour blue and the colour becomes increasingly red as the speed goes down (intermediate velocities take the range of rainbow colours). I understand it's a range conversion problem.
How do I go about this?
I have tried to get the magnitude of the velocity of the ball and rescale it to a range (0,255). Then I use the fill method to give colour to the balls.
public void display(){
float v = sqrt(pow(this.getDX(),2)+pow(this.getDY(),2));
int scale = (int)(v * 255)/8;
stroke(0);
fill(scale,0,scale);
ellipse(this.xpos, this.ypos, this.size ,this.size);
}
}
I am unable to get the desired results. Mostly because I have no clue how to mix the colours, in this case, to get red for slowest balls and blue for the fastest balls.

