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 c30447be98
commit 94047ffd36
2 changed files with 10 additions and 1 deletions

View file

@ -132,4 +132,12 @@ Point operator*(const Point& pt, const Matrix& m)
auto tx = pt.x * m.e11 + pt.y * m.e12 + m.e13;
auto ty = pt.x * m.e21 + pt.y * m.e22 + m.e23;
return {tx, ty};
}
}
uint8_t mathLerp(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

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