diff --git a/src/ImageSharp/Processing/Extensions/LightnessExtension.cs b/src/ImageSharp/Processing/Extensions/LightnessExtension.cs deleted file mode 100644 index cbe4a8d78..000000000 --- a/src/ImageSharp/Processing/Extensions/LightnessExtension.cs +++ /dev/null @@ -1,34 +0,0 @@ -// Copyright (c) Six Labors and contributors. -// Licensed under the Apache License, Version 2.0. -using SixLabors.ImageSharp.Processing.Processors.Filters; -using SixLabors.Primitives; - -namespace SixLabors.ImageSharp.Processing -{ - /// - /// Defines extensions that allow to change image lightness. - /// - public static class LightnessExtension - { - /// - /// Alters the lightness parameter of the image. - /// - /// The image this method extends. - /// Lightness parameter of image. - /// The to allow chaining of operations. - public static IImageProcessingContext Lightness(this IImageProcessingContext source, float lightness) - => source.ApplyProcessor(new LightnessProcessor(lightness)); - - /// - /// Alters the lightness parameter of the image. - /// - /// The image this method extends. - /// Lightness parameter of image. - /// - /// The structure that specifies the portion of the image object to alter. - /// - /// The to allow chaining of operations. - public static IImageProcessingContext Lightness(this IImageProcessingContext source, float lightness, Rectangle rectangle) - => source.ApplyProcessor(new LightnessProcessor(lightness), rectangle); - } -} diff --git a/src/ImageSharp/Processing/Extensions/LightnessExtensions.cs b/src/ImageSharp/Processing/Extensions/LightnessExtensions.cs new file mode 100644 index 000000000..86db9509e --- /dev/null +++ b/src/ImageSharp/Processing/Extensions/LightnessExtensions.cs @@ -0,0 +1,44 @@ +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +using SixLabors.ImageSharp.Processing.Processors.Filters; +using SixLabors.Primitives; + +namespace SixLabors.ImageSharp.Processing +{ + /// + /// Defines extensions that allow the alteration of the lightness component of an + /// using Mutate/Clone. + /// + public static class LightnessExtensions + { + /// + /// Alters the lightness component of the image. + /// + /// + /// A value of 0 will create an image that is completely black. A value of 1 leaves the input unchanged. + /// Other values are linear multipliers on the effect. Values of an amount over 1 are allowed, providing lighter results. + /// + /// The image this method extends. + /// The proportion of the conversion. Must be greater than or equal to 0. + /// The to allow chaining of operations. + public static IImageProcessingContext Lightness(this IImageProcessingContext source, float amount) + => source.ApplyProcessor(new LightnessProcessor(amount)); + + /// + /// Alters the lightness component of the image. + /// + /// + /// A value of 0 will create an image that is completely black. A value of 1 leaves the input unchanged. + /// Other values are linear multipliers on the effect. Values of an amount over 1 are allowed, providing lighter results. + /// + /// The image this method extends. + /// The proportion of the conversion. Must be greater than or equal to 0. + /// + /// The structure that specifies the portion of the image object to alter. + /// + /// The to allow chaining of operations. + public static IImageProcessingContext Lightness(this IImageProcessingContext source, float amount, Rectangle rectangle) + => source.ApplyProcessor(new LightnessProcessor(amount), rectangle); + } +} diff --git a/src/ImageSharp/Processing/KnownFilterMatrices.cs b/src/ImageSharp/Processing/KnownFilterMatrices.cs index 4a325503f..31b19433c 100644 --- a/src/ImageSharp/Processing/KnownFilterMatrices.cs +++ b/src/ImageSharp/Processing/KnownFilterMatrices.cs @@ -436,23 +436,26 @@ namespace SixLabors.ImageSharp.Processing /// Create a lightness filter matrix using the given amount. /// /// - /// A value of -1 will create an image that is completely black. A value of 1 makes the image completely white. + /// A value of 0 will create an image that is completely black. A value of 1 leaves the input unchanged. + /// Other values are linear multipliers on the effect. Values of an amount over 1 are allowed, providing lighter results. /// - /// The proportion of the conversion. Must be between -1 and 1. + /// The proportion of the conversion. Must be greater than or equal to 0. /// The public static ColorMatrix CreateLightnessFilter(float amount) { - Guard.MustBeBetweenOrEqualTo(amount, -1F, 1F, nameof(amount)); + Guard.MustBeGreaterThanOrEqualTo(amount, 0, nameof(amount)); + amount--; + return new ColorMatrix - { - M11 = 1F, - M22 = 1F, - M33 = 1F, - M44 = 1F, - M51 = amount, - M52 = amount, - M53 = amount - }; + { + M11 = 1F, + M22 = 1F, + M33 = 1F, + M44 = 1F, + M51 = amount, + M52 = amount, + M53 = amount + }; } /// diff --git a/src/ImageSharp/Processing/Processors/Filters/LightnessProcessor.cs b/src/ImageSharp/Processing/Processors/Filters/LightnessProcessor.cs index 4f3d332d4..49be3b6a6 100644 --- a/src/ImageSharp/Processing/Processors/Filters/LightnessProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Filters/LightnessProcessor.cs @@ -4,23 +4,27 @@ namespace SixLabors.ImageSharp.Processing.Processors.Filters { /// - /// Applies a lightness filter matrix using + /// Applies a lightness filter matrix using the given amount. /// public sealed class LightnessProcessor : FilterProcessor { /// /// Initializes a new instance of the class. /// - /// Lightness of image - public LightnessProcessor(float lightness) - : base(KnownFilterMatrices.CreateLightnessFilter(lightness)) + /// + /// A value of 0 will create an image that is completely black. A value of 1 leaves the input unchanged. + /// Other values are linear multipliers on the effect. Values of an amount over 1 are allowed, providing lighter results. + /// + /// The proportion of the conversion. Must be greater than or equal to 0. + public LightnessProcessor(float amount) + : base(KnownFilterMatrices.CreateLightnessFilter(amount)) { - this.Lightness = lightness; + this.Amount = amount; } /// - /// Gets Lightness of image. + /// Gets the proportion of the conversion /// - public float Lightness { get; } + public float Amount { get; } } } diff --git a/tests/ImageSharp.Tests/Processing/Filters/LightnessTest.cs b/tests/ImageSharp.Tests/Processing/Filters/LightnessTest.cs index e8a378e35..29d1d63e6 100644 --- a/tests/ImageSharp.Tests/Processing/Filters/LightnessTest.cs +++ b/tests/ImageSharp.Tests/Processing/Filters/LightnessTest.cs @@ -15,7 +15,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Effects this.operations.Lightness(.5F); LightnessProcessor processor = this.Verify(); - Assert.Equal(.5F, processor.Lightness); + Assert.Equal(.5F, processor.Amount); } [Fact] @@ -24,7 +24,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Effects this.operations.Lightness(.5F, this.rect); LightnessProcessor processor = this.Verify(this.rect); - Assert.Equal(.5F, processor.Lightness); + Assert.Equal(.5F, processor.Amount); } } } diff --git a/tests/ImageSharp.Tests/Processing/Processors/Filters/LightnessTest.cs b/tests/ImageSharp.Tests/Processing/Processors/Filters/LightnessTest.cs index f18642d10..c330ed6d9 100644 --- a/tests/ImageSharp.Tests/Processing/Processors/Filters/LightnessTest.cs +++ b/tests/ImageSharp.Tests/Processing/Processors/Filters/LightnessTest.cs @@ -19,7 +19,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Effects = new TheoryData { .5F, - .5F + 1.5F }; [Theory] @@ -27,4 +27,4 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Effects public void ApplyLightnessFilter(TestImageProvider provider, float value) where TPixel : struct, IPixel => provider.RunValidatingProcessorTest(ctx => ctx.Lightness(value), value, this.imageComparer); } -} \ No newline at end of file +} diff --git a/tests/Images/External b/tests/Images/External index 99a2bc523..58b2c01f9 160000 --- a/tests/Images/External +++ b/tests/Images/External @@ -1 +1 @@ -Subproject commit 99a2bc523cd4eb00e37af20d1b2088fa11564c57 +Subproject commit 58b2c01f9b66dd42d2b5b040b85e6846083b5e5f