Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implements Color.Equal() correctly #15218

Merged
merged 2 commits into from
May 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions src/Graphics/src/Graphics/Color.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,11 @@ public override int GetHashCode()
public override bool Equals(object obj)
{
if (obj is Color other)
return NearlyEqual(Red, other.Red)
&& NearlyEqual(Green, other.Green)
&& NearlyEqual(Blue, other.Blue)
&& NearlyEqual(Alpha, other.Alpha);
return ToInt() == other.ToInt();

return base.Equals(obj);
}

static bool NearlyEqual(float f1, float f2, float epsilon = 0.01f)
=> Math.Abs(f1 - f2) < epsilon;

[Obsolete("Use ToArgbHex instead.")]
public string ToHex(bool includeAlpha)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ public static IEnumerable<object[]> ColorConvertData()
new object[] { "hsva(0,0,0,1)", new Color() },
new object[] { "hsva(0,0,0,0)", Colors.Transparent },

new object[] { "hsl(253,66,50)", Color.FromArgb("#512BD4") },
new object[] { "hsv(253,80,83)", Color.FromArgb("#512BD4") },
new object[] { "hsl(253,66,50)", Color.FromArgb("#4F2BD3") },
new object[] { "hsv(253,80,83)", Color.FromArgb("#4F2AD3") },
new object[] { "rgb(81,43,212)", Color.FromArgb("#512BD4") },
};

Expand Down