Blending Colours

How would I go about blending two colours with a percentage of blend?

For example if I was blending Red (1f,0f,0f) and Blue (0f,0f,1f) but wanted only a 25% transition from red to blue… how would I go about this?


t = 0.25f;
blended.r = lerp(t, a.r, b.r);
blended.g = lerp(t, a.g, b.g);
blended.b = lerp(t, a.b, b.b);

public static float lerp(float t, float a, float b) {
   return a + t * (b - a);
}

Maybe he wants a more precise function that guarantees…


public int lerp(int a, int b, int c) {
return (1-c)*a + b*c
}

Thanks for the help guys, I have now implemented it and it works brilliantly to generate a backdrop for my day/night cycle

Should be float, not int. Other than that, I don’t get what this guarantees nor why it’s more precise…


// Imprecise method which does not guarantee v = v1 when t = 1, due to floating-point arithmetic error.
// This form may be used when the hardware has a native Fused Multiply-Add instruction.
float lerp(float v0, float v1, float t) {
  return v0 + t*(v1-v0);
}

// Precise method which guarantees v = v1 when t = 1.
float lerp(float v0, float v1, float t) {
  return (1-t)*v0 + t*v1;
}