From 59f950e71bc6326b13eef0e527ac0b678a12e9b9 Mon Sep 17 00:00:00 2001 From: Mira Grudzinska Date: Mon, 2 Sep 2024 00:10:54 +0200 Subject: [PATCH] common: introduce tvg::clamp --- src/common/tvgMath.cpp | 3 +-- src/common/tvgMath.h | 7 +++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/common/tvgMath.cpp b/src/common/tvgMath.cpp index 6275554a..b33b4027 100644 --- a/src/common/tvgMath.cpp +++ b/src/common/tvgMath.cpp @@ -367,8 +367,7 @@ float Bezier::angle(float t) const 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; + tvg::clamp(result, 0, 255); return static_cast(result); } diff --git a/src/common/tvgMath.h b/src/common/tvgMath.h index 3d020929..ba701c1b 100644 --- a/src/common/tvgMath.h +++ b/src/common/tvgMath.h @@ -68,6 +68,13 @@ static inline bool equal(float a, float b) } +template +static inline void clamp(T& v, const T& min, const T& max) +{ + if (v < min) v = min; + else if (v > max) v = max; +} + /************************************************************************/ /* Matrix functions */ /************************************************************************/