FastLED 3.7.8
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
11FASTLED_NAMESPACE_BEGIN
12
14FASTLED_FORCE_INLINE CRGB& CRGB::operator+= (const CRGB& rhs )
15{
16 r = qadd8( r, rhs.r);
17 g = qadd8( g, rhs.g);
18 b = qadd8( b, rhs.b);
19 return *this;
20}
21
22FASTLED_FORCE_INLINE CRGB& CRGB::addToRGB (uint8_t d )
23{
24 r = qadd8( r, d);
25 g = qadd8( g, d);
26 b = qadd8( b, d);
27 return *this;
28}
29
30FASTLED_FORCE_INLINE CRGB& CRGB::operator-= (const CRGB& rhs )
31{
32 r = qsub8( r, rhs.r);
33 g = qsub8( g, rhs.g);
34 b = qsub8( b, rhs.b);
35 return *this;
36}
37
38FASTLED_FORCE_INLINE CRGB& CRGB::subtractFromRGB(uint8_t d)
39{
40 r = qsub8( r, d);
41 g = qsub8( g, d);
42 b = qsub8( b, d);
43 return *this;
44}
45
46FASTLED_FORCE_INLINE CRGB& CRGB::operator*= (uint8_t d )
47{
48 r = qmul8( r, d);
49 g = qmul8( g, d);
50 b = qmul8( b, d);
51 return *this;
52}
53
54FASTLED_FORCE_INLINE CRGB& CRGB::nscale8_video(uint8_t scaledown )
55{
56 nscale8x3_video( r, g, b, scaledown);
57 return *this;
58}
59
60FASTLED_FORCE_INLINE CRGB& CRGB::operator%= (uint8_t scaledown )
61{
62 nscale8x3_video( r, g, b, scaledown);
63 return *this;
64}
65
66FASTLED_FORCE_INLINE CRGB& CRGB::fadeLightBy (uint8_t fadefactor )
67{
68 nscale8x3_video( r, g, b, 255 - fadefactor);
69 return *this;
70}
71
72FASTLED_FORCE_INLINE CRGB& CRGB::nscale8 (uint8_t scaledown )
73{
74 nscale8x3( r, g, b, scaledown);
75 return *this;
76}
77
78FASTLED_FORCE_INLINE CRGB& CRGB::nscale8 (const CRGB & scaledown )
79{
80 r = ::scale8(r, scaledown.r);
81 g = ::scale8(g, scaledown.g);
82 b = ::scale8(b, scaledown.b);
83 return *this;
84}
85
86FASTLED_FORCE_INLINE CRGB CRGB::scale8 (uint8_t scaledown ) const
87{
88 CRGB out = *this;
89 nscale8x3( out.r, out.g, out.b, scaledown);
90 return out;
91}
92
93FASTLED_FORCE_INLINE CRGB CRGB::scale8 (const CRGB & scaledown ) const
94{
95 CRGB out;
96 out.r = ::scale8(r, scaledown.r);
97 out.g = ::scale8(g, scaledown.g);
98 out.b = ::scale8(b, scaledown.b);
99 return out;
100}
101
102inline CRGB& CRGB::fadeToBlackBy (uint8_t fadefactor )
103{
104 nscale8x3( r, g, b, 255 - fadefactor);
105 return *this;
106}
107
108FASTLED_FORCE_INLINE uint8_t CRGB::getLuma( ) const {
109 //Y' = 0.2126 R' + 0.7152 G' + 0.0722 B'
110 // 54 183 18 (!)
111
112 uint8_t luma = scale8_LEAVING_R1_DIRTY( r, 54) + \
113 scale8_LEAVING_R1_DIRTY( g, 183) + \
114 scale8_LEAVING_R1_DIRTY( b, 18);
115 cleanup_R1();
116 return luma;
117}
118
119FASTLED_FORCE_INLINE uint8_t CRGB::getAverageLight( ) const {
120#if FASTLED_SCALE8_FIXED == 1
121 const uint8_t eightyfive = 85;
122#else
123 const uint8_t eightyfive = 86;
124#endif
125 uint8_t avg = scale8_LEAVING_R1_DIRTY( r, eightyfive) + \
126 scale8_LEAVING_R1_DIRTY( g, eightyfive) + \
127 scale8_LEAVING_R1_DIRTY( b, eightyfive);
128 cleanup_R1();
129 return avg;
130}
131
132FASTLED_FORCE_INLINE CRGB CRGB::lerp8( const CRGB& other, fract8 frac) const
133{
134 CRGB ret;
135
136 ret.r = lerp8by8(r,other.r,frac);
137 ret.g = lerp8by8(g,other.g,frac);
138 ret.b = lerp8by8(b,other.b,frac);
139
140 return ret;
141}
142
143FASTLED_FORCE_INLINE CRGB CRGB::lerp16( const CRGB& other, fract16 frac) const
144{
145 CRGB ret;
146
147 ret.r = lerp16by16(r<<8,other.r<<8,frac)>>8;
148 ret.g = lerp16by16(g<<8,other.g<<8,frac)>>8;
149 ret.b = lerp16by16(b<<8,other.b<<8,frac)>>8;
150
151 return ret;
152}
153
154
156FASTLED_FORCE_INLINE CRGB operator+( const CRGB& p1, const CRGB& p2)
157{
158 return CRGB( qadd8( p1.r, p2.r),
159 qadd8( p1.g, p2.g),
160 qadd8( p1.b, p2.b));
161}
162
164FASTLED_FORCE_INLINE CRGB operator-( const CRGB& p1, const CRGB& p2)
165{
166 return CRGB( qsub8( p1.r, p2.r),
167 qsub8( p1.g, p2.g),
168 qsub8( p1.b, p2.b));
169}
170
172FASTLED_FORCE_INLINE CRGB operator*( const CRGB& p1, uint8_t d)
173{
174 return CRGB( qmul8( p1.r, d),
175 qmul8( p1.g, d),
176 qmul8( p1.b, d));
177}
178
180FASTLED_FORCE_INLINE CRGB operator%( const CRGB& p1, uint8_t d)
181{
182 CRGB retval( p1);
183 retval.nscale8_video( d);
184 return retval;
185}
186
187FASTLED_NAMESPACE_END
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:461
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:478
LIB8STATIC_ALWAYS_INLINE uint8_t qadd8(uint8_t i, uint8_t j)
Add one byte to another, saturating at 0xFF.
Definition math8.h:27
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:466
LIB8STATIC_ALWAYS_INLINE uint8_t qsub8(uint8_t i, uint8_t j)
Subtract one byte from another, saturating at 0x00.
Definition math8.h:99
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:54
FASTLED_FORCE_INLINE uint8_t getLuma() const
Get the "luma" of a CRGB object.
Definition crgb.hpp:108
uint8_t r
Red channel value.
Definition crgb.h:29
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:66
FASTLED_FORCE_INLINE CRGB operator%(const CRGB &p1, uint8_t d)
Scale using CRGB::nscale8_video()
Definition crgb.hpp:180
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:72
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:143
FASTLED_FORCE_INLINE CRGB & operator-=(const CRGB &rhs)
Subtract one CRGB from another, saturating at 0x00 for each channel.
Definition crgb.hpp:30
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:164
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:132
uint8_t g
Green channel value.
Definition crgb.h:33
FASTLED_FORCE_INLINE CRGB & operator+=(const CRGB &rhs)
Add one CRGB to another, saturating at 0xFF for each channel.
Definition crgb.hpp:14
FASTLED_FORCE_INLINE uint8_t getAverageLight() const
Get the average of the R, G, and B values.
Definition crgb.hpp:119
FASTLED_FORCE_INLINE CRGB & addToRGB(uint8_t d)
Add a constant to each channel, saturating at 0xFF.
Definition crgb.hpp:22
FASTLED_FORCE_INLINE CRGB & subtractFromRGB(uint8_t d)
Subtract a constant from each channel, saturating at 0x00.
Definition crgb.hpp:38
uint8_t b
Blue channel value.
Definition crgb.h:37
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:86
FASTLED_FORCE_INLINE CRGB & operator*=(uint8_t d)
Multiply each of the channels by a constant, saturating each channel at 0xFF.
Definition crgb.hpp:46
FASTLED_FORCE_INLINE CRGB & operator%=(uint8_t scaledown)
%= is a synonym for nscale8_video().
Definition crgb.hpp:60
FASTLED_FORCE_INLINE CRGB & fadeToBlackBy(uint8_t fadefactor)
fadeToBlackBy is a synonym for nscale8(), as a fade instead of a scale
Definition crgb.hpp:102
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:172
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:156
LIB8STATIC_ALWAYS_INLINE void cleanup_R1()
Clean up the r1 register after a series of *LEAVING_R1_DIRTY calls.
Definition scale8.h:324
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:161
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:343
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:377
Fast, efficient 8-bit math functions specifically designed for high-performance LED programming.
Representation of an RGB pixel (Red, Green, Blue)
Definition crgb.h:25