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
10#include "fl/downscale.h"
11#include "lib8tion/math8.h"
12
13#include "fl/namespace.h"
14
16
18 fl::Str out;
19 out.append("CRGB(");
20 out.append(int16_t(r));
21 out.append(",");
22 out.append(int16_t(g));
23 out.append(",");
24 out.append(int16_t(b));
25 out.append(")");
26 return out;
27}
28
29CRGB CRGB::computeAdjustment(uint8_t scale, const CRGB &colorCorrection,
30 const CRGB &colorTemperature) {
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 (uint8_t i = 0; i < 3; ++i) {
37 uint8_t cc = colorCorrection.raw[i];
38 uint8_t 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 uint32_t work = (((uint16_t)cc) + 1);
43 work *= (((uint16_t)ct) + 1);
44 work *= scale;
45 work /= 0x10000L;
46 adj.raw[i] = work & 0xFF;
47 }
48 }
49 }
50 return adj;
51#endif
52}
53
54CRGB CRGB::blend(const CRGB &p1, const CRGB &p2, fract8 amountOfP2) {
55 return CRGB(blend8(p1.r, p2.r, amountOfP2), blend8(p1.g, p2.g, amountOfP2),
56 blend8(p1.b, p2.b, amountOfP2));
57}
58
59CRGB CRGB::blendAlphaMaxChannel(const CRGB &upper, const CRGB &lower) {
60 // Use luma of upper pixel as alpha (0..255)
61 uint8_t max_component = 0;
62 for (int i = 0; i < 3; ++i) {
63 if (upper.raw[i] > max_component) {
64 max_component = upper.raw[i];
65 }
66 }
67 // uint8_t alpha = upper.getLuma();
68 // blend(lower, upper, alpha) → (lower * (255−alpha) + upper * alpha) / 256
69 uint8_t amountOf2 = 255 - max_component;
70 return CRGB::blend(upper, lower, amountOf2);
71}
72
73void CRGB::downscale(const CRGB *src, const fl::XYMap &srcXY, CRGB *dst,
74 const fl::XYMap &dstXY) {
75 fl::downscale(src, srcXY, dst, dstXY);
76}
77
78void CRGB::upscale(const CRGB *src, const fl::XYMap &srcXY, CRGB *dst,
79 const fl::XYMap &dstXY) {
82 "Upscaling only works with a src matrix that is rectangular");
83 uint16_t w = srcXY.getWidth();
84 uint16_t h = srcXY.getHeight();
85 fl::bilinearExpand(src, dst, w, h, dstXY);
86}
87
88CRGB &CRGB::nscale8(uint8_t scaledown) {
89 nscale8x3(r, g, b, scaledown);
90 return *this;
91}
92
95 r = qadd8(r, rhs.r);
96 g = qadd8(g, rhs.g);
97 b = qadd8(b, rhs.b);
98 return *this;
99}
100
101CRGB CRGB::lerp8(const CRGB &other, fract8 amountOf2) const {
102 CRGB ret;
103
104 ret.r = lerp8by8(r, other.r, amountOf2);
105 ret.g = lerp8by8(g, other.g, amountOf2);
106 ret.b = lerp8by8(b, other.b, amountOf2);
107
108 return ret;
109}
110
111CRGB &CRGB::fadeToBlackBy(uint8_t fadefactor) {
112 nscale8x3(r, g, b, 255 - fadefactor);
113 return *this;
114}
115
UISlider scale("Scale", 1.0f, 0.0f, 1.0f, 0.01f)
central include file for FastLED, defines the CFastLED class/object
Demonstrates how to mix noise generation with color palettes on a 2D LED matrix.
Str & append(const T &val)
Definition str.h:439
Definition str.h:388
XyMapType getType() const
Definition xymap.cpp:130
uint16_t getWidth() const
Definition xymap.cpp:124
@ kLineByLine
Definition xymap.h:45
uint16_t getHeight() const
Definition xymap.cpp:126
Defines the red, green, and blue (RGB) pixel struct.
uint8_t fract8
ANSI: unsigned short _Fract.
Definition types.h:36
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:437
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 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:667
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:363
Fast, efficient 8-bit math functions specifically designed for high-performance LED programming.
#define FASTLED_NAMESPACE_END
Definition namespace.h:23
Implements the FastLED namespace macros.
void downscale(const CRGB *src, const XYMap &srcXY, CRGB *dst, const XYMap &dstXY)
void bilinearExpand(const CRGB *input, CRGB *output, uint16_t inputWidth, uint16_t inputHeight, fl::XYMap xyMap)
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:73
FASTLED_FORCE_INLINE CRGB()=default
Default constructor.
CRGB & operator+=(const CRGB &rhs)
Add one CRGB to another, saturating at 0xFF for each channel.
Definition crgb.cpp:94
CRGB & nscale8(uint8_t scaledown)
Scale down a RGB to N/256ths of its current brightness, using "plain math" dimming rules.
Definition crgb.cpp:88
static CRGB blend(const CRGB &p1, const CRGB &p2, fract8 amountOfP2)
Definition crgb.cpp:54
static CRGB computeAdjustment(uint8_t scale, const CRGB &colorCorrection, const CRGB &colorTemperature)
Calculates the combined color adjustment to the LEDs at a given scale, color correction,...
Definition crgb.cpp:29
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:101
static void upscale(const CRGB *src, const fl::XYMap &srcXY, CRGB *dst, const fl::XYMap &dstXY)
Definition crgb.cpp:78
fl::Str toString() const
Definition crgb.cpp:17
static CRGB blendAlphaMaxChannel(const CRGB &upper, const CRGB &lower)
Definition crgb.cpp:59
CRGB & fadeToBlackBy(uint8_t fadefactor)
fadeToBlackBy is a synonym for nscale8(), as a fade instead of a scale
Definition crgb.cpp:111
#define FASTLED_WARN_IF
Definition warn.h:8