From 94047ffd36a89242dac0e633921f6a3ab5391c3b Mon Sep 17 00:00:00 2001 From: Mira Grudzinska Date: Thu, 22 Aug 2024 19:37:42 +0200 Subject: [PATCH] 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. --- src/common/tvgMath.cpp | 10 +++++++++- src/common/tvgMath.h | 1 + 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/common/tvgMath.cpp b/src/common/tvgMath.cpp index d22232ee..0254cce9 100644 --- a/src/common/tvgMath.cpp +++ b/src/common/tvgMath.cpp @@ -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}; -} \ No newline at end of file +} + +uint8_t mathLerp(const uint8_t &start, const uint8_t &end, float t) +{ + auto result = static_cast(start + (end - start) * t); + if (result > 255) result = 255; + else if (result < 0) result = 0; + return static_cast(result); +} diff --git a/src/common/tvgMath.h b/src/common/tvgMath.h index 556ed410..df39e3b9 100644 --- a/src/common/tvgMath.h +++ b/src/common/tvgMath.h @@ -259,5 +259,6 @@ static inline T mathLerp(const T &start, const T &end, float t) return static_cast(start + (end - start) * t); } +uint8_t mathLerp(const uint8_t &start, const uint8_t &end, float t); #endif //_TVG_MATH_H_