Same Colors not equal?
-
c1 = Sketchup;;Color.new(1,1,1) c2 = Sketchup;;Color.new(1,1,1) p c1, c2 p c1 == c2
And your result:
Color( 1, 1, 1, 255) Color( 1, 1, 1, 255) false
Seems like it ought to compare true.
-
There seems to be no [working] method for '==' for comparing Colors: but
c1.to_s==c2.to_s
does return true if they have the same rgb values... -
A color is a Sketchup::Color object. So what you've done there is just compare the two objects, which are different, regardless of what their actual color is. It would be nice if Google fixed that.
-
As well as comparing the Color turned into strings
c1.to_s==c2.to_s
to get true/false, you could also compare the rgb values
c1.red==c2.red c1.green==c2.green c1.blue==c2.blue
will all return true if the three color values are all equal...As Chris says the Colors are two different objects so they aren't equal so == returns false unless c1 and c2 refer to the same Color object !
-
@tig said:
There seems to be no [working] method for '==' for comparing Colors: but c1.to_s==c2.to_s does return true if they have the same rgb values...
c1.to_a == c2.to_a
works too. -
So there are lots of ways of checking if two Colors are equal, BUT NOT the obvious c1==c2 !
-
Does converting to an array work faster than to string I wonder? As an array, are the RGB values still left as integers? That seems like it would be a faster comparison.
-
Also
c1.to_i==c2.to_i
works, as the color is converted to a single integer format - that might be even quicker ?
-
@chris fullmer said:
A color is a Sketchup::Color object. So what you've done there is just compare the two objects, which are different, regardless of what their actual color is. It would be nice if Google fixed that.
Ah, of course.
The "Ruby way" would be to define the
<=>
operator for the Color class, and then mix in Comparable. -
Then again, what does it mean for a Color to be greater than another? Nevermind...
Equality is all that's needed. I think the .to_i comparison makes the most sense for clarity.
-
Yea, always avoid comparing strings.
Such a shame that they've not added a proper comparison method for all objects.
Advertisement