FastLED 3.9.13
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
111
112constexpr CRGB CRGB::nscale8_constexpr(const CRGB scaledown) const
113{
114 return CRGB(
115 scale8_constexpr(r, scaledown.r),
116 scale8_constexpr(g, scaledown.g),
117 scale8_constexpr(b, scaledown.b)
118 );
119}
120
121
122FASTLED_FORCE_INLINE CRGB& CRGB::nscale8 (const CRGB & scaledown )
123{
124 r = FUNCTION_SCALE8(r, scaledown.r);
125 g = FUNCTION_SCALE8(g, scaledown.g);
126 b = FUNCTION_SCALE8(b, scaledown.b);
127 return *this;
128}
129
130FASTLED_FORCE_INLINE CRGB CRGB::scale8 (uint8_t scaledown ) const
131{
132 CRGB out = *this;
133 nscale8x3( out.r, out.g, out.b, scaledown);
134 return out;
135}
136
137FASTLED_FORCE_INLINE CRGB CRGB::scale8 (const CRGB & scaledown ) const
138{
139 CRGB out;
140 out.r = FUNCTION_SCALE8(r, scaledown.r);
141 out.g = FUNCTION_SCALE8(g, scaledown.g);
142 out.b = FUNCTION_SCALE8(b, scaledown.b);
143 return out;
144}
145
146inline CRGB& CRGB::fadeToBlackBy (uint8_t fadefactor )
147{
148 nscale8x3( r, g, b, 255 - fadefactor);
149 return *this;
150}
151
152FASTLED_FORCE_INLINE uint8_t CRGB::getLuma( ) const {
153 //Y' = 0.2126 R' + 0.7152 G' + 0.0722 B'
154 // 54 183 18 (!)
155
156 uint8_t luma = scale8_LEAVING_R1_DIRTY( r, 54) + \
157 scale8_LEAVING_R1_DIRTY( g, 183) + \
158 scale8_LEAVING_R1_DIRTY( b, 18);
159 cleanup_R1();
160 return luma;
161}
162
163FASTLED_FORCE_INLINE uint8_t CRGB::getAverageLight( ) const {
164#if FASTLED_SCALE8_FIXED == 1
165 const uint8_t eightyfive = 85;
166#else
167 const uint8_t eightyfive = 86;
168#endif
169 uint8_t avg = scale8_LEAVING_R1_DIRTY( r, eightyfive) + \
170 scale8_LEAVING_R1_DIRTY( g, eightyfive) + \
171 scale8_LEAVING_R1_DIRTY( b, eightyfive);
172 cleanup_R1();
173 return avg;
174}
175
176FASTLED_FORCE_INLINE CRGB CRGB::lerp8( const CRGB& other, fract8 frac) const
177{
178 CRGB ret;
179
180 ret.r = lerp8by8(r,other.r,frac);
181 ret.g = lerp8by8(g,other.g,frac);
182 ret.b = lerp8by8(b,other.b,frac);
183
184 return ret;
185}
186
187FASTLED_FORCE_INLINE CRGB CRGB::lerp16( const CRGB& other, fract16 frac) const
188{
189 CRGB ret;
190
191 ret.r = lerp16by16(r<<8,other.r<<8,frac)>>8;
192 ret.g = lerp16by16(g<<8,other.g<<8,frac)>>8;
193 ret.b = lerp16by16(b<<8,other.b<<8,frac)>>8;
194
195 return ret;
196}
197
198
200FASTLED_FORCE_INLINE CRGB operator+( const CRGB& p1, const CRGB& p2)
201{
202 return CRGB( qadd8( p1.r, p2.r),
203 qadd8( p1.g, p2.g),
204 qadd8( p1.b, p2.b));
205}
206
208FASTLED_FORCE_INLINE CRGB operator-( const CRGB& p1, const CRGB& p2)
209{
210 return CRGB( qsub8( p1.r, p2.r),
211 qsub8( p1.g, p2.g),
212 qsub8( p1.b, p2.b));
213}
214
216FASTLED_FORCE_INLINE CRGB operator*( const CRGB& p1, uint8_t d)
217{
218 return CRGB( qmul8( p1.r, d),
219 qmul8( p1.g, d),
220 qmul8( p1.b, d));
221}
222
224FASTLED_FORCE_INLINE CRGB operator%( const CRGB& p1, uint8_t d)
225{
226 CRGB retval( p1);
227 retval.nscale8_video( d);
228 return retval;
229}
230
232
233#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:224
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:208
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:216
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:200
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:152
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 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:187
FASTLED_FORCE_INLINE CRGB & operator-=(const CRGB &rhs)
Subtract one CRGB from another, saturating at 0x00 for each channel.
Definition crgb.hpp:39
CRGB & nscale8(uint8_t scaledown)
Scale down a RGB to N/256ths of its current brightness, using "plain math" dimming rules.
Definition crgb.cpp:58
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:176
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:163
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:130
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:146