Skip to content

Commit

Permalink
Fix interpolation
Browse files Browse the repository at this point in the history
The macro approach doesn't work here as long as values are passed by reference
  • Loading branch information
Starkku committed Jan 25, 2025
1 parent 5289656 commit 7c5e6a0
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions src/Utilities/Interpolation.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@

namespace detail
{
#define FROM_DOUBLE(type, first, second, percentage, mode) \
(type)(interpolate((double&)first, (double&)second, percentage, mode));

template <typename T>
inline T interpolate(T& first, T& second, double percentage, InterpolationMode mode)
{
Expand All @@ -34,24 +31,34 @@ namespace detail
template <>
inline int interpolate<int>(int& first, int& second, double percentage, InterpolationMode mode)
{
return FROM_DOUBLE(int, first, second, percentage, mode);
double firstValue = first;
double secondValue = second;
return (int)interpolate(firstValue, secondValue, percentage, mode);
}

template <>
inline BYTE interpolate<BYTE>(BYTE& first, BYTE& second, double percentage, InterpolationMode mode)
{
double firstValue = first;
double secondValue = second;
return (BYTE)interpolate(firstValue, secondValue, percentage, mode);
}

template <>
inline ColorStruct interpolate<ColorStruct>(ColorStruct& first, ColorStruct& second, double percentage, InterpolationMode mode)
{
BYTE r = FROM_DOUBLE(BYTE, first.R, second.R, percentage, mode);
BYTE g = FROM_DOUBLE(BYTE, first.G, second.G, percentage, mode);
BYTE b = FROM_DOUBLE(BYTE, first.B, second.B, percentage, mode);
BYTE r = interpolate(first.R, second.R, percentage, mode);
BYTE g = interpolate(first.G, second.G, percentage, mode);
BYTE b = interpolate(first.B, second.B, percentage, mode);
return ColorStruct { r, g, b };
}

template <>
inline TranslucencyLevel interpolate<TranslucencyLevel>(TranslucencyLevel& first, TranslucencyLevel& second, double percentage, InterpolationMode mode)
{
int firstValue = first.GetIntValue();
int secondValue = second.GetIntValue();
int value = FROM_DOUBLE(int, firstValue, secondValue, percentage, mode);
double firstValue = first.GetIntValue();
double secondValue = second.GetIntValue();
int value = (int)interpolate(firstValue, secondValue, percentage, mode);
return TranslucencyLevel(value, true);
}
}

0 comments on commit 7c5e6a0

Please sign in to comment.