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 442 of file crgb.h.

443 {
444 uint8_t curparity = getParity();
445
446 if( parity == curparity) return;
447
448 if( parity ) {
449 // going 'up'
450 if( (b > 0) && (b < 255)) {
451 if( r == g && g == b) {
452 ++r;
453 ++g;
454 }
455 ++b;
456 } else if( (r > 0) && (r < 255)) {
457 ++r;
458 } else if( (g > 0) && (g < 255)) {
459 ++g;
460 } else {
461 if( r == g && g == b) {
462 r ^= 0x01;
463 g ^= 0x01;
464 }
465 b ^= 0x01;
466 }
467 } else {
468 // going 'down'
469 if( b > 1) {
470 if( r == g && g == b) {
471 --r;
472 --g;
473 }
474 --b;
475 } else if( g > 1) {
476 --g;
477 } else if( r > 1) {
478 --r;
479 } else {
480 if( r == g && g == b) {
481 r ^= 0x01;
482 g ^= 0x01;
483 }
484 b ^= 0x01;
485 }
486 }
487 }
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:414

References FASTLED_FORCE_INLINE, and getParity().

+ Here is the call graph for this function: