Browse Source

Add equality operators to Color

Former-commit-id: f3fcba3beea06add26d444ec3901f335e9ed4be8
Former-commit-id: 53d1384beec06442949f63977e925249f29ed44b
Former-commit-id: 9aa082422225046361a153bc299a9f2794311165
af/merge-core
James Jackson-South 10 years ago
parent
commit
ddd64ec78d
  1. 91
      src/ImageProcessor/Colors/Color.cs
  2. 2
      src/ImageProcessor/project.lock.json.REMOVED.git-id

91
src/ImageProcessor/Colors/Color.cs

@ -5,6 +5,8 @@
namespace ImageProcessor
{
using System;
using System.ComponentModel;
using System.Numerics;
/// <summary>
@ -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.
/// </remarks>
public struct Color
public struct Color : IEquatable<Color>
{
/// <summary>
/// Represents a <see cref="Color"/> that has R, G, B, and A values set to zero.
@ -61,6 +63,12 @@ namespace ImageProcessor
this.backingVector = vector;
}
/// <summary>
/// Gets a value indicating whether this <see cref="Color"/> is empty.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public bool IsEmpty => this.backingVector.Equals(default(Vector4));
/// <summary>
/// Gets or sets the blue component of the color.
/// </summary>
@ -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);
}
/// <summary>
/// Compares two <see cref="Color"/> objects for equality.
/// </summary>
/// <param name="left">
/// The <see cref="Color"/> on the left side of the operand.
/// </param>
/// <param name="right">
/// The <see cref="Color"/> on the right side of the operand.
/// </param>
/// <returns>
/// True if the current left is equal to the <paramref name="right"/> parameter; otherwise, false.
/// </returns>
public static bool operator ==(Color left, Color right)
{
return left.Equals(right);
}
/// <summary>
/// Compares two <see cref="Hsv"/> objects for inequality.
/// </summary>
/// <param name="left">
/// The <see cref="Color"/> on the left side of the operand.
/// </param>
/// <param name="right">
/// The <see cref="Color"/> on the right side of the operand.
/// </param>
/// <returns>
/// True if the current left is unequal to the <paramref name="right"/> parameter; otherwise, false.
/// </returns>
public static bool operator !=(Color left, Color right)
{
return !left.Equals(right);
}
/// <summary>
/// Returns a new color whose components are the average of the components of first and second.
/// </summary>
@ -250,5 +292,52 @@ namespace ImageProcessor
return (from * (1 - amount)) + (to * amount);
}
/// <inheritdoc/>
public override bool Equals(object obj)
{
if (obj is Color)
{
Color color = (Color)obj;
return this.backingVector == color.backingVector;
}
return false;
}
/// <inheritdoc/>
public override int GetHashCode()
{
return GetHashCode(this);
}
/// <inheritdoc/>
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.##} ]";
}
/// <inheritdoc/>
public bool Equals(Color other)
{
return this.backingVector.Equals(other.backingVector);
}
/// <summary>
/// Returns the hash code for this instance.
/// </summary>
/// <param name="color">
/// The instance of <see cref="Color"/> to return the hash code for.
/// </param>
/// <returns>
/// A 32-bit signed integer that is the hash code for this instance.
/// </returns>
private static int GetHashCode(Color color) => color.backingVector.GetHashCode();
}
}

2
src/ImageProcessor/project.lock.json.REMOVED.git-id

@ -1 +1 @@
eb00c54ee74016c2b70f81963e7e8f83cb2dd54b
3f05708641eb3ed085d4689aae4a960eb067fd16
Loading…
Cancel
Save