Smolgui
Immediate gui library based on SFML
Loading...
Searching...
No Matches
Interpolation.tpp
Go to the documentation of this file.
1namespace sgui
2{
3/////////////////////////////////////////////////
4template <typename Type>
5constexpr sf::Vector2<Type> round (const sf::Vector2<Type>& value)
6{
7 return sf::Vector2<Type> { std::round (value.x), std::round (value.y) };
8}
9
10/////////////////////////////////////////////////
11template <typename Type>
12constexpr sf::Vector2<Type> round (const sf::Vector3<Type>& value)
13{
14 return sf::Vector3<Type> { std::round (value.x), std::round (value.y), std::round (value.z) };
15}
16
17/////////////////////////////////////////////////
18template <typename Type>
19constexpr Type lerp (
20 const Type& min,
21 const Type& max,
22 const float value)
23{
24 if constexpr (std::is_same_v <Type, sf::Color>) {
25 const uint32_t red = lerp (min.r, max.r, value);
26 const uint32_t green = lerp (min.g, max.g, value);
27 const uint32_t blue = lerp (min.b, max.b, value);
28 const uint32_t alpha = lerp (min.a, max.a, value);
29 return sf::Color (red, green, blue, alpha);
30 }
31 return (1.f - value)*min + value*max;
32}
33
34/////////////////////////////////////////////////
35template <typename Type>
36constexpr Type clamp (
37 const Type min,
38 const Type max,
39 const Type value)
40{
41 return std::min (max, std::max (value, min));
42}
43
44/////////////////////////////////////////////////
45template <typename Type>
46constexpr Type clampedLerp (
47 const Type& min,
48 const Type& max,
49 const float value)
50{
51 return clamp (min, max, lerp (min, max, value));
52}
53
54/////////////////////////////////////////////////
55template <typename Type>
56constexpr float inverseLerp (
57 const Type& min,
58 const Type& max,
59 const Type& value)
60{
61 auto fMin = static_cast<float> (min);
62 auto fMax = static_cast<float> (max);
63 auto fValue = static_cast<float> (value);
64 return clamp (0.f, 1.f, (fValue - fMin) / (fMax - fMin));
65}
66
67/////////////////////////////////////////////////
68template <typename InputType,
69 typename OutputType>
70constexpr OutputType remap (
71 const InputType& minInput,
72 const InputType& maxInput,
73 const OutputType& minOutput,
74 const OutputType& maxOutput,
75 const InputType& value)
76{
77 auto t = inverseLerp (minInput, maxInput, value);
78 return clampedLerp (minOutput, maxOutput, t);
79}
80
81} // namespace sgui