FastLED 3.9.15
Loading...
Searching...
No Matches

◆ FL_DISABLE_WARNING()

FL_DISABLE_WARNING_PUSH FL_DISABLE_WARNING ( float- equal)

Definition at line 13 of file map_range.h.

21 {
22
23template <typename T> struct vec2;
24
25namespace map_range_detail {
26
27// primary template: unchanged
28template <typename T, typename U> struct map_range_math;
29template <typename T> bool equals(T a, T b) { return a == b; }
30
31} // namespace map_range_detail
32
33template <typename T, typename U>
34FASTLED_FORCE_INLINE U map_range(T value, T in_min, T in_max, U out_min,
35 U out_max) {
36 // Not fully tested with all unsigned types, so watch out if you use this
37 // with u16 and you value < in_min.
38 using namespace map_range_detail;
39 if (equals(value, in_min)) {
40 return out_min;
41 }
42 if (equals(value, in_max)) {
43 return out_max;
44 }
45 return map_range_math<T, U>::map(value, in_min, in_max, out_min, out_max);
46}
47
48template <typename T, typename U>
49FASTLED_FORCE_INLINE U map_range_clamped(T value, T in_min, T in_max, U out_min,
50 U out_max) {
51 // Not fully tested with all unsigned types, so watch out if you use this
52 // with u16 and you value < in_min.
53 using namespace map_range_detail;
54 value = clamp(value, in_min, in_max);
55 return map_range<T, U>(value, in_min, in_max, out_min, out_max);
56}
57
60
61namespace map_range_detail {
62
63// primary template: unchanged
64template <typename T, typename U> struct map_range_math {
65 static U map(T value, T in_min, T in_max, U out_min, U out_max) {
66 if (in_min == in_max)
67 return out_min;
68 return out_min +
69 (value - in_min) * (out_max - out_min) / (in_max - in_min);
70 }
71};
72
73template <> struct map_range_math<u8, u8> {
74 static u8 map(u8 value, u8 in_min, u8 in_max,
75 u8 out_min, u8 out_max) {
76 if (value == in_min) {
77 return out_min;
78 }
79 if (value == in_max) {
80 return out_max;
81 }
82 // Promote u8 to i16 for mapping.
83 i16 v16 = value;
84 i16 in_min16 = in_min;
85 i16 in_max16 = in_max;
86 i16 out_min16 = out_min;
87 i16 out_max16 = out_max;
88 i16 out16 = map_range<i16, i16>(v16, in_min16, in_max16,
89 out_min16, out_max16);
90 if (out16 < 0) {
91 out16 = 0;
92 } else if (out16 > 255) {
93 out16 = 255;
94 }
95 return static_cast<u8>(out16);
96 }
97};
98
99// partial specialization for U = vec2<V>
100template <typename T, typename V> struct map_range_math<T, vec2<V>> {
101 static vec2<V> map(T value, T in_min, T in_max, vec2<V> out_min,
102 vec2<V> out_max) {
103 if (in_min == in_max) {
104 return out_min;
105 }
106 // normalized [0..1]
107 T scale = (value - in_min) / T(in_max - in_min);
108 // deltas
109 V dx = out_max.x - out_min.x;
110 V dy = out_max.y - out_min.y;
111 // lerp each component
112 V x = out_min.x + V(dx * scale);
113 V y = out_min.y + V(dy * scale);
114 return {x, y};
115 }
116};
117
118inline bool equals(float a, float b) { return ALMOST_EQUAL_FLOAT(a, b); }
119inline bool equals(double d, double d2) { return ALMOST_EQUAL_DOUBLE(d, d2); }
120
121} // namespace map_range_detail
122
123} // namespace fl
int y
Definition simple.h:93
int x
Definition simple.h:92
uint16_t scale
Definition Noise.ino:74
#define FASTLED_FORCE_INLINE
Definition force_inline.h:6
#define ALMOST_EQUAL_FLOAT(a, b)
Definition math_macros.h:63
#define ALMOST_EQUAL_DOUBLE(a, b)
Definition math_macros.h:73
unsigned char u8
Definition int.h:17
FASTLED_FORCE_INLINE T clamp(T value, T min, T max)
Definition clamp.h:10

References ALMOST_EQUAL_DOUBLE, ALMOST_EQUAL_FLOAT, fl::clamp(), FASTLED_FORCE_INLINE, FL_DISABLE_WARNING_FLOAT_CONVERSION, FL_DISABLE_WARNING_IMPLICIT_INT_CONVERSION, FL_DISABLE_WARNING_SIGN_CONVERSION, scale, x, and y.

+ Here is the call graph for this function: