FastLED 3.9.7
Loading...
Searching...
No Matches
rgbw.cpp
Go to the documentation of this file.
1
3
4#include <stdint.h>
5
6#define FASTLED_INTERNAL
7#include "FastLED.h"
8
9#include "rgbw.h"
10
11
13
14namespace {
15inline uint8_t min3(uint8_t a, uint8_t b, uint8_t c) {
16 if (a < b) {
17 if (a < c) {
18 return a;
19 } else {
20 return c;
21 }
22 } else {
23 if (b < c) {
24 return b;
25 } else {
26 return c;
27 }
28 }
29}
30
31inline uint8_t divide_by_3(uint8_t x) {
32 uint16_t y = (uint16_t(x) * 85) >> 8;
33 return static_cast<uint8_t>(y);
34}
35} // namespace
36
37// @brief Converts RGB to RGBW using a color transfer method
38// from color channels to 3x white.
39// @author Jonathanese
40void rgb_2_rgbw_exact(uint16_t w_color_temperature, uint8_t r, uint8_t g,
41 uint8_t b, uint8_t r_scale, uint8_t g_scale,
42 uint8_t b_scale, uint8_t *out_r, uint8_t *out_g,
43 uint8_t *out_b, uint8_t *out_w) {
44 (void)w_color_temperature;
45 r = scale8(r, r_scale);
46 g = scale8(g, g_scale);
47 b = scale8(b, b_scale);
48 uint8_t min_component = min3(r, g, b);
49 *out_r = r - min_component;
50 *out_g = g - min_component;
51 *out_b = b - min_component;
52 *out_w = min_component;
53}
54
55void rgb_2_rgbw_max_brightness(uint16_t w_color_temperature, uint8_t r,
56 uint8_t g, uint8_t b, uint8_t r_scale,
57 uint8_t g_scale, uint8_t b_scale, uint8_t *out_r,
58 uint8_t *out_g, uint8_t *out_b, uint8_t *out_w) {
59 (void)w_color_temperature;
60 *out_r = scale8(r, r_scale);
61 *out_g = scale8(g, g_scale);
62 *out_b = scale8(b, b_scale);
63 *out_w = min3(r, g, b);
64}
65
66void rgb_2_rgbw_null_white_pixel(uint16_t w_color_temperature, uint8_t r,
67 uint8_t g, uint8_t b, uint8_t r_scale,
68 uint8_t g_scale, uint8_t b_scale,
69 uint8_t *out_r, uint8_t *out_g, uint8_t *out_b,
70 uint8_t *out_w) {
71 (void)w_color_temperature;
72 *out_r = scale8(r, r_scale);
73 *out_g = scale8(g, g_scale);
74 *out_b = scale8(b, b_scale);
75 *out_w = 0;
76}
77
78void rgb_2_rgbw_white_boosted(uint16_t w_color_temperature, uint8_t r,
79 uint8_t g, uint8_t b, uint8_t r_scale,
80 uint8_t g_scale, uint8_t b_scale, uint8_t *out_r,
81 uint8_t *out_g, uint8_t *out_b, uint8_t *out_w) {
82 (void)w_color_temperature;
83 r = scale8(r, r_scale);
84 g = scale8(g, g_scale);
85 b = scale8(b, b_scale);
86 uint8_t min_component = min3(r, g, b);
87 uint8_t w;
88 bool is_min = true;
89 if (min_component <= 84) {
90 w = 3 * min_component;
91 } else {
92 w = 255;
93 is_min = false;
94 }
95 uint8_t r_prime;
96 uint8_t g_prime;
97 uint8_t b_prime;
98 if (is_min) {
99 r_prime = r - min_component;
100 g_prime = g - min_component;
101 b_prime = b - min_component;
102 } else {
103 uint8_t w3 = divide_by_3(w);
104 r_prime = r - w3;
105 g_prime = g - w3;
106 b_prime = b - w3;
107 }
108
109 *out_r = r_prime;
110 *out_g = g_prime;
111 *out_b = b_prime;
112 *out_w = w;
113}
114
115rgb_2_rgbw_function g_user_function = rgb_2_rgbw_exact;
116
117void set_rgb_2_rgbw_function(rgb_2_rgbw_function func) {
118 if (func == nullptr) {
119 g_user_function = rgb_2_rgbw_exact;
120 return;
121 }
122 g_user_function = func;
123}
124
125void rgb_2_rgbw_user_function(uint16_t w_color_temperature, uint8_t r,
126 uint8_t g, uint8_t b, uint8_t r_scale,
127 uint8_t g_scale, uint8_t b_scale, uint8_t *out_r,
128 uint8_t *out_g, uint8_t *out_b, uint8_t *out_w) {
129 g_user_function(w_color_temperature, r, g, b, r_scale, g_scale, b_scale,
130 out_r, out_g, out_b, out_w);
131}
132
133void rgbw_partial_reorder(EOrderW w_placement, uint8_t b0, uint8_t b1,
134 uint8_t b2, uint8_t w, uint8_t *out_b0,
135 uint8_t *out_b1, uint8_t *out_b2, uint8_t *out_b3) {
136
137 uint8_t out[4] = {b0, b1, b2, 0};
138 switch (w_placement) {
139 // unrolled loop for speed.
140 case W3:
141 out[3] = w;
142 break;
143 case W2:
144 out[3] = out[2]; // memmove and copy.
145 out[2] = w;
146 break;
147 case W1:
148 out[3] = out[2];
149 out[2] = out[1];
150 out[1] = w;
151 break;
152 case W0:
153 out[3] = out[2];
154 out[2] = out[1];
155 out[1] = out[0];
156 out[0] = w;
157 break;
158 }
159 *out_b0 = out[0];
160 *out_b1 = out[1];
161 *out_b2 = out[2];
162 *out_b3 = out[3];
163}
164
central include file for FastLED, defines the CFastLED class/object
EOrderW
Definition eorder.h:24
@ W3
White is fourth.
Definition eorder.h:25
@ W1
White is second.
Definition eorder.h:27
@ W0
White is first.
Definition eorder.h:28
@ W2
White is third.
Definition eorder.h:26
LIB8STATIC_ALWAYS_INLINE uint8_t scale8(uint8_t i, fract8 scale)
Scale one byte by a second one, which is treated as the numerator of a fraction whose denominator is ...
Definition scale8.h:34
#define FASTLED_NAMESPACE_END
End of the FastLED namespace.
Definition namespace.h:16
#define FASTLED_NAMESPACE_BEGIN
Start of the FastLED namespace.
Definition namespace.h:14
void rgb_2_rgbw_null_white_pixel(uint16_t w_color_temperature, uint8_t r, uint8_t g, uint8_t b, uint8_t r_scale, uint8_t g_scale, uint8_t b_scale, uint8_t *out_r, uint8_t *out_g, uint8_t *out_b, uint8_t *out_w)
Converts RGB to RGBW with the W channel set to black, always.
Definition rgbw.cpp:66
void rgb_2_rgbw_max_brightness(uint16_t w_color_temperature, uint8_t r, uint8_t g, uint8_t b, uint8_t r_scale, uint8_t g_scale, uint8_t b_scale, uint8_t *out_r, uint8_t *out_g, uint8_t *out_b, uint8_t *out_w)
The minimum brigthness of the RGB channels is used to set the W channel.
Definition rgbw.cpp:55
void rgb_2_rgbw_white_boosted(uint16_t w_color_temperature, uint8_t r, uint8_t g, uint8_t b, uint8_t r_scale, uint8_t g_scale, uint8_t b_scale, uint8_t *out_r, uint8_t *out_g, uint8_t *out_b, uint8_t *out_w)
Converts RGB to RGBW with a boosted white channel.
Definition rgbw.cpp:78
void rgb_2_rgbw_exact(uint16_t w_color_temperature, uint8_t r, uint8_t g, uint8_t b, uint8_t r_scale, uint8_t g_scale, uint8_t b_scale, uint8_t *out_r, uint8_t *out_g, uint8_t *out_b, uint8_t *out_w)
Converts RGB to RGBW using a color transfer method from saturated color channels to white.
Definition rgbw.cpp:40
Functions for red, green, blue, white (RGBW) output.