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