FastLED 3.9.8
Loading...
Searching...
No Matches
crgb.hpp
Go to the documentation of this file.
1
3
4#pragma once
5
6#include <stdint.h>
7#include "chsv.h"
8#include "crgb.h"
9#include "lib8tion.h"
10#include "fl/namespace.h"
11#include "fl/force_inline.h"
12#include "fl/str.h"
13
14#if FASTLED_IS_USING_NAMESPACE
15#define FUNCTION_SCALE8(a,b) FASTLED_NAMESPACE::scale8(a,b)
16#else
17#define FUNCTION_SCALE8(a,b) ::scale8(a,b)
18#endif
19
21
23FASTLED_FORCE_INLINE CRGB& CRGB::operator+= (const CRGB& rhs )
24{
25 r = qadd8( r, rhs.r);
26 g = qadd8( g, rhs.g);
27 b = qadd8( b, rhs.b);
28 return *this;
29}
30
31FASTLED_FORCE_INLINE CRGB& CRGB::addToRGB (uint8_t d )
32{
33 r = qadd8( r, d);
34 g = qadd8( g, d);
35 b = qadd8( b, d);
36 return *this;
37}
38
39FASTLED_FORCE_INLINE CRGB& CRGB::operator-= (const CRGB& rhs )
40{
41 r = qsub8( r, rhs.r);
42 g = qsub8( g, rhs.g);
43 b = qsub8( b, rhs.b);
44 return *this;
45}
46
48FASTLED_FORCE_INLINE CRGB& CRGB::operator++ ()
49{
50 addToRGB(1);
51 return *this;
52}
53
55FASTLED_FORCE_INLINE CRGB CRGB::operator++ (int )
56{
57 CRGB retval(*this);
58 ++(*this);
59 return retval;
60}
61
62FASTLED_FORCE_INLINE CRGB& CRGB::subtractFromRGB(uint8_t d)
63{
64 r = qsub8( r, d);
65 g = qsub8( g, d);
66 b = qsub8( b, d);
67 return *this;
68}
69
70FASTLED_FORCE_INLINE CRGB& CRGB::operator*= (uint8_t d )
71{
72 r = qmul8( r, d);
73 g = qmul8( g, d);
74 b = qmul8( b, d);
75 return *this;
76}
77
78FASTLED_FORCE_INLINE CRGB& CRGB::nscale8_video(uint8_t scaledown )
79{
80 nscale8x3_video( r, g, b, scaledown);
81 return *this;
82}
83
84FASTLED_FORCE_INLINE CRGB& CRGB::operator%= (uint8_t scaledown )
85{
86 nscale8x3_video( r, g, b, scaledown);
87 return *this;
88}
89
90FASTLED_FORCE_INLINE CRGB& CRGB::fadeLightBy (uint8_t fadefactor )
91{
92 nscale8x3_video( r, g, b, 255 - fadefactor);
93 return *this;
94}
95
97FASTLED_FORCE_INLINE CRGB& CRGB::operator-- ()
98{
100 return *this;
101}
102
104FASTLED_FORCE_INLINE CRGB CRGB::operator-- (int )
105{
106 CRGB retval(*this);
107 --(*this);
108 return retval;
109}
110
111FASTLED_FORCE_INLINE CRGB& CRGB::nscale8 (uint8_t scaledown )
112{
113 nscale8x3( r, g, b, scaledown);
114 return *this;
115}
116
117constexpr CRGB CRGB::nscale8_constexpr(const CRGB scaledown) const
118{
119 return CRGB(
120 scale8_constexpr(r, scaledown.r),
121 scale8_constexpr(g, scaledown.g),
122 scale8_constexpr(b, scaledown.b)
123 );
124}
125
126
127FASTLED_FORCE_INLINE CRGB& CRGB::nscale8 (const CRGB & scaledown )
128{
129 r = FUNCTION_SCALE8(r, scaledown.r);
130 g = FUNCTION_SCALE8(g, scaledown.g);
131 b = FUNCTION_SCALE8(b, scaledown.b);
132 return *this;
133}
134
135FASTLED_FORCE_INLINE CRGB CRGB::scale8 (uint8_t scaledown ) const
136{
137 CRGB out = *this;
138 nscale8x3( out.r, out.g, out.b, scaledown);
139 return out;
140}
141
142FASTLED_FORCE_INLINE CRGB CRGB::scale8 (const CRGB & scaledown ) const
143{
144 CRGB out;
145 out.r = FUNCTION_SCALE8(r, scaledown.r);
146 out.g = FUNCTION_SCALE8(g, scaledown.g);
147 out.b = FUNCTION_SCALE8(b, scaledown.b);
148 return out;
149}
150
151inline CRGB& CRGB::fadeToBlackBy (uint8_t fadefactor )
152{
153 nscale8x3( r, g, b, 255 - fadefactor);
154 return *this;
155}
156
157FASTLED_FORCE_INLINE uint8_t CRGB::getLuma( ) const {
158 //Y' = 0.2126 R' + 0.7152 G' + 0.0722 B'
159 // 54 183 18 (!)
160
161 uint8_t luma = scale8_LEAVING_R1_DIRTY( r, 54) + \
162 scale8_LEAVING_R1_DIRTY( g, 183) + \
163 scale8_LEAVING_R1_DIRTY( b, 18);
164 cleanup_R1();
165 return luma;
166}
167
168FASTLED_FORCE_INLINE uint8_t CRGB::getAverageLight( ) const {
169#if FASTLED_SCALE8_FIXED == 1
170 const uint8_t eightyfive = 85;
171#else
172 const uint8_t eightyfive = 86;
173#endif
174 uint8_t avg = scale8_LEAVING_R1_DIRTY( r, eightyfive) + \
175 scale8_LEAVING_R1_DIRTY( g, eightyfive) + \
176 scale8_LEAVING_R1_DIRTY( b, eightyfive);
177 cleanup_R1();
178 return avg;
179}
180
181FASTLED_FORCE_INLINE CRGB CRGB::lerp8( const CRGB& other, fract8 frac) const
182{
183 CRGB ret;
184
185 ret.r = lerp8by8(r,other.r,frac);
186 ret.g = lerp8by8(g,other.g,frac);
187 ret.b = lerp8by8(b,other.b,frac);
188
189 return ret;
190}
191
192FASTLED_FORCE_INLINE CRGB CRGB::lerp16( const CRGB& other, fract16 frac) const
193{
194 CRGB ret;
195
196 ret.r = lerp16by16(r<<8,other.r<<8,frac)>>8;
197 ret.g = lerp16by16(g<<8,other.g<<8,frac)>>8;
198 ret.b = lerp16by16(b<<8,other.b<<8,frac)>>8;
199
200 return ret;
201}
202
203
205FASTLED_FORCE_INLINE CRGB operator+( const CRGB& p1, const CRGB& p2)
206{
207 return CRGB( qadd8( p1.r, p2.r),
208 qadd8( p1.g, p2.g),
209 qadd8( p1.b, p2.b));
210}
211
213FASTLED_FORCE_INLINE CRGB operator-( const CRGB& p1, const CRGB& p2)
214{
215 return CRGB( qsub8( p1.r, p2.r),
216 qsub8( p1.g, p2.g),
217 qsub8( p1.b, p2.b));
218}
219
221FASTLED_FORCE_INLINE CRGB operator*( const CRGB& p1, uint8_t d)
222{
223 return CRGB( qmul8( p1.r, d),
224 qmul8( p1.g, d),
225 qmul8( p1.b, d));
226}
227
229FASTLED_FORCE_INLINE CRGB operator%( const CRGB& p1, uint8_t d)
230{
231 CRGB retval( p1);
232 retval.nscale8_video( d);
233 return retval;
234}
235
237
238#undef FUNCTION_SCALE8
Defines the hue, saturation, and value (HSV) pixel struct.
Defines the red, green, and blue (RGB) pixel struct.
uint8_t fract8
ANSI: unsigned short _Fract.
Definition types.h:36
uint16_t fract16
ANSI: unsigned _Fract.
Definition types.h:46
LIB8STATIC uint8_t lerp8by8(uint8_t a, uint8_t b, fract8 frac)
Linear interpolation between two unsigned 8-bit values, with 8-bit fraction.
Definition lib8tion.h:455
LIB8STATIC uint16_t lerp16by16(uint16_t a, uint16_t b, fract16 frac)
Linear interpolation between two unsigned 16-bit values, with 16-bit fraction.
Definition lib8tion.h:472
LIB8STATIC_ALWAYS_INLINE uint8_t qadd8(uint8_t i, uint8_t j)
Add one byte to another, saturating at 0xFF.
Definition math8.h:31
LIB8STATIC_ALWAYS_INLINE uint8_t qmul8(uint8_t i, uint8_t j)
8x8 bit multiplication with 8-bit result, saturating at 0xFF.
Definition math8.h:470
LIB8STATIC_ALWAYS_INLINE uint8_t qsub8(uint8_t i, uint8_t j)
Subtract one byte from another, saturating at 0x00.
Definition math8.h:103
FASTLED_FORCE_INLINE CRGB operator%(const CRGB &p1, uint8_t d)
Scale using CRGB::nscale8_video()
Definition crgb.hpp:229
FASTLED_FORCE_INLINE CRGB operator-(const CRGB &p1, const CRGB &p2)
Subtract one CRGB from another, saturating at 0x00 for each channel.
Definition crgb.hpp:213
FASTLED_FORCE_INLINE CRGB operator*(const CRGB &p1, uint8_t d)
Multiply each of the channels by a constant, saturating each channel at 0xFF.
Definition crgb.hpp:221
FASTLED_FORCE_INLINE CRGB operator+(const CRGB &p1, const CRGB &p2)
Add one CRGB to another, saturating at 0xFF for each channel.
Definition crgb.hpp:205
LIB8STATIC_ALWAYS_INLINE void cleanup_R1()
Clean up the r1 register after a series of *LEAVING_R1_DIRTY calls.
Definition scale8.h:333
LIB8STATIC_ALWAYS_INLINE uint8_t scale8_LEAVING_R1_DIRTY(uint8_t i, fract8 scale)
This version of scale8() does not clean up the R1 register on AVR.
Definition scale8.h:170
LIB8STATIC void nscale8x3(uint8_t &r, uint8_t &g, uint8_t &b, fract8 scale)
Scale three one-byte values by a fourth one, which is treated as the numerator of a fraction whose de...
Definition scale8.h:357
LIB8STATIC void nscale8x3_video(uint8_t &r, uint8_t &g, uint8_t &b, fract8 scale)
Scale three one-byte values by a fourth one, which is treated as the numerator of a fraction whose de...
Definition scale8.h:391
Fast, efficient 8-bit math functions specifically designed for high-performance LED programming.
Implements the FastLED namespace macros.
#define FASTLED_NAMESPACE_END
End of the FastLED namespace.
Definition namespace.h:16
#define FASTLED_NAMESPACE_BEGIN
Start of the FastLED namespace.
Definition namespace.h:14
Representation of an RGB pixel (Red, Green, Blue)
Definition crgb.h:54
FASTLED_FORCE_INLINE CRGB & nscale8_video(uint8_t scaledown)
Scale down a RGB to N/256ths of it's current brightness using "video" dimming rules.
Definition crgb.hpp:78
FASTLED_FORCE_INLINE CRGB()=default
Default constructor.
FASTLED_FORCE_INLINE uint8_t getLuma() const
Get the "luma" of a CRGB object.
Definition crgb.hpp:157
uint8_t r
Red channel value.
Definition crgb.h:58
FASTLED_FORCE_INLINE CRGB & fadeLightBy(uint8_t fadefactor)
fadeLightBy is a synonym for nscale8_video(), as a fade instead of a scale
Definition crgb.hpp:90
FASTLED_FORCE_INLINE CRGB & operator++()
Add a constant of '1' from each channel, saturating at 0xFF.
Definition crgb.hpp:48
FASTLED_FORCE_INLINE CRGB & nscale8(uint8_t scaledown)
Scale down a RGB to N/256ths of its current brightness, using "plain math" dimming rules.
Definition crgb.hpp:111
FASTLED_FORCE_INLINE CRGB lerp16(const CRGB &other, fract16 frac) const
Return a new CRGB object after performing a linear interpolation between this object and the passed i...
Definition crgb.hpp:192
FASTLED_FORCE_INLINE CRGB & operator-=(const CRGB &rhs)
Subtract one CRGB from another, saturating at 0x00 for each channel.
Definition crgb.hpp:39
FASTLED_FORCE_INLINE CRGB & operator--()
Subtract a constant of '1' from each channel, saturating at 0x00.
Definition crgb.hpp:97
FASTLED_FORCE_INLINE CRGB lerp8(const CRGB &other, fract8 frac) const
Return a new CRGB object after performing a linear interpolation between this object and the passed i...
Definition crgb.hpp:181
uint8_t g
Green channel value.
Definition crgb.h:62
FASTLED_FORCE_INLINE CRGB & operator+=(const CRGB &rhs)
Add one CRGB to another, saturating at 0xFF for each channel.
Definition crgb.hpp:23
FASTLED_FORCE_INLINE uint8_t getAverageLight() const
Get the average of the R, G, and B values.
Definition crgb.hpp:168
FASTLED_FORCE_INLINE CRGB & addToRGB(uint8_t d)
Add a constant to each channel, saturating at 0xFF.
Definition crgb.hpp:31
FASTLED_FORCE_INLINE CRGB & subtractFromRGB(uint8_t d)
Subtract a constant from each channel, saturating at 0x00.
Definition crgb.hpp:62
uint8_t b
Blue channel value.
Definition crgb.h:66
FASTLED_FORCE_INLINE CRGB scale8(uint8_t scaledown) const
Return a CRGB object that is a scaled down version of this object.
Definition crgb.hpp:135
FASTLED_FORCE_INLINE CRGB & operator*=(uint8_t d)
Multiply each of the channels by a constant, saturating each channel at 0xFF.
Definition crgb.hpp:70
FASTLED_FORCE_INLINE CRGB & operator%=(uint8_t scaledown)
%= is a synonym for nscale8_video().
Definition crgb.hpp:84
FASTLED_FORCE_INLINE CRGB & fadeToBlackBy(uint8_t fadefactor)
fadeToBlackBy is a synonym for nscale8(), as a fade instead of a scale
Definition crgb.hpp:151