0
\$\begingroup\$

I want to understand how does Color.Lerp works in unity.
According to this page it is quite clear to understand how to use it but I'm interested in how it is implemented. Does it work on RGB channels? and how? In the docs it says that the t factor is clamped between 0 and 1 so i wonder to know on which values Lerp works with Color.

\$\endgroup\$

1 Answer 1

5
\$\begingroup\$

Color.Lerp in Unity works just as any lerp implementation, which works like this:

float lerp(float a, float b, float t)
{
    t = clamp01(t);
    return a + (b - a) * t;
}

Or with multiple components, like Color:

Color lerp(Color a, Color b, float t)
{
    t = clamp01(t);
    return new Color(
        a.r + (b.r - a.r) * t,
        a.g + (b.g - a.g) * t,
        a.b + (b.b - a.b) * t,
        a.a + (b.a - a.a) * t
    );
}
\$\endgroup\$
1
  • 5
    \$\begingroup\$ tl;dr: it lerps each RGBA channel separately. \$\endgroup\$
    – Philipp
    Dec 22, 2015 at 14:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .