Another question from the Arduino Forum: There are many ways to set a color like leds[i]=0x000000 or leds[i]=CRGB::Black
It seems to be difficult to test a color: if (ledsi] == 0x000000) doesn’t work: “ambiguous overload for ‘operator==’ (operand types are ‘CRGB’ and ‘int’)”
The same for if (leds[i] == CRGB::Black)
What works is if (leds[i] == CRGB(CRGB::Black))
Why?
Because C++.
0x112233 is just an integer.
CRGB::Purple is also just an integer.
There’s also a constructor (and an assignment operator) for CRGB that takes an integer, which is why you can use an integer like 0x112233 or CRGB::Purple any place where a CRGB is needed. That’s why you can say leds[i] = 0x112233.
However, there are a few cases where this make it ambiguous what you mean, and you hit one of them: the compiler isn’t sure whether you mean to compare your color to the integer 0, or whether you meant to construct a CRGB from that integer, and compare that. And of course the compiler has no idea that those might “mean” the same thing, so you get an error.
And that’s also why putting the explicit cast works: you’ve removed the ambiguity.
As a side note, if you want to compare and see if a pixel is pure black, just use it as a Boolean:
if( led[i] ) { /* it’s lit / }
else { / it’s black */ }
Thanks for the explanation, Mark!
So how would I test for any given CRGB color? Something like
if (leds[i] == CRGB(0x112233)) ?