Browse Source

Fixing bug with non-inclusive AlmostEquals.

af/merge-core
Olivia 10 years ago
parent
commit
9208de1659
  1. 6
      src/ImageSharp/Colors/Spaces/CieLab.cs
  2. 6
      src/ImageSharp/Colors/Spaces/CieXyz.cs
  3. 8
      src/ImageSharp/Colors/Spaces/Cmyk.cs
  4. 6
      src/ImageSharp/Colors/Spaces/Hsl.cs
  5. 6
      src/ImageSharp/Colors/Spaces/Hsv.cs
  6. 35
      tests/ImageSharp.Tests/Colors/ColorEqualityTests.cs

6
src/ImageSharp/Colors/Spaces/CieLab.cs

@ -169,9 +169,9 @@ namespace ImageSharp.Colors.Spaces
{
Vector3 result = Vector3.Abs(this.backingVector - other.backingVector);
return result.X < precision
&& result.Y < precision
&& result.Z < precision;
return result.X <= precision
&& result.Y <= precision
&& result.Z <= precision;
}
}
}

6
src/ImageSharp/Colors/Spaces/CieXyz.cs

@ -160,9 +160,9 @@ namespace ImageSharp.Colors.Spaces
{
Vector3 result = Vector3.Abs(this.backingVector - other.backingVector);
return result.X < precision
&& result.Y < precision
&& result.Z < precision;
return result.X <= precision
&& result.Y <= precision
&& result.Z <= precision;
}
}
}

8
src/ImageSharp/Colors/Spaces/Cmyk.cs

@ -170,10 +170,10 @@ namespace ImageSharp.Colors.Spaces
{
Vector4 result = Vector4.Abs(this.backingVector - other.backingVector);
return result.X < precision
&& result.Y < precision
&& result.Z < precision
&& result.W < precision;
return result.X <= precision
&& result.Y <= precision
&& result.Z <= precision
&& result.W <= precision;
}
}
}

6
src/ImageSharp/Colors/Spaces/Hsl.cs

@ -189,9 +189,9 @@ namespace ImageSharp.Colors.Spaces
{
Vector3 result = Vector3.Abs(this.backingVector - other.backingVector);
return result.X < precision
&& result.Y < precision
&& result.Z < precision;
return result.X <= precision
&& result.Y <= precision
&& result.Z <= precision;
}
}
}

6
src/ImageSharp/Colors/Spaces/Hsv.cs

@ -182,9 +182,9 @@ namespace ImageSharp.Colors.Spaces
{
Vector3 result = Vector3.Abs(this.backingVector - other.backingVector);
return result.X < precision
&& result.Y < precision
&& result.Z < precision;
return result.X <= precision
&& result.Y <= precision
&& result.Z <= precision;
}
}
}

35
tests/ImageSharp.Tests/Colors/ColorEqualityTests.cs

@ -181,6 +181,23 @@ namespace ImageSharp.Tests.Colors
{ new YCbCr(100, 100, 0), new YCbCr(100, 20, 0), typeof(YCbCr) },
};
public static readonly TheoryData<object, object, Type, float> AlmostEqualsData =
new TheoryData<object, object, Type, float>()
{
{ new CieLab(0f, 0f, 0f), new CieLab(0f, 0f, 0f), typeof(CieLab), 0f },
{ new CieLab(0f, 0f, 0f), new CieLab(0f, 0f, 0f), typeof(CieLab), .001f },
{ new CieLab(0f, 0f, 0f), new CieLab(0f, 0f, 0f), typeof(CieLab), .0001f },
{ new CieLab(0f, 0f, 0f), new CieLab(0f, 0f, 0f), typeof(CieLab), .0005f },
{ new CieLab(0f, 0f, 0f), new CieLab(0f, .001f, 0f), typeof(CieLab), .001f },
{ new CieLab(0f, 0f, 0f), new CieLab(0f, 0f, .0001f), typeof(CieLab), .0001f },
{ new CieLab(0f, 0f, 0f), new CieLab(.0005f, 0f, 0f), typeof(CieLab), .0005f },
//{ new CieXyz(380f, 380f, 380f), null, typeof(CieXyz) },
//{ new Cmyk(0f, 0f, 0f, 0f), null, typeof(Cmyk) },
//{ new Hsl(0f, 0f, 0f), null, typeof(Hsl) },
//{ new Hsv(360f, 1f, 1f), null, typeof(Hsv) },
//{ new YCbCr(0, 0, 0), null, typeof(YCbCr) },
};
[Theory]
[MemberData(nameof(EqualityData))]
[MemberData(nameof(EqualityDataColorSpaces))]
@ -308,5 +325,23 @@ namespace ImageSharp.Tests.Colors
// Assert
Assert.True(notEqual);
}
[Theory]
[MemberData(nameof(AlmostEqualsData))]
public void AlmostEquals(object first, object second, Type type, float precision)
{
// Arrange
// Cast to the known object types, this is so that we can hit the
// equality operator on the concrete type, otherwise it goes to the
// default "object" one :)
dynamic firstObject = Convert.ChangeType(first, type);
dynamic secondObject = Convert.ChangeType(second, type);
// Act
var almostEqual = firstObject.AlmostEquals(secondObject, precision);
// Assert
Assert.True(almostEqual);
}
}
}

Loading…
Cancel
Save