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

◆ setParity()

FASTLED_FORCE_INLINE void CRGB::setParity ( fl::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 510 of file crgb.h.

511 {
512 fl::u8 curparity = getParity();
513
514 if( parity == curparity) return;
515
516 if( parity ) {
517 // going 'up'
518 if( (b > 0) && (b < 255)) {
519 if( r == g && g == b) {
520 ++r;
521 ++g;
522 }
523 ++b;
524 } else if( (r > 0) && (r < 255)) {
525 ++r;
526 } else if( (g > 0) && (g < 255)) {
527 ++g;
528 } else {
529 if( r == g && g == b) {
530 r ^= 0x01;
531 g ^= 0x01;
532 }
533 b ^= 0x01;
534 }
535 } else {
536 // going 'down'
537 if( b > 1) {
538 if( r == g && g == b) {
539 --r;
540 --g;
541 }
542 --b;
543 } else if( g > 1) {
544 --g;
545 } else if( r > 1) {
546 --r;
547 } else {
548 if( r == g && g == b) {
549 r ^= 0x01;
550 g ^= 0x01;
551 }
552 b ^= 0x01;
553 }
554 }
555 }
unsigned char u8
Definition int.h:17
FASTLED_FORCE_INLINE fl::u8 getParity()
Returns 0 or 1, depending on the lowest bit of the sum of the color components.
Definition crgb.h:482

References FASTLED_FORCE_INLINE, and getParity().

+ Here is the call graph for this function: