FastLED 3.9.15
Loading...
Searching...
No Matches
map_range.h
Go to the documentation of this file.
1
2#pragma once
3
4#include <stdint.h>
5
6#include "fl/clamp.h"
7#include "fl/force_inline.h"
8#include "fl/math_macros.h"
9
10#pragma GCC diagnostic push
11#pragma GCC diagnostic ignored "-Wfloat-equal"
12
13namespace fl {
14
15template <typename T> struct vec2;
16
18
19// primary template: unchanged
20template <typename T, typename U> struct map_range_math;
21template <typename T> bool equals(T a, T b) { return a == b; }
22
23} // namespace map_range_detail
24
25template <typename T, typename U>
26FASTLED_FORCE_INLINE U map_range(T value, T in_min, T in_max, U out_min,
27 U out_max) {
28 // Not fully tested with all unsigned types, so watch out if you use this
29 // with uint16_t and you value < in_min.
30 using namespace map_range_detail;
31 if (equals(value, in_min)) {
32 return out_min;
33 }
34 if (equals(value, in_max)) {
35 return out_max;
36 }
37 return map_range_math<T, U>::map(value, in_min, in_max, out_min, out_max);
38}
39
40template <typename T, typename U>
41FASTLED_FORCE_INLINE U map_range_clamped(T value, T in_min, T in_max, U out_min,
42 U out_max) {
43 // Not fully tested with all unsigned types, so watch out if you use this
44 // with uint16_t and you value < in_min.
45 using namespace map_range_detail;
46 value = clamp(value, in_min, in_max);
47 return map_range<T, U>(value, in_min, in_max, out_min, out_max);
48}
49
52
53namespace map_range_detail {
54
55// primary template: unchanged
56template <typename T, typename U> struct map_range_math {
57 static U map(T value, T in_min, T in_max, U out_min, U out_max) {
58 if (in_min == in_max)
59 return out_min;
60 return out_min +
61 (value - in_min) * (out_max - out_min) / (in_max - in_min);
62 }
63};
64
65template <> struct map_range_math<uint8_t, uint8_t> {
66 static uint8_t map(uint8_t value, uint8_t in_min, uint8_t in_max,
67 uint8_t out_min, uint8_t out_max) {
68 if (value == in_min) {
69 return out_min;
70 }
71 if (value == in_max) {
72 return out_max;
73 }
74 // Promote uint8_t to int16_t for mapping.
75 int16_t v16 = value;
76 int16_t in_min16 = in_min;
77 int16_t in_max16 = in_max;
78 int16_t out_min16 = out_min;
79 int16_t out_max16 = out_max;
80 int16_t out16 = map_range<uint16_t, uint16_t>(v16, in_min16, in_max16,
81 out_min16, out_max16);
82 if (out16 < 0) {
83 out16 = 0;
84 } else if (out16 > 255) {
85 out16 = 255;
86 }
87 return static_cast<uint8_t>(out16);
88 }
89};
90
91// partial specialization for U = vec2<V>
92template <typename T, typename V> struct map_range_math<T, vec2<V>> {
93 static vec2<V> map(T value, T in_min, T in_max, vec2<V> out_min,
94 vec2<V> out_max) {
95 if (in_min == in_max) {
96 return out_min;
97 }
98 // normalized [0..1]
99 T scale = (value - in_min) / T(in_max - in_min);
100 // deltas
101 V dx = out_max.x - out_min.x;
102 V dy = out_max.y - out_min.y;
103 // lerp each component
104 V x = out_min.x + V(dx * scale);
105 V y = out_min.y + V(dy * scale);
106 return {x, y};
107 }
108};
109
110inline bool equals(float a, float b) { return ALMOST_EQUAL_FLOAT(a, b); }
111inline bool equals(double d, double d2) { return ALMOST_EQUAL_DOUBLE(d, d2); }
112
113} // namespace map_range_detail
114
115} // namespace fl
116
117#pragma GCC diagnostic pop
UISlider scale("Scale", 1.0f, 0.0f, 1.0f, 0.01f)
uint32_t x[NUM_LAYERS]
Definition Fire2023.ino:82
uint32_t y[NUM_LAYERS]
Definition Fire2023.ino:83
#define FASTLED_FORCE_INLINE
Definition force_inline.h:6
#define ALMOST_EQUAL_FLOAT(a, b)
Definition math_macros.h:37
#define ALMOST_EQUAL_DOUBLE(a, b)
Definition math_macros.h:41
bool equals(T a, T b)
Definition map_range.h:21
FASTLED_FORCE_INLINE U map_range_clamped(T value, T in_min, T in_max, U out_min, U out_max)
Definition map_range.h:41
FASTLED_FORCE_INLINE T clamp(T value, T min, T max)
Definition clamp.h:10
FASTLED_FORCE_INLINE U map_range(T value, T in_min, T in_max, U out_min, U out_max)
Definition map_range.h:26
Implements a simple red square effect for 2D LED grids.
Definition crgb.h:16
static vec2< V > map(T value, T in_min, T in_max, vec2< V > out_min, vec2< V > out_max)
Definition map_range.h:93
static uint8_t map(uint8_t value, uint8_t in_min, uint8_t in_max, uint8_t out_min, uint8_t out_max)
Definition map_range.h:66
static U map(T value, T in_min, T in_max, U out_min, U out_max)
Definition map_range.h:57