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

Class alpha16

src/fl/math/alpha.h:87–134  ·  view source on GitHub ↗

Unsigned 16-bit alpha / brightness — UNORM16. Represents values in [0.0, 1.0] inclusive. Interpretation: raw / 65535 raw 0 = 0.0 raw 32768 = 32768/65535 ~ 0.500 raw 65535 = 65535/65535 = 1.0

Source from the content-addressed store, hash-verified

85/// raw 32768 = 32768/65535 ~ 0.500
86/// raw 65535 = 65535/65535 = 1.0
87struct alpha16 {
88 unsigned short value;
89
90 constexpr alpha16() FL_NOEXCEPT : value(0) {}
91
92 // Accept any non-bool integer type (signed or unsigned, any width).
93 template <typename IntT, alpha_detail::enable_if_integer_t<IntT> = 0>
94 constexpr alpha16(IntT v) FL_NOEXCEPT : value(static_cast<unsigned short>(v)) {} // NOLINT — implicit by design
95
96 explicit constexpr alpha16(float f) FL_NOEXCEPT : value(_clamp(f)) {}
97 explicit constexpr alpha16(double f) FL_NOEXCEPT : value(_clamp(static_cast<float>(f))) {}
98 constexpr operator unsigned short() const FL_NOEXCEPT { return value; }
99
100 constexpr unsigned short raw() const FL_NOEXCEPT { return value; }
101 constexpr float to_float() const FL_NOEXCEPT { return value / 65535.0f; }
102
103 static constexpr alpha16 from_float(float f) FL_NOEXCEPT {
104 return alpha16(static_cast<unsigned short>(_clamp(f)));
105 }
106
107 alpha16& operator+=(unsigned short rhs) FL_NOEXCEPT { value += rhs; return *this; }
108 alpha16& operator-=(unsigned short rhs) FL_NOEXCEPT { value -= rhs; return *this; }
109 alpha16& operator*=(unsigned short rhs) FL_NOEXCEPT { value *= rhs; return *this; }
110 alpha16& operator/=(unsigned short rhs) FL_NOEXCEPT { value /= rhs; return *this; }
111 alpha16& operator>>=(int rhs) FL_NOEXCEPT { value >>= rhs; return *this; }
112 alpha16& operator<<=(int rhs) FL_NOEXCEPT { value <<= rhs; return *this; }
113 alpha16& operator++() FL_NOEXCEPT { ++value; return *this; }
114 alpha16 operator++(int) FL_NOEXCEPT { alpha16 t = *this; ++value; return t; }
115 alpha16& operator--() FL_NOEXCEPT { --value; return *this; }
116 alpha16 operator--(int) FL_NOEXCEPT { alpha16 t = *this; --value; return t; }
117
118 /// Scale a signed integer by this alpha (UNORM16 semantics).
119 /// result = (v * (raw + 1)) >> 16
120 /// Identity: scale_signed(x) == x when raw == 65535
121 /// Zero: scale_signed(x) == 0 when raw == 0 and |x| < 65536
122 template <typename T>
123 constexpr T scale_signed(T v) const FL_NOEXCEPT {
124 return static_cast<T>(
125 (static_cast<long long>(v) *
126 static_cast<long long>(static_cast<unsigned int>(value) + 1u)) >> 16);
127 }
128
129 private:
130 static constexpr unsigned short _clamp(float f) FL_NOEXCEPT {
131 return static_cast<unsigned short>(
132 f <= 0.0f ? 0 : (f >= 1.0f ? 65535 : static_cast<unsigned short>(f * 65535.0f + 0.5f)));
133 }
134};
135
136} // namespace fl

Callers 5

alpha_identity<alpha16>Function · 0.85
computeWindowMethod · 0.85
from_floatMethod · 0.85
transformMethod · 0.85
blur.cppFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected