/ src / display / led / effect / display_led_effect_interpolate.cpp
display_led_effect_interpolate.cpp
 1  #include "display_led_effect_interpolate.h"
 2  
 3  namespace display::led::effect
 4  {
 5  	using InterpolationFunction = float (*)(float, float, float);
 6  
 7  	namespace
 8  	{
 9  		// Linear interpolation
10  		constexpr float Lerp(float a, float b, float t)
11  		{
12  			return ((1.0f - t) * a) + (b * t);
13  		}
14  
15  		constexpr RgbColor InterpolateColor(RgbColor a, RgbColor b, float t, InterpolationFunction fn)
16  		{
17  			return Color(
18  				fn(a.red, b.red, t),
19  				fn(a.green, b.green, t),
20  				fn(a.blue, b.blue, t),
21  				fn(a.alpha, b.alpha, t)
22  			);
23  		}
24  	}
25  
26  	void Interpolate::Update(float timeDelta, RgbLedColorBufferDescriptor &colors)
27  	{
28  		if (Effect::AddDeltaTime(timeDelta))
29  		{
30  			colors.ShouldUpdate = true;
31  
32  			float percent = Effect::GetEffectPlaybackPercentage();
33  
34  			RgbColor interpolated = InterpolateColor(startColor, endColor, percent, Lerp);
35  
36  			for (size_t scan = 0; scan < colors.Size; ++scan)
37  			{
38  				colors[scan] = interpolated;
39  			}
40  		}
41  	}
42  
43  } // namespace display::led::effect