From c8a2c079e229f725acf6fc85f161d47b3756757f Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Wed, 6 Nov 2019 21:35:57 +1100 Subject: [PATCH] Fix float clamp accessibility. --- .../Processing/PatternBrush.cs | 2 +- .../Processing/TextGraphicsOptions.cs | 12 ++++---- src/ImageSharp.Drawing/Utils/NumberUtils.cs | 29 +++++++++++++++++++ 3 files changed, 36 insertions(+), 7 deletions(-) create mode 100644 src/ImageSharp.Drawing/Utils/NumberUtils.cs diff --git a/src/ImageSharp.Drawing/Processing/PatternBrush.cs b/src/ImageSharp.Drawing/Processing/PatternBrush.cs index 37bb9b0fc..9024036d0 100644 --- a/src/ImageSharp.Drawing/Processing/PatternBrush.cs +++ b/src/ImageSharp.Drawing/Processing/PatternBrush.cs @@ -158,7 +158,7 @@ namespace SixLabors.ImageSharp.Processing for (int i = 0; i < scanline.Length; i++) { - amountSpan[i] = (scanline[i] * this.Options.BlendPercentage).Clamp(0, 1); + amountSpan[i] = NumberUtils.ClampFloat(scanline[i] * this.Options.BlendPercentage, 0, 1F); int patternX = (x + i) % this.pattern.Columns; overlaySpan[i] = this.pattern[patternY, patternX]; diff --git a/src/ImageSharp.Drawing/Processing/TextGraphicsOptions.cs b/src/ImageSharp.Drawing/Processing/TextGraphicsOptions.cs index 6c140be72..13c7f4bdf 100644 --- a/src/ImageSharp.Drawing/Processing/TextGraphicsOptions.cs +++ b/src/ImageSharp.Drawing/Processing/TextGraphicsOptions.cs @@ -1,4 +1,4 @@ -// Copyright (c) Six Labors and contributors. +// Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. using SixLabors.Fonts; @@ -11,13 +11,13 @@ namespace SixLabors.ImageSharp.Processing /// public struct TextGraphicsOptions { - private const int DefaultTextDpi = 72; - /// /// Represents the default . /// public static readonly TextGraphicsOptions Default = new TextGraphicsOptions(true); + private const int DefaultTextDpi = 72; + private float? blendPercentage; private int? antialiasSubpixelDepth; @@ -51,7 +51,7 @@ namespace SixLabors.ImageSharp.Processing this.antialiasSubpixelDepth = 16; this.ColorBlendingMode = PixelColorBlendingMode.Normal; this.AlphaCompositionMode = PixelAlphaCompositionMode.SrcOver; - this.blendPercentage = 1; + this.blendPercentage = 1F; this.antialias = enableAntialiasing; this.dpiX = DefaultTextDpi; this.dpiY = DefaultTextDpi; @@ -70,7 +70,7 @@ namespace SixLabors.ImageSharp.Processing /// /// Gets or sets a value indicating the blending percentage to apply to the drawing operation /// - public float BlendPercentage { get => (this.blendPercentage ?? 1).Clamp(0, 1); set => this.blendPercentage = value; } + public float BlendPercentage { get => this.blendPercentage ?? 1F; set => this.blendPercentage = NumberUtils.ClampFloat(value, 0, 1F); } // In the future we could expose a PixelBlender directly on here // or some forms of PixelBlender factory for each pixel type. Will need @@ -160,4 +160,4 @@ namespace SixLabors.ImageSharp.Processing }; } } -} \ No newline at end of file +} diff --git a/src/ImageSharp.Drawing/Utils/NumberUtils.cs b/src/ImageSharp.Drawing/Utils/NumberUtils.cs new file mode 100644 index 000000000..d034c5d7e --- /dev/null +++ b/src/ImageSharp.Drawing/Utils/NumberUtils.cs @@ -0,0 +1,29 @@ +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +using System.Runtime.CompilerServices; + +namespace SixLabors.ImageSharp +{ + /// + /// Utility methods for numeric primitives. + /// + internal static class NumberUtils + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static float ClampFloat(float value, float min, float max) + { + if (value >= max) + { + return max; + } + + if (value <= min) + { + return min; + } + + return value; + } + } +}