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

◆ setParity()

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

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

References FASTLED_FORCE_INLINE, and getParity().

+ Here is the call graph for this function: