Puzzled - color matching True/False

I must be overlooking something stupid here… I’m trying to see if an object color matches a predefined RGB color. My test - create a new blank file with one object in it which has color Rhino “Green” - RGB (0,255,0). Then run the following:

import rhinoscriptsyntax as rs
import System
objs=rs.NormalObjects()
color=rs.ObjectColor(objs[0])
print color==System.Drawing.Color.FromArgb(255,0,255,0)

By my way of thinking, the statement should be True, but it isn’t…
I can individually check R, G and B, but it seems like there should be something simpler that I’ve missed, or something I’m doing wrong here…

Thanks, --Mitch

1 Like

somehow color returns lime
and System.drawing.color.fromargb returns Color [A=0, R=255, G=0, B=0]

so it can never match

one is a ARGB color other is a string.

is it possible to say like rs.ObjectColor(objs[0]) convert to Argb? and then compare?
if I use ToArgb I get some weird number.

This works somehow:

import rhinoscriptsyntax as rs
import System
objs=rs.NormalObjects()
color=rs.ObjectColor(objs[0])
colorConvert=System.Drawing.Color.FromArgb(color.A,color.R,color.G,color.B)
print colorConvert == System.Drawing.Color.FromArgb(255,0,255,0)

This also works, but just seems bizarre:

import rhinoscriptsyntax as rs
import System
objs=rs.NormalObjects()
color=rs.ObjectColor(objs[0])
print color.ToArgb()==System.Drawing.Color.FromArgb(255,0,255,0).ToArgb()

That is the integer number equivalent of the color, so in principle it should serve as a unique identifier.

The thing is, that a System.Drawing.Color object has a property Value - which should be this number, I think - it shows up in the debugger, but I can’t seem to get at that property in the usual way.

1 Like

I think it looks more neat to create a function that does this:

colorConvert=System.Drawing.Color.FromArgb(color.A,color.R,color.G,color.B)

:grimacing:

That’s weird … I had a chance to test the script on Rhino 5 SR7 and there it worked …

On RS 10 no way …
Even Color.Equals() fails …
Only Color.ToArgb() seems to work, just as you said.

Maybe a different version of System.Drawing.Color ? …

Yeah, I tried that too…

Maybe things are getting adapted for Mac and something broke? Hmm… @stevebaer Any ideas?

–Mitch

not sure if python changes this at all but for pure .net:

for system.drawing.color comparison uses not only the A,R,G,B values but other states of the color struct to compare (named color for one) so for example
Color.Black!=Color.FromARGB(0,0,0)

so even though both of those are ‘black’ and share same ARGB values they are not equal color structs.(one is ‘named’ black) so will return false when testing for equality. the …ToARGB method for testing to see if simply same ‘color’ for lack of better word I’ve found is reliable when dealing with the .net system.drawing.color

Thanks for the info, Chris !

… Actually I suspected that there might have been some Microsoft logic behind that … :wink:

1 Like

this is how microsoft implemented System.Drawing.Color.
see