Comparing Colors

F

fripper

I have a simple VB 2005 app that has a bitmap image and at one point I want
to look at a particular pixel and see if it has a color value equal to the
value of a color variable, c. For example:

dim c as color

c = color.red

If bitmap(x,y) = c then
 
F

fripper

That did it ... thanks a lot. I don't understand why

If color1 = color2 then ...

doesn't work.
 
A

Armin Zingler

fripper said:
I have a simple VB 2005 app that has a bitmap image and at one point
I want to look at a particular pixel and see if it has a color value
equal to the value of a color variable, c. For example:

dim c as color

c = color.red

If bitmap(x,y) = c then
.
.
.

For reaons that I don't understand the equality is not recognized
even though I am in debug mode and I can see that the value of c and
bitmap(x,y) are identical. Is there some secret to comparing color
values in VB 2005?

In addition to Peter's answer:
There is no secret. You can compare strings and numeric values, but not
complex objects. Which properties do you want to compare? The R value? G
value? B value?


Armin
 
P

Peter Macej

That did it ... thanks a lot. I don't understand why
If color1 = color2 then ...

doesn't work.

Color is an object - reference type. You cannot simply compare two
objects by "=" because objects may be very complex with many properties
which themselves may be complex nested data types.

String is one exception, it's object and you can compare it with "=".
Normally you can only compare primitive types like Integer or Boolean.
Color.ToArgb returns Integer and that's why you can compare it.
 
A

Armin Zingler

Armin Zingler said:
In addition to Peter's answer:
There is no secret. You can compare strings and numeric values, but
not complex objects. Which properties do you want to compare? The R
value? G value? B value?

I was talking about the Color type. In your own classes, you can define your
own comparison operators. Not available in VB 2003.


Armin
 
P

Phill W.

fripper said:
Is there some secret to comparing color values in VB 2005?

Unless it's changed from VB 2003 ...

Dim c1 as Color = Color.Back
Dim c2 as Color = Color.Green

If c1.Equals( c2 ) Then
...

HTH,
Phill W.
 
L

Larry Lard

Phill said:
Unless it's changed from VB 2003 ...

Dim c1 as Color = Color.Back
Dim c2 as Color = Color.Green

If c1.Equals( c2 ) Then

It is unfortunate that while this looks like the right thing to do, it
isn't. From the docs for Color.Equals (my emphasis):
This structure only does comparisons with other Color structures. To
compare colors based solely on their ARGB values, you should do the
following:

if ( color1.ToArgb() == color2.ToArgb()) ...

This is because the .Equals and == operators determine equivalency
using more than just the ARGB value of the colors. ****For example,
Color.Black and Color.FromArgb(0,0,0) are not considered equal since
Color.Black is a named color and Color.FromArgb(0,0,0) is not.****
I'm not a philosopher, so I can't make an _educated_ comment on the
logic that leads Color.Black and RGB 0,0,0 to be regarded as 'not
equal' ...
 
F

fripper

Thanks for the clear and helpful comments.



Larry Lard said:
It is unfortunate that while this looks like the right thing to do, it
isn't. From the docs for Color.Equals (my emphasis):

This structure only does comparisons with other Color structures. To
compare colors based solely on their ARGB values, you should do the
following:

if ( color1.ToArgb() == color2.ToArgb()) ...

This is because the .Equals and == operators determine equivalency
using more than just the ARGB value of the colors. ****For example,
Color.Black and Color.FromArgb(0,0,0) are not considered equal since
Color.Black is a named color and Color.FromArgb(0,0,0) is not.****

I'm not a philosopher, so I can't make an _educated_ comment on the
logic that leads Color.Black and RGB 0,0,0 to be regarded as 'not
equal' ...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top