Why Color.Lerp or Color32.Lerp doesnt work properly?

i have made a simple script to test Color32.Lerp to use it on my Directional Light Color Changing, but it seems like that Color32.Lerp or Color.Lerp only works downwards(backwards), i mean it Interpolates all the RGBA values only when they’re higher than the Interpolated Color Variable, here is an example:

var damp : float = 0.005;
var first : Color32;
var second : Color32;
function Update ()
{
	first = Color32.Lerp(first,second,damp);
}

and my “first” variable is black for example, and my “second” one is white, and it wont interpolate, it will be very dark grey (looks like it interpolates like that: RGBA from 255 * damp = 1.275 RGBA)
and when “first” is white and “second” is black, it interpolates perfectly, so can someone tell me why this happens?

when “first” is white and “second” is black, it interpolates perfectly

I am 100% sure this is not the case - it’s still very bright grey, but you don’t notice it. The reason you’re not reaching your destination color is because you’re using Lerp in a special way. Lerp is really simple to implement:

lerp(a, b, t) = a + (b - a) * t

It means that you get a as result when t is 0, and b when t is 1. And of course, a proportionally interpolated value inbetween when t is between 0 and 1, too. So if you want a proper linear interpolation, you would have to define a set a and a set b, then increase the value of t from 0 to 1 (or 1 to 0) over time.

What you did instead is to put in a constant t and use the updated a every frame. This leads to behaviour that does not match the original definition of what lerp is for - even though it is of course okay to use it that way if the result is what you want.

What happens is that you always go a set percentage of your remaining way to the destination. There is that Meg Ryan movie “I.Q.” where she explains pretty much the same thing. Freely quoted: “When I go half of the way towards you, then again half of the remaining way, and again and again, I will never reach you because I will always leave the other half of the remaining way between us.”

So since the (b - a) term becomes smaller with every iteration (because a gets closer to b), (b - a) * t becomes smaller as well. So in the beginning, your color value changes faster and then the change becomes smaller and smaller. As long as a is not equal to b and t is not 1, a will never become equal to b.

And that is why you never reach your “second” color. No matter which way you put in the parameters.