FastLED 3.10.6
Loading...
Searching...
No Matches
flux_scalar.cpp.hpp
Go to the documentation of this file.
1// ok no header - implementation for fl/gfx/flux_scalar.h
2
4
5namespace fl {
6
7namespace {
8constexpr i32 kUnityQ16 = 65536;
9} // namespace
10
12
14
16
18 // Round to nearest so mid brightnesses are not biased low. 255 lands on
19 // exactly 65536: 255 * 65536 / 255.
20 const u32 raw = (static_cast<u32>(brightness) * 65536u + 127u) / 255u;
21 return FluxScalar(static_cast<i32>(raw));
22}
23
25 if (raw < 0) {
26 raw = 0;
27 } else if (raw > kUnityQ16) {
28 raw = kUnityQ16;
29 }
30 return FluxScalar(raw);
31}
32
34 // Q16 x Q16 is Q32; the i64 accumulator is required because two
35 // near-unity scalars already exceed i32.
36 const i64 product = static_cast<i64>(mRawQ16) * static_cast<i64>(other.mRawQ16);
37 return FluxScalar(static_cast<i32>((product + 32768) >> 16));
38}
39
41 const i64 raw = static_cast<i64>(scalar.rawQ16());
42 if (raw == kUnityQ16) {
43 // Pure short-circuit: the general path below is already bit-exact at
44 // unity, since drive * 65536 + 32768 >> 16 == drive. This only avoids
45 // walking the buffer on the default full-brightness path. Exactness is
46 // pinned by a test, not by this branch -- verified by removing it.
47 return;
48 }
49 for (size i = 0; i < drives.size(); ++i) {
50 const i64 scaled = static_cast<i64>(drives[i]) * raw;
51 // Round half away from zero so negative signed-wide intermediates
52 // are treated symmetrically.
53 drives[i] = static_cast<i32>(scaled >= 0 ? (scaled + 32768) >> 16
54 : -((-scaled + 32768) >> 16));
55 }
56}
57
58} // namespace fl
fl::UISlider brightness("Brightness", BRIGHTNESS, 0, 255)
static FluxScalar fromRawQ16(i32 raw) FL_NO_EXCEPT
A limiter fraction already in s16.16, clamped to [0, 1].
FluxScalar composedWith(FluxScalar other) const FL_NO_EXCEPT
Multiplicative composition, commutative: combining brightness and the limiter in either order gives t...
FluxScalar(i32 raw) FL_NO_EXCEPT
i32 rawQ16() const FL_NO_EXCEPT
static FluxScalar fromBrightness(u8 brightness) FL_NO_EXCEPT
setBrightness(b) as the linear flux scalar b/255.
static FluxScalar unity() FL_NO_EXCEPT
Identity. Drives pass through unchanged.
A linear flux scalar in s16.16.
Definition flux_scalar.h:21
unsigned char u8
Definition stdint.h:131
void applyFluxScalar(FluxScalar scalar, span< i32 > drives) FL_NO_EXCEPT
Scale every drive by one scalar, in place.
InputGamut g FL_NO_EXCEPT
Definition rgbw.h:121
Base definition for an LED controller.
Definition crgb.hpp:179
fl::i64 i64
Definition stdint.h:221