FastLED 3.10.8
Loading...
Searching...
No Matches
noise.cpp.hpp
Go to the documentation of this file.
1
3
4#include "fl/system/fastled.h"
5#include "fl/gfx/hsv16.h"
6#include "fl/math/math.h"
8#include "fl/stl/noexcept.h"
9
10// Forward declarations from src/noise.cpp
11fl::u16 inoise16(fl::u32 x, fl::u32 y, fl::u32 z, fl::u32 t) FL_NO_EXCEPT;
12fl::u16 inoise16(fl::u32 x, fl::u32 y, fl::u32 z) FL_NO_EXCEPT;
13
14
15
16namespace fl {
17
26
28HSV16 noiseRingHSV16(float angle, u32 time, float radius) FL_NO_EXCEPT {
29 // Convert angle to cartesian coordinates
30 float x = fl::cosf(angle);
31 float y = fl::sinf(angle);
32
33 // Map sin/cos values from [-1, 1] to [0, 0xFFFF]
34 // This ensures positive values for uint32_t conversion
35 u32 nx = static_cast<u32>((x + 1.0f) * 0.5f * radius * 0xffff);
36 u32 ny = static_cast<u32>((y + 1.0f) * 0.5f * radius * 0xffff);
37
38 // Sample three different z-slices for H, S, V components
39 // Using offsets 0x0, 0x10000, 0x20000 to separate them in noise space
40 u16 h_raw = inoise16(nx, ny, time);
41 u16 s_raw = inoise16(nx, ny, time + 0x10000);
42 u16 v_raw = inoise16(nx, ny, time + 0x20000);
43
44 // Rescale from observed noise range to full 0-65535 range using global extents
45 u16 h = rescaleNoiseValue16(h_raw);
46 u16 s = rescaleNoiseValue16(s_raw);
47 u16 v = rescaleNoiseValue16(v_raw);
48
49 return HSV16(h, s, v);
50}
51
52CHSV noiseRingHSV8(float angle, u32 time, float radius) FL_NO_EXCEPT {
53 fl::HSV16 hsv16 = noiseRingHSV16(angle, time, radius);
54
55 // Scale 16-bit components down to 8-bit using bit shift with rounding
56 // This preserves the relative position in the value range
57 u8 h = (hsv16.h + 128) >> 8;
58 u8 s = (hsv16.s + 128) >> 8;
59 u8 v = (hsv16.v + 128) >> 8;
60
61 return CHSV(h, s, v);
62}
63
64CRGB noiseRingCRGB(float angle, u32 time, float radius) FL_NO_EXCEPT {
65 // Convert angle to cartesian coordinates
66 float x = fl::cosf(angle);
67 float y = fl::sinf(angle);
68
69 // Map sin/cos values from [-1, 1] to [0, 0xFFFF]
70 // This ensures positive values for uint32_t conversion
71 u32 nx = static_cast<u32>((x + 1.0f) * 0.5f * radius * 0xffff);
72 u32 ny = static_cast<u32>((y + 1.0f) * 0.5f * radius * 0xffff);
73
74 // Sample three different z-slices for R, G, B components (direct RGB)
75 // Using offsets 0x0, 0x10000, 0x20000 to separate them in noise space
76 u16 r16_raw = inoise16(nx, ny, time);
77 u16 g16_raw = inoise16(nx, ny, time + 0x10000);
78 u16 b16_raw = inoise16(nx, ny, time + 0x20000);
79
80 // Rescale from observed noise range to full 0-65535 range using global extents
81 u16 r16 = rescaleNoiseValue16(r16_raw);
82 u16 g16 = rescaleNoiseValue16(g16_raw);
83 u16 b16 = rescaleNoiseValue16(b16_raw);
84
85 // Scale down to 8-bit
86 u8 r = r16 >> 8;
87 u8 g = g16 >> 8;
88 u8 b = b16 >> 8;
89
90 return CRGB(r, g, b);
91}
92
93
95HSV16 noiseSphereHSV16(float angle, float phi, u32 time, float radius) FL_NO_EXCEPT {
96 // Convert spherical coordinates to cartesian
97 // angle: azimuth (0 to 2π), phi: polar angle from north pole (0 to π)
98 // x = sin(phi) * cos(angle)
99 // y = sin(phi) * sin(angle)
100 // z = cos(phi)
101 float sin_phi = fl::sinf(phi);
102 float cos_phi = fl::cosf(phi);
103 float x = sin_phi * fl::cosf(angle);
104 float y = sin_phi * fl::sinf(angle);
105 float z = cos_phi;
106
107 // Map cartesian values from [-1, 1] to [0, 0xFFFF]
108 // This ensures positive values for uint32_t conversion
109 u32 nx = static_cast<u32>((x + 1.0f) * 0.5f * radius * 0xffff);
110 u32 ny = static_cast<u32>((y + 1.0f) * 0.5f * radius * 0xffff);
111 u32 nz = static_cast<u32>((z + 1.0f) * 0.5f * radius * 0xffff);
112
113 // Sample three different t-slices for H, S, V components
114 // Using offsets 0x0, 0x10000, 0x20000 to separate them in noise space
115 u16 h = inoise16(nx, ny, nz, time);
116 u16 s = inoise16(nx, ny, nz, time + 0x10000);
117 u16 v = inoise16(nx, ny, nz, time + 0x20000);
118
119 return HSV16(h, s, v);
120}
121
122CHSV noiseSphereHSV8(float angle, float phi, u32 time, float radius) FL_NO_EXCEPT {
123 HSV16 hsv16 = noiseSphereHSV16(angle, phi, time, radius);
124
125 // Scale 16-bit components down to 8-bit using bit shift with rounding
126 // This preserves the relative position in the value range
127 u8 h = (hsv16.h + 128) >> 8;
128 u8 s = (hsv16.s + 128) >> 8;
129 u8 v = (hsv16.v + 128) >> 8;
130
131 return CHSV(h, s, v);
132}
133
134CRGB noiseSphereCRGB(float angle, float phi, u32 time, float radius) FL_NO_EXCEPT {
135 // Convert spherical coordinates to cartesian
136 // angle: azimuth (0 to 2π), phi: polar angle from north pole (0 to π)
137 // x = sin(phi) * cos(angle)
138 // y = sin(phi) * sin(angle)
139 // z = cos(phi)
140 float sin_phi = fl::sinf(phi);
141 float cos_phi = fl::cosf(phi);
142 float x = sin_phi * fl::cosf(angle);
143 float y = sin_phi * fl::sinf(angle);
144 float z = cos_phi;
145
146 // Map cartesian values from [-1, 1] to [0, 0xFFFF]
147 // This ensures positive values for uint32_t conversion
148 u32 nx = static_cast<u32>((x + 1.0f) * 0.5f * radius * 0xffff);
149 u32 ny = static_cast<u32>((y + 1.0f) * 0.5f * radius * 0xffff);
150 u32 nz = static_cast<u32>((z + 1.0f) * 0.5f * radius * 0xffff);
151
152 // Sample three different t-slices for R, G, B components (direct RGB)
153 // Using offsets 0x0, 0x10000, 0x20000 to separate them in noise space
154 u16 r16 = inoise16(nx, ny, nz, time);
155 u16 g16 = inoise16(nx, ny, nz, time + 0x10000);
156 u16 b16 = inoise16(nx, ny, nz, time + 0x20000);
157
158 u8 r = fl::int_scale<u16, u8>(r16);
159 u8 g = fl::int_scale<u16, u8>(g16);
160 u8 b = fl::int_scale<u16, u8>(b16);
161
162 return CRGB(r, g, b);
163}
164
165
167HSV16 noiseCylinderHSV16(float angle, float height, u32 time, float radius) FL_NO_EXCEPT {
168 // Convert cylindrical coordinates to cartesian
169 // angle: azimuth around cylinder (0 to 2π)
170 // height: vertical position (used directly)
171 // x = cos(angle)
172 // y = sin(angle)
173 // z = height
174 float x = fl::cosf(angle);
175 float y = fl::sinf(angle);
176
177 // Map sin/cos values from [-1, 1] to [0, 0xFFFF]
178 // This ensures positive values for uint32_t conversion
179 u32 nx = static_cast<u32>((x + 1.0f) * 0.5f * radius * 0xffff);
180 u32 ny = static_cast<u32>((y + 1.0f) * 0.5f * radius * 0xffff);
181 u32 nz = static_cast<u32>(height * radius * 0xffff);
182
183 // Sample three different t-slices for H, S, V components
184 // Using offsets 0x0, 0x10000, 0x20000 to separate them in noise space
185 u16 h_raw = inoise16(nx, ny, nz, time);
186 u16 s_raw = inoise16(nx, ny, nz, time + 0x10000);
187 u16 v_raw = inoise16(nx, ny, nz, time + 0x20000);
188
189 // Rescale from observed noise range to full 0-65535 range using global extents
190 u16 h = rescaleNoiseValue16(h_raw);
191 u16 s = rescaleNoiseValue16(s_raw);
192 u16 v = rescaleNoiseValue16(v_raw);
193
194 return HSV16(h, s, v);
195}
196
197CHSV noiseCylinderHSV8(float angle, float height, u32 time, float radius) FL_NO_EXCEPT {
198 HSV16 hsv16 = noiseCylinderHSV16(angle, height, time, radius);
199
200 // Scale 16-bit components down to 8-bit using bit shift with rounding
201 // This preserves the relative position in the value range
202 u8 h = (hsv16.h + 128) >> 8;
203 u8 s = (hsv16.s + 128) >> 8;
204 u8 v = (hsv16.v + 128) >> 8;
205
206 return CHSV(h, s, v);
207}
208
209CRGB noiseCylinderCRGB(float angle, float height, u32 time, float radius) FL_NO_EXCEPT {
210 // Convert cylindrical coordinates to cartesian
211 // angle: azimuth around cylinder (0 to 2π)
212 // height: vertical position (used directly)
213 // x = cos(angle)
214 // y = sin(angle)
215 // z = height
216 float x = fl::cosf(angle);
217 float y = fl::sinf(angle);
218
219 // Map sin/cos values from [-1, 1] to [0, 0xFFFF]
220 // This ensures positive values for uint32_t conversion
221 u32 nx = static_cast<u32>((x + 1.0f) * 0.5f * radius * 0xffff);
222 u32 ny = static_cast<u32>((y + 1.0f) * 0.5f * radius * 0xffff);
223 u32 nz = static_cast<u32>(height * radius * 0xffff);
224
225 // Sample three different t-slices for R, G, B components (direct RGB)
226 // Using offsets 0x0, 0x10000, 0x20000 to separate them in noise space
227 u16 r16_raw = inoise16(nx, ny, nz, time);
228 u16 g16_raw = inoise16(nx, ny, nz, time + 0x10000);
229 u16 b16_raw = inoise16(nx, ny, nz, time + 0x20000);
230
231 // Rescale from observed noise range to full 0-65535 range using global extents
232 u16 r16 = rescaleNoiseValue16(r16_raw);
233 u16 g16 = rescaleNoiseValue16(g16_raw);
234 u16 b16 = rescaleNoiseValue16(b16_raw);
235
236 // Scale down to 8-bit
237 u8 r = fl::int_scale<u16, u8>(r16);
238 u8 g = fl::int_scale<u16, u8>(g16);
239 u8 b = fl::int_scale<u16, u8>(b16);
240
241 return CRGB(r, g, b);
242}
243
244} // namespace fl
int y
Definition simple.h:93
int x
Definition simple.h:92
uint32_t z[NUM_LAYERS]
Definition Fire2023.h:93
#define FASTLED_FORCE_INLINE
static uint32_t t
Definition Luminova.h:55
fl::u16 inoise16(fl::u32 x, fl::u32 y, fl::u32 z, fl::u32 t) FL_NO_EXCEPT
Functions to generate noise patterns on rings and spheres.
Internal FastLED header for implementation files.
CRGB noiseCylinderCRGB(float angle, float height, u32 time, float radius) FL_NO_EXCEPT
Generate CRGB noise for a cylinder pattern.
HSV16 noiseCylinderHSV16(float angle, float height, u32 time, float radius) FL_NO_EXCEPT
Cylinder noise functions - sample three z-slices for independent component evolution.
CHSV noiseCylinderHSV8(float angle, float height, u32 time, float radius) FL_NO_EXCEPT
Generate HSV8 (8-bit) noise for a cylinder pattern.
fl::hsv8 CHSV
Definition chsv.h:11
CHSV noiseRingHSV8(float angle, u32 time, float radius) FL_NO_EXCEPT
Generate HSV8 (8-bit) noise for a ring pattern.
Definition noise.cpp.hpp:52
CRGB noiseRingCRGB(float angle, u32 time, float radius) FL_NO_EXCEPT
Generate CRGB noise for a ring pattern.
Definition noise.cpp.hpp:64
HSV16 noiseRingHSV16(float angle, u32 time, float radius) FL_NO_EXCEPT
Ring noise functions - sample three z-slices for independent component evolution.
Definition noise.cpp.hpp:28
CHSV noiseSphereHSV8(float angle, float phi, u32 time, float radius) FL_NO_EXCEPT
Generate HSV8 (8-bit) noise for a sphere pattern.
CRGB noiseSphereCRGB(float angle, float phi, u32 time, float radius) FL_NO_EXCEPT
Generate CRGB noise for a sphere pattern.
HSV16 noiseSphereHSV16(float angle, float phi, u32 time, float radius) FL_NO_EXCEPT
Sphere noise functions - sample three z-slices for independent component evolution.
Definition noise.cpp.hpp:95
unsigned char u8
Definition stdint.h:131
fl::CRGB CRGB
Definition video.h:18
u8 u8 fract8 blur_amount FL_NO_EXCEPT
Definition blur.h:186
u8 u8 height
Definition blur.h:186
float sinf(float value) FL_NO_EXCEPT
Definition math.h:360
fl::u64 time() FL_NO_EXCEPT
Alias for millis64() - returns 64-bit millisecond time.
Definition chrono.h:346
FASTLED_FORCE_INLINE U map_range_clamped(T value, T in_min, T in_max, U out_min, U out_max) FL_NO_EXCEPT
Definition math.h:194
FASTLED_FORCE_INLINE u16 rescaleNoiseValue16(u16 raw_value) FL_NO_EXCEPT
Rescale raw inoise16() output to full 16-bit range [0, 65535] Curries in the global NOISE16_EXTENT_MI...
Definition noise.cpp.hpp:22
constexpr u16 NOISE16_EXTENT_MAX
Definition noise.h:24
InputGamut g
Definition rgbw.h:125
float cosf(float value) FL_NO_EXCEPT
Definition math.h:366
constexpr u16 NOISE16_EXTENT_MIN
Observed min/max extents for inoise16() output.
Definition noise.h:23
Base definition for an LED controller.
Definition crgb.hpp:179
#define FL_NO_EXCEPT
Representation of an 8-bit RGB pixel (Red, Green, Blue)
Definition crgb.h:38
u16 v
Definition hsv16.h:12
u16 h
Definition hsv16.h:10
u16 s
Definition hsv16.h:11