FastLED 3.10.8
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 460 of file crgb.h.

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

References FASTLED_FORCE_INLINE, fl::FL_NO_EXCEPT, fl::g, and getParity().

+ Here is the call graph for this function: