PC Review


Reply
Thread Tools Rate Thread

Color comparer mystery

 
 
Andrew
Guest
Posts: n/a
 
      3rd Jul 2008
The following code gets colors and sorts them. When calling
GetColorsSorted() it uses the ColorComparer to compare each color.

In VS2008 on XP when running with the "Debug" build the compare has a
different number of iterations to when running under a "Release" build. I
can't understand why this would be.

When the program was run on Vista the "Debug" version worked (again with a
different no of iterations) but the "Release" version hung forever because
the comparer never produced a zero return when it found the same items.

Why would the float variables get different values between the "Debug" and
"Release" builds?

Can anyone understand why it would never drop through to the zero return?

I have now fixed this, see the bottom for details.

Here is the code:

public static IEnumerable<Color> GetColors()
{
foreach (PropertyInfo PInfo in typeof(Color).GetProperties())
if (PInfo.PropertyType == typeof(Color))
if (PInfo.Name != "Transparent")
if (PInfo.Name != "Empty")
yield return Color.FromName(PInfo.Name);
}

public static IEnumerable<Color> GetColorsSorted()
{
List<Color> List = new List<Color>();

List.AddRange(GetColors());

List.Sort(new ColorComparer());

foreach (Color C in List)
yield return C;
}

internal class ColorComparer : IComparer<Color>
{
#region IComparer Members

public int Compare(Color x, Color y)
{
float hx, hy, sx, sy, bx, by;

// get saturation values
sx = x.GetSaturation();
sy = y.GetSaturation();
// get hue values
hx = x.GetHue();
hy = y.GetHue();
// get brightness values
bx = x.GetBrightness();
by = y.GetBrightness();

// determine order
// 1 : hue
if (hx < hy)
return -1;
else if (hx > hy)
return 1;
else
{
// 2 : saturation
if (sx < sy)
return -1;
else if (sx > sy)
return 1;
else
{
// 3 : brightness
if (bx < by)
return -1;
else if (bx > by)
return 1;
else
return 0;
}
}
}
#endregion
}


To fix this I added the following code at the top of the comparer:
if (x.ToArgb() == y.ToArgb())
return 0;



--
Andrew Cutforth - AJC Software - www.ajcsoft.com
The best folder synchronize and directory compare tool available.
AJC Active Backup instantly archives every file you edit giving you
unlimited undo and automatic revision control. Never lose your data again.


 
Reply With Quote
 
 
 
 
jp2msft
Guest
Posts: n/a
 
      3rd Jul 2008
That's neat code, Andrew. You've got a few more years under your belt than I
do.

I'm going to go see what "yield" means, now.
 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need Char Comparer Help Jonathan Wood Microsoft C# .NET 14 16th Jan 2010 08:55 AM
about IComparer and Comparer Tony Microsoft C# .NET 10 17th Jun 2008 11:24 AM
mystery color shift in background =?Utf-8?B?cmFkbWFuMjAyMA==?= Microsoft Frontpage 3 7th May 2007 09:48 PM
mystery background color danwABC-googNOSPAMMER@yahoo.com Microsoft Excel Discussion 9 21st Sep 2006 08:18 PM
SortedList - Comparer help sho_nuff Microsoft C# .NET 1 16th Jun 2004 02:45 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:11 PM.