Bug 26455 - Color GetHashCode returns wrong results.
Summary: Color GetHashCode returns wrong results.
Status: VERIFIED FIXED
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 1.3.1
Hardware: PC Windows
: Normal critical
Target Milestone: ---
Assignee: Rui Marinho
URL: http://www.teechart.net/support/viewt...
Depends on:
Blocks:
 
Reported: 2015-01-27 07:19 UTC by Narcís Calvet
Modified: 2015-06-16 14:01 UTC (History)
7 users (show)

Tags: ac
Is this bug a regression?: ---
Last known good build:


Attachments
Sample App (6.45 MB, application/octet-stream)
2015-01-29 06:46 UTC, Udham Singh
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
VERIFIED FIXED

Description Narcís Calvet 2015-01-27 07:19:28 UTC
Xamarin.Forms.Color.GetHashCode() method is wrong. Xamarin.Forms.Color.Red and Xamarin.Forms.Color.Blue get the same result from GetHashCode(). This can be seen with this code snippet:

                    Color red = Color.Red; //R=1, G=0, B=0, A=1
                    int hashRed = red.GetHashCode();

                    Color blue = Color.Blue; //R=0, G=0, B=1, A=1
                    int hashBlue = blue.GetHashCode();

                    Color green = Color.Green; //R=0, G=0,501960813999176, B=0, A=0
                    int hashGreen = green.GetHashCode();

                    if (hashRed == hashBlue)
                    {
                      //Xamarin.Forms.Color.GetHashCode() is wrong.
                    }

That's probably to the implementation of the Xamarin.Forms.Color.GetHashCode() method, which I obtained using .NET Reflector:

public override int GetHashCode()
{
    return (((((float) this.r).GetHashCode() + ((float) this.g).GetHashCode()) + ((float) this.b).GetHashCode()) + ((float) this.a).GetHashCode());
}

With this implementation, Color.Red (1, 0, 0, 1) and Color.Blue (0, 0, 1, 1) constants have exactly the same values but in a different order, which creates the same result for GetHashCode(). Color.Green, for example, is not defined to be (0, 1, 0, 1) and therefore provides a different hash code. However, defining green like this:

                    Color green = Color.FromRgba(0, 255, 0, 255);//Color.Green;
                    int hashGreen = green.GetHashCode();

returns the same hash code as Color.Blue and Color.Red constants. 

Wrong hash code leads to wrong object caching as can bee seen with the problem a common customer described at the URL.
Comment 1 Narcís Calvet 2015-01-27 09:37:17 UTC
System.Drawing.Color.GetHashCode is more sofisticated:

https://github.com/mosa/Mono-Class-Libraries/blob/master/mcs/class/System.Drawing/System.Drawing/Color.cs
http://referencesource.microsoft.com/#System.Drawing/commonui/System/Drawing/Color.cs,954f1e7775630ce1

I wonder if something like this is possible in Xamarin.Forms.
Comment 2 Udham Singh 2015-01-29 06:46:18 UTC
Created attachment 9547 [details]
Sample App

I have checked this issue with the help of code snippet given in bug description and observed the reported behavior. Please refer the screencast : http://www.screencast.com/t/lkENfgA3

Environment Info : 

Xamarin.Forms : 1.3.2.6309-pre2

Microsoft Visual Studio Professional 2013
Version 12.0.31101.00 Update 4
Microsoft .NET Framework
Version 4.5.50938

Xamarin   3.9.291.0 (9f3e552)
Comment 3 Rui Marinho 2015-06-11 18:46:25 UTC
Should be fixed in 1.4.3 final.
Comment 4 Rajneesh Kumar 2015-06-16 14:01:59 UTC
I have checked this issue with Xamarin.Forms 1.4.3.6364-pre3 and its working fine at my end.

Hence closing this issue.

Thanks..!

Environment info:

Microsoft Visual Studio Professional 2013
Version 12.0.30723.00 Update 3
Microsoft .NET Framework
Version 4.5.51641

Xamarin   3.11.590.0 
Xamarin.Android   5.1.3.1 
Xamarin.iOS   8.10.1.0