FastLED 3.9.15
Loading...
Searching...
No Matches
alpha.h
Go to the documentation of this file.
1#pragma once
2
5
7#include "fl/stl/noexcept.h"
8
9namespace fl {
10
11namespace alpha_detail {
12
13// True when T is a non-bool integer type (signed or unsigned, any width).
14template <typename T>
20
21// SFINAE helper — drop into a template parameter list:
22// template <typename IntT, alpha_detail::enable_if_integer_t<IntT> = 0>
23template <typename T>
26
27} // namespace alpha_detail
28
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
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
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
typename fl::enable_if< is_non_bool_integer< T >::value, int >::type enable_if_integer_t
Definition alpha.h:24
Base definition for an LED controller.
Definition crgb.hpp:179
#define FL_NOEXCEPT
constexpr float to_float() const FL_NOEXCEPT
Definition alpha.h:101
constexpr alpha16(IntT v) FL_NOEXCEPT
Definition alpha.h:94
alpha16 & operator*=(unsigned short rhs) FL_NOEXCEPT
Definition alpha.h:109
alpha16 & operator++() FL_NOEXCEPT
Definition alpha.h:113
alpha16 & operator+=(unsigned short rhs) FL_NOEXCEPT
Definition alpha.h:107
constexpr T scale_signed(T v) const FL_NOEXCEPT
Scale a signed integer by this alpha (UNORM16 semantics).
Definition alpha.h:123
alpha16 operator--(int) FL_NOEXCEPT
Definition alpha.h:116
constexpr unsigned short raw() const FL_NOEXCEPT
Definition alpha.h:100
alpha16 operator++(int) FL_NOEXCEPT
Definition alpha.h:114
alpha16 & operator>>=(int rhs) FL_NOEXCEPT
Definition alpha.h:111
constexpr alpha16(double f) FL_NOEXCEPT
Definition alpha.h:97
constexpr alpha16(float f) FL_NOEXCEPT
Definition alpha.h:96
constexpr alpha16() FL_NOEXCEPT
Definition alpha.h:90
alpha16 & operator--() FL_NOEXCEPT
Definition alpha.h:115
alpha16 & operator-=(unsigned short rhs) FL_NOEXCEPT
Definition alpha.h:108
unsigned short value
Definition alpha.h:88
static constexpr alpha16 from_float(float f) FL_NOEXCEPT
Definition alpha.h:103
alpha16 & operator<<=(int rhs) FL_NOEXCEPT
Definition alpha.h:112
alpha16 & operator/=(unsigned short rhs) FL_NOEXCEPT
Definition alpha.h:110
static constexpr unsigned short _clamp(float f) FL_NOEXCEPT
Definition alpha.h:130
unsigned char value
Definition alpha.h:43
constexpr unsigned char raw() const FL_NOEXCEPT
Definition alpha.h:55
constexpr alpha8(double f) FL_NOEXCEPT
Definition alpha.h:52
alpha8 & operator--() FL_NOEXCEPT
Definition alpha.h:70
alpha8 operator++(int) FL_NOEXCEPT
Definition alpha.h:69
constexpr float to_float() const FL_NOEXCEPT
Definition alpha.h:56
static constexpr alpha8 from_float(float f) FL_NOEXCEPT
Definition alpha.h:58
constexpr alpha8(float f) FL_NOEXCEPT
Definition alpha.h:51
alpha8 & operator>>=(int rhs) FL_NOEXCEPT
Definition alpha.h:66
alpha8 & operator++() FL_NOEXCEPT
Definition alpha.h:68
alpha8 & operator<<=(int rhs) FL_NOEXCEPT
Definition alpha.h:67
alpha8 & operator+=(unsigned char rhs) FL_NOEXCEPT
Definition alpha.h:62
alpha8 & operator*=(unsigned char rhs) FL_NOEXCEPT
Definition alpha.h:64
static constexpr unsigned char _clamp(float f) FL_NOEXCEPT
Definition alpha.h:74
alpha8 & operator-=(unsigned char rhs) FL_NOEXCEPT
Definition alpha.h:63
constexpr alpha8(IntT v) FL_NOEXCEPT
Definition alpha.h:49
alpha8 & operator/=(unsigned char rhs) FL_NOEXCEPT
Definition alpha.h:65
alpha8 operator--(int) FL_NOEXCEPT
Definition alpha.h:71
constexpr alpha8() FL_NOEXCEPT
Definition alpha.h:45
static constexpr bool value
Definition alpha.h:16