FastLED 3.9.15
Loading...
Searching...
No Matches

◆ setParity()

FASTLED_FORCE_INLINE void fl::CRGB::setParity ( u8 parity)
inline

Adjusts the color in the smallest way possible so that the parity of the coloris now the desired value.

This allows you to "hide" one bit of information in the color.

Ideally, we find one color channel which already has data in it, and modify just that channel by one. We don't want to light up a channel that's black if we can avoid it, and if the pixel is 'grayscale', (meaning that R==G==B), we modify all three channels at once, to preserve the neutral hue.

There's no such thing as a free lunch; in many cases this "hidden bit" may actually be visible, but this code makes reasonable efforts to hide it as much as is reasonably possible.

Also, an effort is made to make it such that repeatedly setting the parity to different values will not cause the color to "drift". Toggling the parity twice should generally result in the original color again.

Definition at line 453 of file crgb.h.

454 {
455 u8 curparity = getParity();
456
457 if( parity == curparity) return;
458
459 if( parity ) {
460 // going 'up'
461 if( (b > 0) && (b < 255)) {
462 if( r == g && g == b) {
463 ++r;
464 ++g;
465 }
466 ++b;
467 } else if( (r > 0) && (r < 255)) {
468 ++r;
469 } else if( (g > 0) && (g < 255)) {
470 ++g;
471 } else {
472 if( r == g && g == b) {
473 r ^= 0x01;
474 g ^= 0x01;
475 }
476 b ^= 0x01;
477 }
478 } else {
479 // going 'down'
480 if( b > 1) {
481 if( r == g && g == b) {
482 --r;
483 --g;
484 }
485 --b;
486 } else if( g > 1) {
487 --g;
488 } else if( r > 1) {
489 --r;
490 } else {
491 if( r == g && g == b) {
492 r ^= 0x01;
493 g ^= 0x01;
494 }
495 b ^= 0x01;
496 }
497 }
498 }
unsigned char u8
Definition stdint.h:131
FASTLED_FORCE_INLINE u8 getParity() FL_NOEXCEPT
Returns 0 or 1, depending on the lowest bit of the sum of the color components.
Definition crgb.h:425

References FASTLED_FORCE_INLINE, FL_NOEXCEPT, and getParity().

+ Here is the call graph for this function: