common: introduce tvg::clamp

This commit is contained in:
Mira Grudzinska 2024-09-02 00:10:54 +02:00 committed by Hermet Park
parent b71b329a01
commit 59f950e71b
2 changed files with 8 additions and 2 deletions

View file

@ -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<int>(start + (end - start) * t);
if (result > 255) result = 255;
else if (result < 0) result = 0;
tvg::clamp(result, 0, 255);
return static_cast<uint8_t>(result);
}

View file

@ -68,6 +68,13 @@ static inline bool equal(float a, float b)
}
template <typename T>
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 */
/************************************************************************/