8000 setHSBPixel · programmer131/esp32-snippets@59cf797 · GitHub
[go: up one dir, main page]

Skip to content

Commit 59cf797

Browse files
committed
setHSBPixel
1 parent 1e58455 commit 59cf797

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

cpp_utils/WS2812.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,73 @@ void WS2812::setPixel(uint16_t index, uint32_t pixel) {
241241
this->pixels[index].blue = (pixel & 0xff0000) >> 16;
242242
} // setPixel
243243

244+
/**
245+
* @brief Set the given pixel to the specified HSB color.
246+
*
247+
* The LEDs are not actually updated until a call to show().
248+
*
249+
* @param [in] index The pixel that is to have its color set.
250+
* @param [in] hue The amount of hue in the pixel (0-360).
251+
* @param [in] green The amount of saturation in the pixel (0-255).
252+
* @param [in] blue The amount of brightnes in the pixel (0-255).
253+
*/
254+
void WS2812::setHSBPixel(uint16_t index, uint16_t hue, uint8_t saturation, uint8_t brightnes) {
255+
double sat_red;
256+
double sat_green;
257+
double sat_blue;
258+
double ctmp_red;
259+
double ctmp_green;
260+
double ctmp_blue;
261+
double new_red;
262+
double new_green;
263+
double new_blue;
264+
double dSaturation=(double)saturation/255;
265+
double dBrightnes=(double)brightnes/255;
266+
267+
assert(index < pixelCount);
268+
269+
if (hue < 120) {
270+
sat_red = (120 - hue) / 60.0;
271+
sat_green = hue / 60.0;
272+
sat_blue = 0;
273+
} else if (hue < 240) {
274+
sat_red = 0;
275+
sat_green = (240 - hue) / 60.0;
276+
sat_blue = (hue - 120) / 60.0;
277+
} else {
278+
sat_red = (hue - 240) / 60.0;
279+
sat_green = 0;
280+
sat_blue = (360 - hue) / 60.0;
281+
}
282+
283+
if (sat_red>1.0) {
284+
sat_red=1.0;
285+
}
286+
if (sat_green>1.0) {
287+
sat_green=1.0;
288+
}
289+
if (sat_blue>1.0) {
290+
sat_blue=1.0;
291+
}
292+
293+
ctmp_red = 2 * dSaturation * sat_red + (1 - dSaturation);
294+
ctmp_green = 2 * dSaturation * sat_green + (1 - dSaturation);
295+
ctmp_blue = 2 * dSaturation * sat_blue + (1 - dSaturation);
296+
297+
if (dBrightnes < 0.5) {
298+
new_red = dBrightnes * ctmp_red;
299+
new_green = dBrightnes * ctmp_green;
300+
new_blue = dBrightnes * ctmp_blue;
301+
} else {
302+
new_red = (1 - dBrightnes) * ctmp_red + 2 * dBrightnes - 1;
303+
new_green = (1 - dBrightnes) * ctmp_green + 2 * dBrightnes - 1;
304+
new_blue = (1 - dBrightnes) * ctmp_blue + 2 * dBrightnes - 1;
305+
}
306+
307+
this->pixels[index].red = (uint8_t)(new_red*255);
308+
this->pixels[index].green = (uint8_t)(new_green*255);
309+
this->pixels[index].blue = (uint8_t)(new_blue*255);
310+
} // setHSBPixel
244311

245312
/**
246313
* @brief Clear all the pixel colors.

cpp_utils/WS2812.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class WS2812 {
4545
void setPixel(uint16_t index, uint8_t red, uint8_t green, uint8_t blue);
4646
void setPixel(uint16_t index, pixel_t pixel);
4747
void setPixel(uint16_t index, uint32_t pixel);
48+
void setHSBPixel(uint16_t index, uint16_t hue, uint8_t saturation, uint8_t brightnes);
4849
void clear();
4950
virtual ~WS2812();
5051
private:

0 commit comments

Comments
 (0)
0