@@ -241,6 +241,73 @@ void WS2812::setPixel(uint16_t index, uint32_t pixel) {
241
241
this ->pixels [index].blue = (pixel & 0xff0000 ) >> 16 ;
242
242
} // setPixel
243
243
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
244
311
245
312
/* *
246
313
* @brief Clear all the pixel colors.
0 commit comments