Skip to content

CHSV compare (== and !=) converts to CRGB before comparision #691

Closed

Description

@jeremyste

(I'm on version 3.002.000 so forgive me if this has been addressed in a later version)

I noticed that if I use a compare on two HSV values (== or !=), FastLED converts them to CRGB at runtime. For example, this will always print true:

#include <FastLED.h>

CHSV color1 = CHSV(0,0,0);
CHSV color2 = CHSV(128,0,0);

void setup() {
  Serial.begin(115200);

  if (color1 == color2){  //both colors convert to CRGB {0, 0, 0} at runtime
    Serial.println("true");  //will always print "true"
  } else {
    Serial.println("false");  //what I was expecting
  }
}

I was expecting a comparison based on the values in memory. Also, doesn't a conversion cost more cycles than a direct comparison?

This discrepancy caused some minor bugs in my project but my solution was to overload the operators to allow direct comparison of CHSV types:

bool operator==(CHSV a, CHSV b) {
	return (a.hue == b.hue && a.saturation == b.saturation && a.value == b.value);
}

bool operator!=(CHSV a, CHSV b) {
	return !(a == b);
}

Perhaps this is something that can be added to the library?

Activity

  1. kriegsman commented on Dec 7, 2018

    @kriegsman
    Member

    Thanks for spotting that -- definitely just an oversight and we should definitely add it.

  2. self-assigned this
    on Dec 7, 2018
  3. marmilicious commented on Dec 7, 2018

    @marmilicious
    Contributor

    But CHSV(0,0,0) and CHSV(128,0,0) would both display black, so in a way it is true, right? I guess that's sort of an edge case though.

  4. jeremyste commented on Dec 9, 2018

    @jeremyste
    Author

    @marmilicious Haha, that's why I wasn't sure if it was intended behavior to convert to RGB when comparing because technically it is true. I actually discovered this with values that map to not-black (CHSV(32, 120, 221) and CHSV(33, 119, 220)), but I picked an obvious and reproducible case.

    Out of curiosity, I wrote some really quick and dirty code to see how many collisions there are for a selected CHSV. I found that the closer to black a color is, the more likely there will be other CHSVs that equal it. Check this out:

    //finds and prints all cases where the selected CHSV == another CHSV
    #include <FastLED.h>
    
    const CHSV color1 = CHSV(42, 24, 31);	//pick a CHSV to check
    
    unsigned long totalSoFar = 0;
    
    void setup()
    {
    	Serial.begin(1000000);
    	for (int h2 = 0; h2 <= 255; h2++) {
    		for (int s2 = 0; s2 <= 255; s2++) {
    			for (int v2 = 0; v2 <= 255; v2++) {
    				//check for HSV == HSV
    				if (color1.h != h2 && color1.s != s2 && color1.v != v2) {
    					//check if there is an equality between HSV1 and HSV2 due to RGB conversion
    					CHSV color2 = CHSV(h2, s2, v2);
    					if (color1 == color2) {
    						totalSoFar++;
    						Serial.print("CHSV(");
    						Serial.print(color1.h);
    						Serial.print(",");
    						Serial.print(color1.s);
    						Serial.print(",");
    						Serial.print(color1.v);
    						Serial.print(") == CHSV(");
    						Serial.print(h2);
    						Serial.print(",");
    						Serial.print(s2);
    						Serial.print(",");
    						Serial.print(v2);
    						Serial.print(")   Total so far:");
    						Serial.println(totalSoFar);
    					}
    				}
    			}
    		}
    	}
    
    	Serial.print("Done!");
    }
    
    void loop(){}
  5. marmilicious commented on Dec 9, 2018

    @marmilicious
    Contributor

    @jeremyste Nice check!

  6. added
    area: core
    status: stale
    status: triage
    on Apr 16, 2026
  7. moved this from Triage to Done in FastLED Trackeron Aug 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area: corestatus: stalestatus: triage

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions