diff --git a/src/ImageSharp/Colors/ColorspaceTransforms.cs b/src/ImageSharp/Colors/ColorspaceTransforms.cs index 9785a888f4..cda7022705 100644 --- a/src/ImageSharp/Colors/ColorspaceTransforms.cs +++ b/src/ImageSharp/Colors/ColorspaceTransforms.cs @@ -19,11 +19,6 @@ namespace ImageSharp /// public partial struct Color { - /// - /// The epsilon for comparing floating point numbers. - /// - private const float Epsilon = 0.001F; - /// /// Allows the implicit conversion of an instance of to a /// . @@ -110,12 +105,12 @@ namespace ImageSharp float s = color.S; float v = color.V; - if (Math.Abs(s) < Epsilon) + if (Math.Abs(s) < Constants.Epsilon) { return new Color(v, v, v, 1); } - float h = (Math.Abs(color.H - 360) < Epsilon) ? 0 : color.H / 60; + float h = (Math.Abs(color.H - 360) < Constants.Epsilon) ? 0 : color.H / 60; int i = (int)Math.Truncate(h); float f = h - i; @@ -183,9 +178,9 @@ namespace ImageSharp float s = color.S; float l = color.L; - if (Math.Abs(l) > Epsilon) + if (Math.Abs(l) > Constants.Epsilon) { - if (Math.Abs(s) < Epsilon) + if (Math.Abs(s) < Constants.Epsilon) { r = g = b = l; } diff --git a/src/ImageSharp/Colors/Spaces/CieLab.cs b/src/ImageSharp/Colors/Spaces/CieLab.cs index faa6c24724..f4f349b52b 100644 --- a/src/ImageSharp/Colors/Spaces/CieLab.cs +++ b/src/ImageSharp/Colors/Spaces/CieLab.cs @@ -20,11 +20,6 @@ namespace ImageSharp.Colors.Spaces /// public static readonly CieLab Empty = default(CieLab); - /// - /// The epsilon for comparing floating point numbers. - /// - private const float Epsilon = 0.001f; - /// /// The backing vector for SIMD support. /// @@ -166,7 +161,7 @@ namespace ImageSharp.Colors.Spaces /// public bool Equals(CieLab other) { - return this.AlmostEquals(other, Epsilon); + return this.AlmostEquals(other, Constants.Epsilon); } /// diff --git a/src/ImageSharp/Colors/Spaces/CieXyz.cs b/src/ImageSharp/Colors/Spaces/CieXyz.cs index 9deffe0669..49396d3f61 100644 --- a/src/ImageSharp/Colors/Spaces/CieXyz.cs +++ b/src/ImageSharp/Colors/Spaces/CieXyz.cs @@ -20,11 +20,6 @@ namespace ImageSharp.Colors.Spaces /// public static readonly CieXyz Empty = default(CieXyz); - /// - /// The epsilon for comparing floating point numbers. - /// - private const float Epsilon = 0.001f; - /// /// The backing vector for SIMD support. /// @@ -157,7 +152,7 @@ namespace ImageSharp.Colors.Spaces /// public bool Equals(CieXyz other) { - return this.AlmostEquals(other, Epsilon); + return this.AlmostEquals(other, Constants.Epsilon); } /// diff --git a/src/ImageSharp/Colors/Spaces/Cmyk.cs b/src/ImageSharp/Colors/Spaces/Cmyk.cs index e8f2a41f5c..190d73598f 100644 --- a/src/ImageSharp/Colors/Spaces/Cmyk.cs +++ b/src/ImageSharp/Colors/Spaces/Cmyk.cs @@ -19,11 +19,6 @@ namespace ImageSharp.Colors.Spaces /// public static readonly Cmyk Empty = default(Cmyk); - /// - /// The epsilon for comparing floating point numbers. - /// - private const float Epsilon = 0.001f; - /// /// The backing vector for SIMD support. /// @@ -90,7 +85,7 @@ namespace ImageSharp.Colors.Spaces float k = Math.Min(c, Math.Min(m, y)); - if (Math.Abs(k - 1.0f) <= Epsilon) + if (Math.Abs(k - 1.0f) <= Constants.Epsilon) { return new Cmyk(0, 0, 0, 1); } @@ -167,7 +162,7 @@ namespace ImageSharp.Colors.Spaces /// public bool Equals(Cmyk other) { - return this.AlmostEquals(other, Epsilon); + return this.AlmostEquals(other, Constants.Epsilon); } /// diff --git a/src/ImageSharp/Colors/Spaces/Hsl.cs b/src/ImageSharp/Colors/Spaces/Hsl.cs index 5c478ecf14..2cb02107b9 100644 --- a/src/ImageSharp/Colors/Spaces/Hsl.cs +++ b/src/ImageSharp/Colors/Spaces/Hsl.cs @@ -19,11 +19,6 @@ namespace ImageSharp.Colors.Spaces /// public static readonly Hsl Empty = default(Hsl); - /// - /// The epsilon for comparing floating point numbers. - /// - private const float Epsilon = 0.001F; - /// /// The backing vector for SIMD support. /// @@ -85,20 +80,20 @@ namespace ImageSharp.Colors.Spaces float s = 0; float l = (max + min) / 2; - if (Math.Abs(chroma) < Epsilon) + if (Math.Abs(chroma) < Constants.Epsilon) { return new Hsl(0, s, l); } - if (Math.Abs(r - max) < Epsilon) + if (Math.Abs(r - max) < Constants.Epsilon) { h = (g - b) / chroma; } - else if (Math.Abs(g - max) < Epsilon) + else if (Math.Abs(g - max) < Constants.Epsilon) { h = 2 + ((b - r) / chroma); } - else if (Math.Abs(b - max) < Epsilon) + else if (Math.Abs(b - max) < Constants.Epsilon) { h = 4 + ((r - g) / chroma); } @@ -186,7 +181,7 @@ namespace ImageSharp.Colors.Spaces /// public bool Equals(Hsl other) { - return this.AlmostEquals(other, Epsilon); + return this.AlmostEquals(other, Constants.Epsilon); } /// diff --git a/src/ImageSharp/Colors/Spaces/Hsv.cs b/src/ImageSharp/Colors/Spaces/Hsv.cs index 9df16a27b4..8f7ebbdc76 100644 --- a/src/ImageSharp/Colors/Spaces/Hsv.cs +++ b/src/ImageSharp/Colors/Spaces/Hsv.cs @@ -19,11 +19,6 @@ namespace ImageSharp.Colors.Spaces /// public static readonly Hsv Empty = default(Hsv); - /// - /// The epsilon for comparing floating point numbers. - /// - private const float Epsilon = 0.001F; - /// /// The backing vector for SIMD support. /// @@ -85,20 +80,20 @@ namespace ImageSharp.Colors.Spaces float s = 0; float v = max; - if (Math.Abs(chroma) < Epsilon) + if (Math.Abs(chroma) < Constants.Epsilon) { return new Hsv(0, s, v); } - if (Math.Abs(r - max) < Epsilon) + if (Math.Abs(r - max) < Constants.Epsilon) { h = (g - b) / chroma; } - else if (Math.Abs(g - max) < Epsilon) + else if (Math.Abs(g - max) < Constants.Epsilon) { h = 2 + ((b - r) / chroma); } - else if (Math.Abs(b - max) < Epsilon) + else if (Math.Abs(b - max) < Constants.Epsilon) { h = 4 + ((r - g) / chroma); } @@ -179,7 +174,7 @@ namespace ImageSharp.Colors.Spaces /// public bool Equals(Hsv other) { - return this.AlmostEquals(other, Epsilon); + return this.AlmostEquals(other, Constants.Epsilon); } /// diff --git a/src/ImageSharp/Colors/Vector4BlendTransforms.cs b/src/ImageSharp/Colors/Vector4BlendTransforms.cs index 870d653888..2fa6aad4b4 100644 --- a/src/ImageSharp/Colors/Vector4BlendTransforms.cs +++ b/src/ImageSharp/Colors/Vector4BlendTransforms.cs @@ -14,11 +14,6 @@ namespace ImageSharp /// public class Vector4BlendTransforms { - /// - /// The epsilon for comparing floating point numbers. - /// - private const float Epsilon = 0.0001F; - /// /// The blending formula simply selects the source vector. /// @@ -203,13 +198,13 @@ namespace ImageSharp amount = amount.Clamp(0, 1); // Santize on zero alpha - if (Math.Abs(backdrop.W) < Epsilon) + if (Math.Abs(backdrop.W) < Constants.Epsilon) { source.W *= amount; return source; } - if (Math.Abs(source.W) < Epsilon) + if (Math.Abs(source.W) < Constants.Epsilon) { return backdrop; } @@ -266,7 +261,7 @@ namespace ImageSharp /// private static float BlendDodge(float b, float s) { - return Math.Abs(s - 1F) < Epsilon ? s : Math.Min(b / (1F - s), 1F); + return Math.Abs(s - 1F) < Constants.Epsilon ? s : Math.Min(b / (1F - s), 1F); } /// @@ -279,7 +274,7 @@ namespace ImageSharp /// private static float BlendBurn(float b, float s) { - return Math.Abs(s) < Epsilon ? s : Math.Max(1F - ((1F - b) / s), 0F); + return Math.Abs(s) < Constants.Epsilon ? s : Math.Max(1F - ((1F - b) / s), 0F); } /// diff --git a/src/ImageSharp/Common/Constants.cs b/src/ImageSharp/Common/Constants.cs new file mode 100644 index 0000000000..cf43951bc5 --- /dev/null +++ b/src/ImageSharp/Common/Constants.cs @@ -0,0 +1,18 @@ +// +// Copyright (c) James Jackson-South and contributors. +// Licensed under the Apache License, Version 2.0. +// + +namespace ImageSharp +{ + /// + /// Common constants used throughout the project + /// + internal static class Constants + { + /// + /// The epsilon for comparing floating point numbers. + /// + public static readonly float Epsilon = 0.001f; + } +} diff --git a/src/ImageSharp/Common/Helpers/ImageMaths.cs b/src/ImageSharp/Common/Helpers/ImageMaths.cs index 99212346aa..7455b542d1 100644 --- a/src/ImageSharp/Common/Helpers/ImageMaths.cs +++ b/src/ImageSharp/Common/Helpers/ImageMaths.cs @@ -91,9 +91,7 @@ namespace ImageSharp /// public static float SinC(float x) { - const float Epsilon = .00001F; - - if (Math.Abs(x) > Epsilon) + if (Math.Abs(x) > Constants.Epsilon) { x *= (float)Math.PI; return Clean((float)Math.Sin(x) / x); @@ -166,7 +164,6 @@ namespace ImageSharp public static Rectangle GetFilteredBoundingRectangle(ImageBase bitmap, float componentValue, RgbaComponent channel = RgbaComponent.B) where TColor : struct, IPackedPixel, IEquatable { - const float Epsilon = .00001f; int width = bitmap.Width; int height = bitmap.Height; Point topLeft = default(Point); @@ -178,19 +175,19 @@ namespace ImageSharp switch (channel) { case RgbaComponent.R: - delegateFunc = (pixels, x, y, b) => Math.Abs(pixels[x, y].ToVector4().X - b) > Epsilon; + delegateFunc = (pixels, x, y, b) => Math.Abs(pixels[x, y].ToVector4().X - b) > Constants.Epsilon; break; case RgbaComponent.G: - delegateFunc = (pixels, x, y, b) => Math.Abs(pixels[x, y].ToVector4().Y - b) > Epsilon; + delegateFunc = (pixels, x, y, b) => Math.Abs(pixels[x, y].ToVector4().Y - b) > Constants.Epsilon; break; case RgbaComponent.B: - delegateFunc = (pixels, x, y, b) => Math.Abs(pixels[x, y].ToVector4().Z - b) > Epsilon; + delegateFunc = (pixels, x, y, b) => Math.Abs(pixels[x, y].ToVector4().Z - b) > Constants.Epsilon; break; default: - delegateFunc = (pixels, x, y, b) => Math.Abs(pixels[x, y].ToVector4().W - b) > Epsilon; + delegateFunc = (pixels, x, y, b) => Math.Abs(pixels[x, y].ToVector4().W - b) > Constants.Epsilon; break; } @@ -278,9 +275,7 @@ namespace ImageSharp /// . private static float Clean(float x) { - const float Epsilon = .00001F; - - if (Math.Abs(x) < Epsilon) + if (Math.Abs(x) < Constants.Epsilon) { return 0F; } diff --git a/src/ImageSharp/Drawing/Processors/DrawPathProcessor.cs b/src/ImageSharp/Drawing/Processors/DrawPathProcessor.cs index 54ebc28efd..dab0baa5a2 100644 --- a/src/ImageSharp/Drawing/Processors/DrawPathProcessor.cs +++ b/src/ImageSharp/Drawing/Processors/DrawPathProcessor.cs @@ -9,7 +9,6 @@ namespace ImageSharp.Drawing.Processors using System.Linq; using System.Numerics; using System.Threading.Tasks; - using ImageSharp.Processors; using Paths; using Pens; @@ -27,7 +26,6 @@ namespace ImageSharp.Drawing.Processors { private const float AntialiasFactor = 1f; private const int PaddingFactor = 1; // needs to been the same or greater than AntialiasFactor - private const float Epsilon = 0.001f; private readonly IPen pen; private readonly IPath[] paths; @@ -138,7 +136,7 @@ namespace ImageSharp.Drawing.Processors var opacity = this.Opacity(color.DistanceFromElement); - if (opacity > Epsilon) + if (opacity > Constants.Epsilon) { int offsetColorX = x - minX; diff --git a/src/ImageSharp/Drawing/Processors/FillShapeProcessor.cs b/src/ImageSharp/Drawing/Processors/FillShapeProcessor.cs index df5cec71c8..7da2e041a2 100644 --- a/src/ImageSharp/Drawing/Processors/FillShapeProcessor.cs +++ b/src/ImageSharp/Drawing/Processors/FillShapeProcessor.cs @@ -21,8 +21,6 @@ namespace ImageSharp.Drawing.Processors public class FillShapeProcessor : ImageFilteringProcessor where TColor : struct, IPackedPixel, IEquatable { - private const float Epsilon = 0.001f; - private const float AntialiasFactor = 1f; private const int DrawPadding = 1; private readonly IBrush fillColor; @@ -95,7 +93,7 @@ namespace ImageSharp.Drawing.Processors float dist = this.poly.Distance(currentPoint); float opacity = this.Opacity(dist); - if (opacity > Epsilon) + if (opacity > Constants.Epsilon) { Vector4 backgroundVector = sourcePixels[offsetX, offsetY].ToVector4(); Vector4 sourceVector = applicator.GetColor(currentPoint).ToVector4(); diff --git a/src/ImageSharp/Filters/Processors/Effects/BackgroundColorProcessor.cs b/src/ImageSharp/Filters/Processors/Effects/BackgroundColorProcessor.cs index 122c5a0ffe..ab6fa2424f 100644 --- a/src/ImageSharp/Filters/Processors/Effects/BackgroundColorProcessor.cs +++ b/src/ImageSharp/Filters/Processors/Effects/BackgroundColorProcessor.cs @@ -16,11 +16,6 @@ namespace ImageSharp.Processors public class BackgroundColorProcessor : ImageFilteringProcessor where TColor : struct, IPackedPixel, IEquatable { - /// - /// The epsilon for comparing floating point numbers. - /// - private const float Epsilon = 0.001f; - /// /// Initializes a new instance of the class. /// @@ -82,7 +77,7 @@ namespace ImageSharp.Processors color = Vector4BlendTransforms.PremultipliedLerp(backgroundColor, color, .5F); } - if (Math.Abs(a) < Epsilon) + if (Math.Abs(a) < Constants.Epsilon) { color = backgroundColor; } diff --git a/src/ImageSharp/Filters/Processors/Transforms/RotateProcessor.cs b/src/ImageSharp/Filters/Processors/Transforms/RotateProcessor.cs index 33e5b64707..e81cd7e57e 100644 --- a/src/ImageSharp/Filters/Processors/Transforms/RotateProcessor.cs +++ b/src/ImageSharp/Filters/Processors/Transforms/RotateProcessor.cs @@ -70,9 +70,7 @@ namespace ImageSharp.Processors /// protected override void BeforeApply(ImageBase source, Rectangle sourceRectangle) { - const float Epsilon = .0001F; - - if (Math.Abs(this.Angle) < Epsilon || Math.Abs(this.Angle - 90) < Epsilon || Math.Abs(this.Angle - 180) < Epsilon || Math.Abs(this.Angle - 270) < Epsilon) + if (Math.Abs(this.Angle) < Constants.Epsilon || Math.Abs(this.Angle - 90) < Constants.Epsilon || Math.Abs(this.Angle - 180) < Constants.Epsilon || Math.Abs(this.Angle - 270) < Constants.Epsilon) { return; } @@ -91,26 +89,25 @@ namespace ImageSharp.Processors /// The private bool OptimizedApply(ImageBase source) { - const float Epsilon = .0001F; - if (Math.Abs(this.Angle) < Epsilon) + if (Math.Abs(this.Angle) < Constants.Epsilon) { // No need to do anything so return. return true; } - if (Math.Abs(this.Angle - 90) < Epsilon) + if (Math.Abs(this.Angle - 90) < Constants.Epsilon) { this.Rotate90(source); return true; } - if (Math.Abs(this.Angle - 180) < Epsilon) + if (Math.Abs(this.Angle - 180) < Constants.Epsilon) { this.Rotate180(source); return true; } - if (Math.Abs(this.Angle - 270) < Epsilon) + if (Math.Abs(this.Angle - 270) < Constants.Epsilon) { this.Rotate270(source); return true; diff --git a/src/ImageSharp/Quantizers/Wu/WuQuantizer.cs b/src/ImageSharp/Quantizers/Wu/WuQuantizer.cs index debc9518be..e235e68e88 100644 --- a/src/ImageSharp/Quantizers/Wu/WuQuantizer.cs +++ b/src/ImageSharp/Quantizers/Wu/WuQuantizer.cs @@ -33,11 +33,6 @@ namespace ImageSharp.Quantizers public sealed class WuQuantizer : IQuantizer where TColor : struct, IPackedPixel, IEquatable { - /// - /// The epsilon for comparing floating point numbers. - /// - private const float Epsilon = 1e-5F; - /// /// The index bits. /// @@ -542,7 +537,7 @@ namespace ImageSharp.Quantizers double temp; - if (Math.Abs(halfW) < Epsilon) + if (Math.Abs(halfW) < Constants.Epsilon) { continue; } @@ -555,7 +550,7 @@ namespace ImageSharp.Quantizers halfA = wholeA - halfA; halfW = wholeW - halfW; - if (Math.Abs(halfW) < Epsilon) + if (Math.Abs(halfW) < Constants.Epsilon) { continue; } @@ -762,7 +757,7 @@ namespace ImageSharp.Quantizers double weight = Volume(cube[k], this.vwt); - if (Math.Abs(weight) > Epsilon) + if (Math.Abs(weight) > Constants.Epsilon) { float r = (float)(Volume(cube[k], this.vmr) / weight); float g = (float)(Volume(cube[k], this.vmg) / weight); diff --git a/tests/ImageSharp.Tests/Common/ConstantsTests.cs b/tests/ImageSharp.Tests/Common/ConstantsTests.cs new file mode 100644 index 0000000000..0adda0c0f6 --- /dev/null +++ b/tests/ImageSharp.Tests/Common/ConstantsTests.cs @@ -0,0 +1,18 @@ +// +// Copyright (c) James Jackson-South and contributors. +// Licensed under the Apache License, Version 2.0. +// + +namespace ImageSharp.Tests.Common +{ + using Xunit; + + public class ConstantsTests + { + [Fact] + public void Epsilon() + { + Assert.Equal(Constants.Epsilon, 0.001f); + } + } +}