common: uint8_t version of the lerp

An uint8_t version of the lerp function
is introduced to handle cases where
the interpolation factor t exceeds 1, which
previously caused overflow issues due to casting.
This commit is contained in:
Mira Grudzinska 2024-08-22 19:37:42 +02:00 committed by Hermet Park
parent 6b0c81bf36
commit 078b1c8d86
2 changed files with 13 additions and 1 deletions

View file

@ -363,4 +363,14 @@ float Bezier::angle(float t) const
return rad2deg(tvg::atan2(pt.y, pt.x));
}
}
uint8_t lerp(const uint8_t &start, const uint8_t &end, float t)
{
auto result = static_cast<int>(start + (end - start) * t);
if (result > 255) result = 255;
else if (result < 0) result = 0;
return static_cast<uint8_t>(result);
}
}

View file

@ -286,6 +286,8 @@ static inline T lerp(const T &start, const T &end, float t)
return static_cast<T>(start + (end - start) * t);
}
uint8_t lerp(const uint8_t &start, const uint8_t &end, float t);
}
#endif //_TVG_MATH_H_