MCPcopy Create free account
hub / github.com/FastLED/FastLED / alpha8

Class alpha8

src/fl/math/alpha.h:42–78  ·  view source on GitHub ↗

Unsigned 8-bit alpha / brightness — UNORM8. Represents values in [0.0, 1.0] inclusive. Interpretation: raw / 255 raw 0 = 0.0 raw 128 = 128/255 ~ 0.502 raw 255 = 255/255 = 1.0 FastLED's scale8() uses the (scale+1)>>8 approximation for efficient UNORM scaling: scale8(x, 255) == x (exact identity). Implicit conversion to/from unsigned char preserves full backward compatibility with code that tre

Source from the content-addressed store, hash-verified

40/// Implicit conversion to/from unsigned char preserves full backward
41/// compatibility with code that treats this as a plain u8.
42struct alpha8 {
43 unsigned char value;
44
45 constexpr alpha8() FL_NOEXCEPT : value(0) {}
46
47 // Accept any non-bool integer type (signed or unsigned, any width).
48 template <typename IntT, alpha_detail::enable_if_integer_t<IntT> = 0>
49 constexpr alpha8(IntT v) FL_NOEXCEPT : value(static_cast<unsigned char>(v)) {} // NOLINT — implicit by design
50
51 explicit constexpr alpha8(float f) FL_NOEXCEPT : value(_clamp(f)) {}
52 explicit constexpr alpha8(double f) FL_NOEXCEPT : value(_clamp(static_cast<float>(f))) {}
53 constexpr operator unsigned char() const FL_NOEXCEPT { return value; }
54
55 constexpr unsigned char raw() const FL_NOEXCEPT { return value; }
56 constexpr float to_float() const FL_NOEXCEPT { return value / 255.0f; }
57
58 static constexpr alpha8 from_float(float f) FL_NOEXCEPT {
59 return alpha8(static_cast<unsigned char>(_clamp(f)));
60 }
61
62 alpha8& operator+=(unsigned char rhs) FL_NOEXCEPT { value += rhs; return *this; }
63 alpha8& operator-=(unsigned char rhs) FL_NOEXCEPT { value -= rhs; return *this; }
64 alpha8& operator*=(unsigned char rhs) FL_NOEXCEPT { value *= rhs; return *this; }
65 alpha8& operator/=(unsigned char rhs) FL_NOEXCEPT { value /= rhs; return *this; }
66 alpha8& operator>>=(int rhs) FL_NOEXCEPT { value >>= rhs; return *this; }
67 alpha8& operator<<=(int rhs) FL_NOEXCEPT { value <<= rhs; return *this; }
68 alpha8& operator++() FL_NOEXCEPT { ++value; return *this; }
69 alpha8 operator++(int) FL_NOEXCEPT { alpha8 t = *this; ++value; return t; }
70 alpha8& operator--() FL_NOEXCEPT { --value; return *this; }
71 alpha8 operator--(int) FL_NOEXCEPT { alpha8 t = *this; --value; return t; }
72
73 private:
74 static constexpr unsigned char _clamp(float f) FL_NOEXCEPT {
75 return static_cast<unsigned char>(
76 f <= 0.0f ? 0 : (f >= 1.0f ? 255 : static_cast<unsigned char>(f * 255.0f + 0.5f)));
77 }
78};
79
80/// Unsigned 16-bit alpha / brightness — UNORM16.
81/// Represents values in [0.0, 1.0] inclusive.

Callers 5

alpha_identity<alpha8>Function · 0.85
blurGaussianFunction · 0.85
blurGaussianFunction · 0.85
from_floatMethod · 0.85
blur.cppFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected