FastLED 3.9.15
Loading...
Searching...
No Matches
crgb.cpp
Go to the documentation of this file.
1
3
4#define FASTLED_INTERNAL
5#include "crgb.h"
6#include "FastLED.h"
7#include "fl/xymap.h"
8
9#include "fl/upscale.h"
10#include "fl/downscale.h"
11#include "lib8tion/math8.h"
12
13#include "fl/namespace.h"
14#include "fl/int.h"
15
17
19 fl::string out;
20 out.append("CRGB(");
21 out.append(int16_t(r));
22 out.append(",");
23 out.append(int16_t(g));
24 out.append(",");
25 out.append(int16_t(b));
26 out.append(")");
27 return out;
28}
29
30CRGB CRGB::computeAdjustment(uint8_t scale, const CRGB &colorCorrection,
31 const CRGB &colorTemperature) {
32#if defined(NO_CORRECTION) && (NO_CORRECTION == 1)
33 return CRGB(scale, scale, scale);
34#else
35 CRGB adj(0, 0, 0);
36 if (scale > 0) {
37 for (uint8_t i = 0; i < 3; ++i) {
38 uint8_t cc = colorCorrection.raw[i];
39 uint8_t ct = colorTemperature.raw[i];
40 if (cc > 0 && ct > 0) {
41 // Optimized for AVR size. This function is only called very
42 // infrequently so size matters more than speed.
43 fl::u32 work = (((fl::u16)cc) + 1);
44 work *= (((fl::u16)ct) + 1);
45 work *= scale;
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) {
56 return CRGB(blend8(p1.r, p2.r, amountOfP2), blend8(p1.g, p2.g, amountOfP2),
57 blend8(p1.b, p2.b, amountOfP2));
58}
59
60CRGB CRGB::blendAlphaMaxChannel(const CRGB &upper, const CRGB &lower) {
61 // Use luma of upper pixel as alpha (0..255)
62 uint8_t 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 uint8_t 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) {
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) {
83 "Upscaling only works with a src matrix that is rectangular");
84 fl::u16 w = srcXY.getWidth();
85 fl::u16 h = srcXY.getHeight();
86 fl::upscale(src, dst, w, h, dstXY);
87}
88
89CRGB &CRGB::nscale8(uint8_t scaledown) {
90 nscale8x3(r, g, b, scaledown);
91 return *this;
92}
93
96 r = qadd8(r, rhs.r);
97 g = qadd8(g, rhs.g);
98 b = qadd8(b, rhs.b);
99 return *this;
100}
101
102CRGB CRGB::lerp8(const CRGB &other, fract8 amountOf2) const {
103 CRGB ret;
104
105 ret.r = lerp8by8(r, other.r, amountOf2);
106 ret.g = lerp8by8(g, other.g, amountOf2);
107 ret.b = lerp8by8(b, other.b, amountOf2);
108
109 return ret;
110}
111
112CRGB &CRGB::fadeToBlackBy(uint8_t fadefactor) {
113 nscale8x3(r, g, b, 255 - fadefactor);
114 return *this;
115}
116
central include file for FastLED, defines the CFastLED class/object
uint16_t scale
Definition Noise.ino:74
XyMapType getType() const
Definition xymap.cpp:128
u16 getWidth() const
Definition xymap.cpp:122
@ kLineByLine
Definition xymap.h:47
u16 getHeight() const
Definition xymap.cpp:124
string & append(const BitsetFixed< N > &bs)
Definition str.h:675
Defines the red, green, and blue (RGB) pixel struct.
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:293
LIB8STATIC_ALWAYS_INLINE uint8_t qadd8(uint8_t i, uint8_t j)
Add one byte to another, saturating at 0xFF.
Definition math8.h:40
LIB8STATIC uint8_t blend8(uint8_t a, uint8_t b, uint8_t amountOfB)
Blend a variable proportion (0-255) of one byte to another.
Definition math8.h:683
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:367
Fast, efficient 8-bit math functions specifically designed for high-performance LED programming.
#define FASTLED_NAMESPACE_END
Definition namespace.h:23
#define FASTLED_NAMESPACE_BEGIN
Definition namespace.h:22
Implements the FastLED namespace macros.
void downscale(const CRGB *src, const XYMap &srcXY, CRGB *dst, const XYMap &dstXY)
u8 fract8
Fixed-Point Fractional Types.
Definition int.h:49
void upscale(const CRGB *input, CRGB *output, u16 inputWidth, u16 inputHeight, const fl::XYMap &xyMap)
Definition upscale.h:58
static void downscale(const CRGB *src, const fl::XYMap &srcXY, CRGB *dst, const fl::XYMap &dstXY)
Downscale an CRGB matrix (or strip) to the smaller size.
Definition crgb.cpp:74
CRGB & nscale8(fl::u8 scaledown)
Scale down a RGB to N/256ths of its current brightness, using "plain math" dimming rules.
FASTLED_FORCE_INLINE CRGB()
Default constructor.
Definition crgb.h:158
CRGB & operator+=(const CRGB &rhs)
Add one CRGB to another, saturating at 0xFF for each channel.
Definition crgb.cpp:95
fl::string toString() const
Definition crgb.cpp:18
static CRGB blend(const CRGB &p1, const CRGB &p2, fract8 amountOfP2)
Definition crgb.cpp:55
CRGB lerp8(const CRGB &other, fract8 amountOf2) const
Return a new CRGB object after performing a linear interpolation between this object and the passed i...
Definition crgb.cpp:102
CRGB & fadeToBlackBy(fl::u8 fadefactor)
fadeToBlackBy is a synonym for nscale8(), as a fade instead of a scale
static void upscale(const CRGB *src, const fl::XYMap &srcXY, CRGB *dst, const fl::XYMap &dstXY)
Definition crgb.cpp:79
static CRGB blendAlphaMaxChannel(const CRGB &upper, const CRGB &lower)
Definition crgb.cpp:60
static CRGB computeAdjustment(fl::u8 scale, const CRGB &colorCorrection, const CRGB &colorTemperature)
Calculates the combined color adjustment to the LEDs at a given scale, color correction,...
Representation of an RGB pixel (Red, Green, Blue)
Definition crgb.h:86
#define FASTLED_WARN_IF
Definition warn.h:8