FastLED 3.10.8
Loading...
Searching...
No Matches
crgb.cpp.hpp
Go to the documentation of this file.
1
3
4#define FASTLED_INTERNAL
5#include "crgb.h"
6#include "fl/system/fastled.h"
7#include "fl/math/xymap.h"
8
9#include "fl/gfx/upscale.h"
10#include "fl/gfx/downscale.h"
11#include "fl/math/math8.h"
12
13#include "fl/stl/int.h"
14
15
16
18 fl::string out;
19 out.append("CRGB(");
20 out.append(fl::i16(r));
21 out.append(",");
22 out.append(fl::i16(g));
23 out.append(",");
24 out.append(fl::i16(b));
25 out.append(")");
26 return out;
27}
28
30 const CRGB &colorTemperature) FL_NO_EXCEPT {
31#if defined(NO_CORRECTION) && (NO_CORRECTION == 1)
32 return CRGB(scale, scale, scale);
33#else
34 CRGB adj(0, 0, 0);
35 if (scale > 0) {
36 for (fl::u8 i = 0; i < 3; ++i) {
37 fl::u8 cc = colorCorrection.raw[i];
38 fl::u8 ct = colorTemperature.raw[i];
39 if (cc > 0 && ct > 0) {
40 // Optimized for AVR size. This function is only called very
41 // infrequently so size matters more than speed.
42 // (cc + 1) * scale is at most 65280, so it fits in
43 // 16 bits. Reordering the exact product avoids a wide multiply.
44 const fl::u16 scaledCorrection = (fl::u16(cc) + 1U) * scale;
45 fl::u32 work = fl::u32(scaledCorrection) * (fl::u16(ct) + 1U);
46 work /= 0x10000L;
47 adj.raw[i] = work & 0xFF;
48 }
49 }
50 }
51 return adj;
52#endif
53}
54
55CRGB CRGB::blend(const CRGB &p1, const CRGB &p2, fract8 amountOfP2) FL_NO_EXCEPT {
56 return CRGB(fl::blend8(p1.r, p2.r, amountOfP2), fl::blend8(p1.g, p2.g, amountOfP2),
57 fl::blend8(p1.b, p2.b, amountOfP2));
58}
59
61 // Use luma of upper pixel as alpha (0..255)
62 fl::u8 max_component = 0;
63 for (int i = 0; i < 3; ++i) {
64 if (upper.raw[i] > max_component) {
65 max_component = upper.raw[i];
66 }
67 }
68 // uint8_t alpha = upper.getLuma();
69 // blend(lower, upper, alpha) → (lower * (255−alpha) + upper * alpha) / 256
70 fl::u8 amountOf2 = 255 - max_component;
71 return CRGB::blend(upper, lower, amountOf2);
72}
73
74void CRGB::downscale(const CRGB *src, const fl::XYMap &srcXY, CRGB *dst,
75 const fl::XYMap &dstXY) FL_NO_EXCEPT {
76 fl::downscale(src, srcXY, dst, dstXY);
77}
78
79void CRGB::upscale(const CRGB *src, const fl::XYMap &srcXY, CRGB *dst,
80 const fl::XYMap &dstXY) FL_NO_EXCEPT {
81 FL_WARN_IF(srcXY.getType() != fl::XYMap::kLineByLine, "Upscaling only works with a src matrix that is rectangular");
82 fl::u16 w = srcXY.getWidth();
83 fl::u16 h = srcXY.getHeight();
84 fl::upscale(src, dst, w, h, dstXY);
85}
86
88 nscale8x3(r, g, b, scaledown);
89 return *this;
90}
91
94 r = qadd8(r, rhs.r);
95 g = qadd8(g, rhs.g);
96 b = qadd8(b, rhs.b);
97 return *this;
98}
99
100CRGB CRGB::lerp8(const CRGB &other, fract8 amountOf2) const FL_NO_EXCEPT {
101 CRGB ret;
102
103 ret.r = lerp8by8(r, other.r, amountOf2);
104 ret.g = lerp8by8(g, other.g, amountOf2);
105 ret.b = lerp8by8(b, other.b, amountOf2);
106
107 return ret;
108}
109
111 nscale8x3(r, g, b, 255 - fadefactor);
112 return *this;
113}
114
fl::UISlider scale("Scale", 4,.1, 4,.1)
@ kLineByLine
Definition xymap.h:43
string & append(const bitset_fixed< N > &bs) FL_NO_EXCEPT
Definition string.h:293
Legacy header.
Legacy compatibility header for 8-bit math functions.
Internal FastLED header for implementation files.
LIB8STATIC fl::u8 lerp8by8(fl::u8 a, fl::u8 b, fract8 frac) FL_NO_EXCEPT
Linear interpolation between two unsigned 8-bit values, with 8-bit fraction.
Definition lib8tion.h:364
#define FL_WARN_IF(COND,...)
Definition log.h:438
unsigned char u8
Definition stdint.h:131
fl::CRGB CRGB
Definition video.h:18
u8 u8 fract8 blur_amount FL_NO_EXCEPT
Definition blur.h:186
void upscale(const CRGB *input, CRGB *output, u16 inputWidth, u16 inputHeight, const fl::XYMap &xyMap) FL_NO_EXCEPT
Definition upscale.h:57
u8 fract8
Fixed-Point Fractional Types.
Definition stdint.h:160
InputGamut g
Definition rgbw.h:125
void downscale(const CRGB *src, const XYMap &srcXY, CRGB *dst, const XYMap &dstXY) FL_NO_EXCEPT
static CRGB blendAlphaMaxChannel(const CRGB &upper, const CRGB &lower) FL_NO_EXCEPT
Definition crgb.cpp.hpp:60
CRGB & fadeToBlackBy(u8 fadefactor) FL_NO_EXCEPT
fadeToBlackBy is a synonym for nscale8(), as a fade instead of a scale
Definition crgb.cpp.hpp:110
CRGB lerp8(const CRGB &other, fract8 amountOf2) const FL_NO_EXCEPT
Return a new CRGB object after performing a linear interpolation between this object and the passed i...
Definition crgb.cpp.hpp:100
static CRGB blend(const CRGB &p1, const CRGB &p2, fract8 amountOfP2) FL_NO_EXCEPT
Definition crgb.cpp.hpp:55
CRGB & nscale8(u8 scaledown) FL_NO_EXCEPT
Scale down a RGB to N/256ths of its current brightness, using "plain math" dimming rules.
Definition crgb.cpp.hpp:87
FASTLED_FORCE_INLINE CRGB() FL_NO_EXCEPT
Default constructor.
Definition crgb.h:111
CRGB & operator+=(const CRGB &rhs) FL_NO_EXCEPT
Add one CRGB to another, saturating at 0xFF for each channel.
Definition crgb.cpp.hpp:93
static CRGB computeAdjustment(u8 scale, const CRGB &colorCorrection, const CRGB &colorTemperature) FL_NO_EXCEPT
Calculates the combined color adjustment to the LEDs at a given scale, color correction,...
Definition crgb.cpp.hpp:29
static void downscale(const CRGB *src, const XYMap &srcXY, CRGB *dst, const XYMap &dstXY) FL_NO_EXCEPT
Downscale a CRGB matrix (or strip) to the smaller size.
Definition crgb.cpp.hpp:74
string toString() const FL_NO_EXCEPT
Definition crgb.cpp.hpp:17
static void upscale(const CRGB *src, const XYMap &srcXY, CRGB *dst, const XYMap &dstXY) FL_NO_EXCEPT
Definition crgb.cpp.hpp:79