From 078b1c8d8671faad1678245f8e01c4c6443d76d0 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 | 12 +++++++++++- src/common/tvgMath.h | 2 ++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/common/tvgMath.cpp b/src/common/tvgMath.cpp index 2b2f1b32..6275554a 100644 --- a/src/common/tvgMath.cpp +++ b/src/common/tvgMath.cpp @@ -363,4 +363,14 @@ float Bezier::angle(float t) const return rad2deg(tvg::atan2(pt.y, pt.x)); } -} \ No newline at end of file + +uint8_t lerp(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 71509d57..3d020929 100644 --- a/src/common/tvgMath.h +++ b/src/common/tvgMath.h @@ -286,6 +286,8 @@ static inline T lerp(const T &start, const T &end, float t) return static_cast(start + (end - start) * t); } +uint8_t lerp(const uint8_t &start, const uint8_t &end, float t); + } #endif //_TVG_MATH_H_