|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Comparing ColorsI 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? Thanks. Use Color.ToArgb method:
If color1.ToArgb = color2.ToArgb Then -- Peter Macej Helixoft - http://www.vbdocman.com VBdocman - Automatic generator of technical documentation for VB, VB ..NET and ASP .NET code That did it ... thanks a lot. I don't understand why
If color1 = color2 then ... doesn't work. Show quoteHide quote "Peter Macej" <pe***@vbdocman.com> wrote in message news:%23%23XCsYiPGHA.668@TK2MSFTNGP11.phx.gbl... > Use Color.ToArgb method: > > If color1.ToArgb = color2.ToArgb Then > > > -- > Peter Macej > Helixoft - http://www.vbdocman.com > VBdocman - Automatic generator of technical documentation for VB, VB .NET > and ASP .NET code > That did it ... thanks a lot. I don't understand why Color is an object - reference type. You cannot simply compare two > > If color1 = color2 then ... > > doesn't work. 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. -- Peter Macej Helixoft - http://www.vbdocman.com VBdocman - Automatic generator of technical documentation for VB, VB ..NET and ASP .NET code "Peter Macej" <pe***@vbdocman.com> schrieb: 'Color' is a value type (structure).>> That did it ... thanks a lot. I don't understand why >> >> If color1 = color2 then ... >> >> doesn't work. > > Color is an object - reference type. -- M S Herfried K. Wagner M V P <URL:http://dotnet.mvps.org/> V B <URL:http://classicvb.org/petition/>
Show quote
Hide quote
"fripper" <yo***@indiana.edu> schrieb In addition to Peter's answer:> > 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? 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 "Armin Zingler" <az.nospam@freenet.de> schrieb I was talking about the Color type. In your own classes, you can define your> 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? own comparison operators. Not available in VB 2003. Armin "fripper" <yo***@indiana.edu> wrote in message Unless it's changed from VB 2003 ...news:u6p329hPGHA.3576@TK2MSFTNGP15.phx.gbl... > > Is there some secret to comparing color values in VB 2005? Dim c1 as Color = Color.Back Dim c2 as Color = Color.Green If c1.Equals( c2 ) Then ... HTH, Phill W. Phill W. wrote:
> "fripper" <yo***@indiana.edu> wrote in message It is unfortunate that while this looks like the right thing to do, it> news:u6p329hPGHA.3576@TK2MSFTNGP15.phx.gbl... > > > > 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 isn't. From the docs for Color.Equals (my emphasis): >> This structure only does comparisons with other Color structures. Tocompare 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 thelogic that leads Color.Black and RGB 0,0,0 to be regarded as 'not equal' ... -- Larry Lard Replies to group please Thanks for the clear and helpful comments.
Show quoteHide quote "Larry Lard" <larryl***@hotmail.com> wrote in message news:1141394987.597772.183280@i39g2000cwa.googlegroups.com... > > Phill W. wrote: >> "fripper" <yo***@indiana.edu> wrote in message >> news:u6p329hPGHA.3576@TK2MSFTNGP15.phx.gbl... >> > >> > 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 > > 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' ... > > -- > Larry Lard > Replies to group please > |
|||||||||||||||||||||||