.NET Framework - Color comparer mystery
Asked By Andrew
03-Jul-08 11:54 AM

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
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.
PInfo.PropertyType
(1)
Vista
(1)
Color.FromName
(1)
List.AddRange
(1)
PropertyInfo
(1)
XP
(1)
GetSaturation
(1)
GetBrightness
(1)
Peter Duniho replied...
I don't know the precise answer. But it's not surprising at all to me
that there may be subtle differences in floating point behavior between
the two versions. Doing identity comparisons between floating point
values is almost never the right approach, and your experience
demonstrates why.
I think that your solution, converting to integers and then comparing
that, is a perfectly reasonable approach, especially given the problem
domain. You could also have compared the difference between two floating
point values and considered them equal as long as the difference was small
enough. That's a fairly common approach in other scenarios using floating
point, but in this case it's probably overkill.
Pete
jp2msf replied...
That's neat code, Andrew. You've got a few more years under your belt than I
do.
I am going to go see what "yield" means, now. ;)
I'm setting a databound label's backcolor and forecolor like this. . . BackColor = '<%# System.Drawing.Color.FromName(Eval("AttachmentEntityBackColor")) %> ' Font-Bold = "True" ForeColor = '<%# System.Drawing.Color.FromName(Eval("AttachmentEntityForeColor")) %> ' Text = "CURRENT COLOR SCHEME"> < / asp:Label> The problem is when the value is it's null in the db then use "#ffffff"? Thanks! ASP.NET Discussions System.Drawing.Color.FromName (1) DBNull (1) AttachmentEntityForeColor (1) FromName (1) Color (1) Eval (1) AColor (1) Equaivalent (1 DBNull.value), %> ' Text = "CURRENT COLOR SCHEME"> < / asp:Label> u can't use IIF() System.Drawing.Color.FromName(Eval("AttachmentEntityForeColor")) will still be executed, resulting in error (IIF is not equaivalent to C
Getting a Thumbnail In Vista .NET Framework Hi, I'm hoping someone can help me get a scalable thumbnail from Vista. Vista has a new COM API, IThumbnailCache which backs on to Vista's global thumbnail store. Unfortunately, I can't find it I know it's there coclasses, but it's not there. I've downloaded a spreadsheet detailing all the new Vista APIs and where they can be found, but this says IThumbnailCache is in uuid.dll any example code? Cheers, Tim http: / / www.dotnoodle.com - The DotNet Search. .NET Framework Discussions Vista (1) IThumbnailCache (1) APIs (1) Thumbnail (1) Windows (1) Coclasses (1) COM (1) IDL (1 from the IDL file. - - = 20 Jim Barry, MVP (Windows SDK) keywords: Getting, a, Thumbnail, In, Vista description: Hi, I'm hoping someone can help me get a scalable thumbnail from Vista. Vista has a new COM API, IThumbnailCache which backs on to Vista's global thumbn
ID = "lblIndicatorReady" runat = "server" Text = "RE" ToolTip = "Ready" BackColor = '<%# Eval("Ready").ToString() = = "True"?System.Drawing.Color.FromName("#FFFF80"):System.Drawing.Color.White %> ' Enabled = '<%# Eval("Ready") %> ' Font-Bold = '<%# Eval("Ready") %> '> I.e. late and Bold properties are shown correctly though. Any ideas? Dmitry. ASP.NET Discussions System.Drawing.Color.FromName (1) Color.FromName (1) KnownColor (1) EventArgs (1) FormView (1) ASP.NET (1) TextBox (1) Color.Maroon (1 further research, I got the cause of the problem. It is actually caused by the "Color.FromName" function you used. In your original code, you use it as below: System.Drawing.Color
Can the .NET2.0 application apply Vista theme in Windows Vista? .NET Framework I want to know if my .net2.0 application can apply Vista theme when run in Windows Vista. How to do if it is possible. Thank you. VB.NET Discussions Windows Vista (1 Vista (1) WPF (1) XP (1) Wagner (1) Seth (1) Rowe (1) NET2.0 (1) I XP. Thanks, Seth Rowe No, the controls have no any changes when you change Windows Vista theme. ??????:1192624763.712163.125080@z24g2000prh.googlegroups.com. . . Does your application have visual styles enabled? - - M I know is to use WPF. - - Tom Shelton keywords: Can, the, .NET2.0, application, apply, Vista, theme, in, Windows, Vista? description: I want to know if my .net2.0 application can