FastLED 3.9.3
Loading...
Searching...
No Matches
crgb.hpp
1
2#pragma once
3
4#include <stdint.h>
5#include "chsv.h"
6#include "crgb.h"
7#include "lib8tion.h"
8#include "namespace.h"
9#include "force_inline.h"
10
11#if FASTLED_IS_USING_NAMESPACE
12#define FUNCTION_SCALE8(a,b) FASTLED_NAMESPACE::scale8(a,b)
13#else
14#define FUNCTION_SCALE8(a,b) ::scale8(a,b)
15#endif
16
17FASTLED_NAMESPACE_BEGIN
18
20FASTLED_FORCE_INLINE CRGB& CRGB::operator+= (const CRGB& rhs )
21{
22 r = qadd8( r, rhs.r);
23 g = qadd8( g, rhs.g);
24 b = qadd8( b, rhs.b);
25 return *this;
26}
27
28FASTLED_FORCE_INLINE CRGB& CRGB::addToRGB (uint8_t d )
29{
30 r = qadd8( r, d);
31 g = qadd8( g, d);
32 b = qadd8( b, d);
33 return *this;
34}
35
36FASTLED_FORCE_INLINE CRGB& CRGB::operator-= (const CRGB& rhs )
37{
38 r = qsub8( r, rhs.r);
39 g = qsub8( g, rhs.g);
40 b = qsub8( b, rhs.b);
41 return *this;
42}
43
45FASTLED_FORCE_INLINE CRGB& CRGB::operator++ ()
46{
47 addToRGB(1);
48 return *this;
49}
50
52FASTLED_FORCE_INLINE CRGB CRGB::operator++ (int )
53{
54 CRGB retval(*this);
55 ++(*this);
56 return retval;
57}
58
59FASTLED_FORCE_INLINE CRGB& CRGB::subtractFromRGB(uint8_t d)
60{
61 r = qsub8( r, d);
62 g = qsub8( g, d);
63 b = qsub8( b, d);
64 return *this;
65}
66
67FASTLED_FORCE_INLINE CRGB& CRGB::operator*= (uint8_t d )
68{
69 r = qmul8( r, d);
70 g = qmul8( g, d);
71 b = qmul8( b, d);
72 return *this;
73}
74
75FASTLED_FORCE_INLINE CRGB& CRGB::nscale8_video(uint8_t scaledown )
76{
77 nscale8x3_video( r, g, b, scaledown);
78 return *this;
79}
80
81FASTLED_FORCE_INLINE CRGB& CRGB::operator%= (uint8_t scaledown )
82{
83 nscale8x3_video( r, g, b, scaledown);
84 return *this;
85}
86
87FASTLED_FORCE_INLINE CRGB& CRGB::fadeLightBy (uint8_t fadefactor )
88{
89 nscale8x3_video( r, g, b, 255 - fadefactor);
90 return *this;
91}
92
94FASTLED_FORCE_INLINE CRGB& CRGB::operator-- ()
95{
97 return *this;
98}
99
101FASTLED_FORCE_INLINE CRGB CRGB::operator-- (int )
102{
103 CRGB retval(*this);
104 --(*this);
105 return retval;
106}
107
108FASTLED_FORCE_INLINE CRGB& CRGB::nscale8 (uint8_t scaledown )
109{
110 nscale8x3( r, g, b, scaledown);
111 return *this;
112}
113
114constexpr CRGB CRGB::nscale8_constexpr(const CRGB scaledown) const
115{
116 return CRGB(
117 scale8_constexpr(r, scaledown.r),
118 scale8_constexpr(g, scaledown.g),
119 scale8_constexpr(b, scaledown.b)
120 );
121}
122
123
124FASTLED_FORCE_INLINE CRGB& CRGB::nscale8 (const CRGB & scaledown )
125{
126 r = FUNCTION_SCALE8(r, scaledown.r);
127 g = FUNCTION_SCALE8(g, scaledown.g);
128 b = FUNCTION_SCALE8(b, scaledown.b);
129 return *this;
130}
131
132FASTLED_FORCE_INLINE CRGB CRGB::scale8 (uint8_t scaledown ) const
133{
134 CRGB out = *this;
135 nscale8x3( out.r, out.g, out.b, scaledown);
136 return out;
137}
138
139FASTLED_FORCE_INLINE CRGB CRGB::scale8 (const CRGB & scaledown ) const
140{
141 CRGB out;
142 out.r = FUNCTION_SCALE8(r, scaledown.r);
143 out.g = FUNCTION_SCALE8(g, scaledown.g);
144 out.b = FUNCTION_SCALE8(b, scaledown.b);
145 return out;
146}
147
148inline CRGB& CRGB::fadeToBlackBy (uint8_t fadefactor )
149{
150 nscale8x3( r, g, b, 255 - fadefactor);
151 return *this;
152}
153
154FASTLED_FORCE_INLINE uint8_t CRGB::getLuma( ) const {
155 //Y' = 0.2126 R' + 0.7152 G' + 0.0722 B'
156 // 54 183 18 (!)
157
158 uint8_t luma = scale8_LEAVING_R1_DIRTY( r, 54) + \
159 scale8_LEAVING_R1_DIRTY( g, 183) + \
160 scale8_LEAVING_R1_DIRTY( b, 18);
161 cleanup_R1();
162 return luma;
163}
164
165FASTLED_FORCE_INLINE uint8_t CRGB::getAverageLight( ) const {
166#if FASTLED_SCALE8_FIXED == 1
167 const uint8_t eightyfive = 85;
168#else
169 const uint8_t eightyfive = 86;
170#endif
171 uint8_t avg = scale8_LEAVING_R1_DIRTY( r, eightyfive) + \
172 scale8_LEAVING_R1_DIRTY( g, eightyfive) + \
173 scale8_LEAVING_R1_DIRTY( b, eightyfive);
174 cleanup_R1();
175 return avg;
176}
177
178FASTLED_FORCE_INLINE CRGB CRGB::lerp8( const CRGB& other, fract8 frac) const
179{
180 CRGB ret;
181
182 ret.r = lerp8by8(r,other.r,frac);
183 ret.g = lerp8by8(g,other.g,frac);
184 ret.b = lerp8by8(b,other.b,frac);
185
186 return ret;
187}
188
189FASTLED_FORCE_INLINE CRGB CRGB::lerp16( const CRGB& other, fract16 frac) const
190{
191 CRGB ret;
192
193 ret.r = lerp16by16(r<<8,other.r<<8,frac)>>8;
194 ret.g = lerp16by16(g<<8,other.g<<8,frac)>>8;
195 ret.b = lerp16by16(b<<8,other.b<<8,frac)>>8;
196
197 return ret;
198}
199
200
202FASTLED_FORCE_INLINE CRGB operator+( const CRGB& p1, const CRGB& p2)
203{
204 return CRGB( qadd8( p1.r, p2.r),
205 qadd8( p1.g, p2.g),
206 qadd8( p1.b, p2.b));
207}
208
210FASTLED_FORCE_INLINE CRGB operator-( const CRGB& p1, const CRGB& p2)
211{
212 return CRGB( qsub8( p1.r, p2.r),
213 qsub8( p1.g, p2.g),
214 qsub8( p1.b, p2.b));
215}
216
218FASTLED_FORCE_INLINE CRGB operator*( const CRGB& p1, uint8_t d)
219{
220 return CRGB( qmul8( p1.r, d),
221 qmul8( p1.g, d),
222 qmul8( p1.b, d));
223}
224
226FASTLED_FORCE_INLINE CRGB operator%( const CRGB& p1, uint8_t d)
227{
228 CRGB retval( p1);
229 retval.nscale8_video( d);
230 return retval;
231}
232
233FASTLED_NAMESPACE_END
234
235#undef FUNCTION_SCALE8
uint8_t fract8
ANSI: unsigned short _Fract.
Definition types.h:30
uint16_t fract16
ANSI: unsigned _Fract.
Definition types.h:40
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 & nscale8_video(uint8_t scaledown)
Scale down a RGB to N/256ths of it's current brightness using "video" dimming rules.
Definition crgb.hpp:75
FASTLED_FORCE_INLINE CRGB()=default
Default constructor.
FASTLED_FORCE_INLINE uint8_t getLuma() const
Get the "luma" of a CRGB object.
Definition crgb.hpp:154
uint8_t r
Red channel value.
Definition crgb.h:43
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:87
FASTLED_FORCE_INLINE CRGB operator%(const CRGB &p1, uint8_t d)
Scale using CRGB::nscale8_video()
Definition crgb.hpp:226
FASTLED_FORCE_INLINE CRGB & operator++()
Add a constant of '1' from each channel, saturating at 0xFF.
Definition crgb.hpp:45
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:108
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:189
FASTLED_FORCE_INLINE CRGB & operator-=(const CRGB &rhs)
Subtract one CRGB from another, saturating at 0x00 for each channel.
Definition crgb.hpp:36
FASTLED_FORCE_INLINE CRGB & operator--()
Subtract a constant of '1' from each channel, saturating at 0x00.
Definition crgb.hpp:94
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:210
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:178
uint8_t g
Green channel value.
Definition crgb.h:47
FASTLED_FORCE_INLINE CRGB & operator+=(const CRGB &rhs)
Add one CRGB to another, saturating at 0xFF for each channel.
Definition crgb.hpp:20
FASTLED_FORCE_INLINE uint8_t getAverageLight() const
Get the average of the R, G, and B values.
Definition crgb.hpp:165
FASTLED_FORCE_INLINE CRGB & addToRGB(uint8_t d)
Add a constant to each channel, saturating at 0xFF.
Definition crgb.hpp:28
FASTLED_FORCE_INLINE CRGB & subtractFromRGB(uint8_t d)
Subtract a constant from each channel, saturating at 0x00.
Definition crgb.hpp:59
uint8_t b
Blue channel value.
Definition crgb.h:51
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:132
FASTLED_FORCE_INLINE CRGB & operator*=(uint8_t d)
Multiply each of the channels by a constant, saturating each channel at 0xFF.
Definition crgb.hpp:67
FASTLED_FORCE_INLINE CRGB & operator%=(uint8_t scaledown)
%= is a synonym for nscale8_video().
Definition crgb.hpp:81
FASTLED_FORCE_INLINE CRGB & fadeToBlackBy(uint8_t fadefactor)
fadeToBlackBy is a synonym for nscale8(), as a fade instead of a scale
Definition crgb.hpp:148
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:218
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:202
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.
Representation of an RGB pixel (Red, Green, Blue)
Definition crgb.h:39