Skip to content

Commit 64130c8

Browse files
Add compare Color
- operator == - operator != - Equals
1 parent c05afdf commit 64130c8

2 files changed

Lines changed: 59 additions & 0 deletions

File tree

IronSoftware.Drawing/IronSoftware.Drawing.Common.Tests/UnitTests/ColorFunctionality.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,14 @@ public void Should_Create_FromName()
558558
color.B.Should().Be(0);
559559
}
560560

561+
[FactWithAutomaticDisplayName]
562+
public void Should_Equal()
563+
{
564+
Color.Red.Equals(Color.FromName("red")).Should().BeTrue();
565+
(Color.Yellow == Color.FromName("yellow")).Should().BeTrue();
566+
(Color.Gray != Color.FromName("darkgray")).Should().BeTrue();
567+
}
568+
561569
#if !NET472
562570
[FactWithAutomaticDisplayName]
563571
public void Cast_Maui_from_Color()

IronSoftware.Drawing/IronSoftware.Drawing.Common/Color.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,6 +1083,57 @@ static public implicit operator Microsoft.Maui.Graphics.Color(Color Color)
10831083
return Microsoft.Maui.Graphics.Color.FromRgba(Color.R, Color.G, Color.B, Color.A);
10841084
}
10851085

1086+
/// <summary>
1087+
/// Tests whether two specified <see cref="Color"/> structures are equivalent.
1088+
/// </summary>
1089+
/// <param name="left">The <see cref="Color"/> that is to the left of the equality operator.</param>
1090+
/// <param name="right">The <see cref="Color"/> that is to the right of the equality operator.</param>
1091+
/// <returns>true if the two <see cref="Color"/> structures are equal; otherwise, false.</returns>
1092+
public static bool operator ==(Color left, Color right)
1093+
{
1094+
return left.R == right.R &&
1095+
left.G == right.G &&
1096+
left.B == right.B &&
1097+
left.A == right.A;
1098+
}
1099+
1100+
/// <summary>
1101+
/// Tests whether two specified <see cref="Color"/> structures are different.
1102+
/// </summary>
1103+
/// <param name="left">The <see cref="Color"/> that is to the left of the inequality operator.</param>
1104+
/// <param name="right">The <see cref="Color"/> that is to the right of the inequality operator.</param>
1105+
/// <returns>true if the two <see cref="Color"/> structures are different; otherwise, false.</returns>
1106+
public static bool operator !=(Color left, Color right)
1107+
{
1108+
return left.R != right.R ||
1109+
left.G != right.G ||
1110+
left.B != right.B ||
1111+
left.A != right.A;
1112+
}
1113+
1114+
/// <summary>
1115+
/// Indicates whether the current object is equal to another object of the same type.
1116+
/// </summary>
1117+
/// <param name="other">An object to compare with this object.</param>
1118+
/// <returns>true if the current object is equal to other; otherwise, false.</returns>
1119+
public override bool Equals(object other)
1120+
{
1121+
if (other == null)
1122+
{
1123+
return false;
1124+
}
1125+
1126+
if (!(other is Color))
1127+
{
1128+
return false;
1129+
}
1130+
1131+
return this.R == ((Color)other).R &&
1132+
this.G == ((Color)other).G &&
1133+
this.B == ((Color)other).B &&
1134+
this.A== ((Color)other).A;
1135+
}
1136+
10861137
#region Private Method
10871138

10881139
private static InvalidOperationException NoConverterException(string color, Exception innerException)

0 commit comments

Comments
 (0)