diff --git a/src/ImageProcessor/Colors/Color.cs b/src/ImageProcessor/Colors/Color.cs index 682e5f119..e2bff3e03 100644 --- a/src/ImageProcessor/Colors/Color.cs +++ b/src/ImageProcessor/Colors/Color.cs @@ -5,6 +5,8 @@ namespace ImageProcessor { + using System; + using System.ComponentModel; using System.Numerics; /// @@ -14,7 +16,7 @@ namespace ImageProcessor /// This struct is fully mutable. This is done (against the guidelines) for the sake of performance, /// as it avoids the need to create new values for modification operations. /// - public struct Color + public struct Color : IEquatable { /// /// Represents a that has R, G, B, and A values set to zero. @@ -61,6 +63,12 @@ namespace ImageProcessor this.backingVector = vector; } + /// + /// Gets a value indicating whether this is empty. + /// + [EditorBrowsable(EditorBrowsableState.Never)] + public bool IsEmpty => this.backingVector.Equals(default(Vector4)); + /// /// Gets or sets the blue component of the color. /// @@ -220,6 +228,40 @@ namespace ImageProcessor return new Color(left.R - right.R, left.G - right.G, left.B - right.B, left.A - right.A); } + /// + /// Compares two objects for equality. + /// + /// + /// The on the left side of the operand. + /// + /// + /// The on the right side of the operand. + /// + /// + /// True if the current left is equal to the parameter; otherwise, false. + /// + public static bool operator ==(Color left, Color right) + { + return left.Equals(right); + } + + /// + /// Compares two objects for inequality. + /// + /// + /// The on the left side of the operand. + /// + /// + /// The on the right side of the operand. + /// + /// + /// True if the current left is unequal to the parameter; otherwise, false. + /// + public static bool operator !=(Color left, Color right) + { + return !left.Equals(right); + } + /// /// Returns a new color whose components are the average of the components of first and second. /// @@ -250,5 +292,52 @@ namespace ImageProcessor return (from * (1 - amount)) + (to * amount); } + + /// + public override bool Equals(object obj) + { + if (obj is Color) + { + Color color = (Color)obj; + + return this.backingVector == color.backingVector; + } + + return false; + } + + /// + public override int GetHashCode() + { + return GetHashCode(this); + } + + /// + public override string ToString() + { + if (this.IsEmpty) + { + return "Color [ Empty ]"; + } + + return $"Color [ R={this.R:#0.##}, G={this.G:#0.##}, B={this.B:#0.##}, A={this.A:#0.##} ]"; + } + + /// + public bool Equals(Color other) + { + return this.backingVector.Equals(other.backingVector); + } + + /// + /// Returns the hash code for this instance. + /// + /// + /// The instance of to return the hash code for. + /// + /// + /// A 32-bit signed integer that is the hash code for this instance. + /// + private static int GetHashCode(Color color) => color.backingVector.GetHashCode(); } } diff --git a/src/ImageProcessor/project.lock.json.REMOVED.git-id b/src/ImageProcessor/project.lock.json.REMOVED.git-id index dba2656f5..24339fed2 100644 --- a/src/ImageProcessor/project.lock.json.REMOVED.git-id +++ b/src/ImageProcessor/project.lock.json.REMOVED.git-id @@ -1 +1 @@ -eb00c54ee74016c2b70f81963e7e8f83cb2dd54b \ No newline at end of file +3f05708641eb3ed085d4689aae4a960eb067fd16 \ No newline at end of file