Browse Source

Fix up code and tests

af/merge-core
James Jackson-South 7 years ago
parent
commit
ccaaf402e4
  1. 34
      src/ImageSharp/Processing/Extensions/LightnessExtension.cs
  2. 44
      src/ImageSharp/Processing/Extensions/LightnessExtensions.cs
  3. 27
      src/ImageSharp/Processing/KnownFilterMatrices.cs
  4. 18
      src/ImageSharp/Processing/Processors/Filters/LightnessProcessor.cs
  5. 4
      tests/ImageSharp.Tests/Processing/Filters/LightnessTest.cs
  6. 4
      tests/ImageSharp.Tests/Processing/Processors/Filters/LightnessTest.cs
  7. 2
      tests/Images/External

34
src/ImageSharp/Processing/Extensions/LightnessExtension.cs

@ -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
{
/// <summary>
/// Defines extensions that allow to change image lightness.
/// </summary>
public static class LightnessExtension
{
/// <summary>
/// Alters the lightness parameter of the image.
/// </summary>
/// <param name="source">The image this method extends.</param>
/// <param name="lightness">Lightness parameter of image.</param>
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
public static IImageProcessingContext Lightness(this IImageProcessingContext source, float lightness)
=> source.ApplyProcessor(new LightnessProcessor(lightness));
/// <summary>
/// Alters the lightness parameter of the image.
/// </summary>
/// <param name="source">The image this method extends.</param>
/// <param name="lightness">Lightness parameter of image.</param>
/// <param name="rectangle">
/// The <see cref="Rectangle"/> structure that specifies the portion of the image object to alter.
/// </param>
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
public static IImageProcessingContext Lightness(this IImageProcessingContext source, float lightness, Rectangle rectangle)
=> source.ApplyProcessor(new LightnessProcessor(lightness), rectangle);
}
}

44
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
{
/// <summary>
/// Defines extensions that allow the alteration of the lightness component of an <see cref="Image"/>
/// using Mutate/Clone.
/// </summary>
public static class LightnessExtensions
{
/// <summary>
/// Alters the lightness component of the image.
/// </summary>
/// <remarks>
/// 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.
/// </remarks>
/// <param name="source">The image this method extends.</param>
/// <param name="amount">The proportion of the conversion. Must be greater than or equal to 0.</param>
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
public static IImageProcessingContext Lightness(this IImageProcessingContext source, float amount)
=> source.ApplyProcessor(new LightnessProcessor(amount));
/// <summary>
/// Alters the lightness component of the image.
/// </summary>
/// <remarks>
/// 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.
/// </remarks>
/// <param name="source">The image this method extends.</param>
/// <param name="amount">The proportion of the conversion. Must be greater than or equal to 0.</param>
/// <param name="rectangle">
/// The <see cref="Rectangle"/> structure that specifies the portion of the image object to alter.
/// </param>
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
public static IImageProcessingContext Lightness(this IImageProcessingContext source, float amount, Rectangle rectangle)
=> source.ApplyProcessor(new LightnessProcessor(amount), rectangle);
}
}

27
src/ImageSharp/Processing/KnownFilterMatrices.cs

@ -436,23 +436,26 @@ namespace SixLabors.ImageSharp.Processing
/// Create a lightness filter matrix using the given amount. /// Create a lightness filter matrix using the given amount.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// 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.
/// </remarks> /// </remarks>
/// <param name="amount">The proportion of the conversion. Must be between -1 and 1.</param> /// <param name="amount">The proportion of the conversion. Must be greater than or equal to 0.</param>
/// <returns>The <see cref="ColorMatrix"/></returns> /// <returns>The <see cref="ColorMatrix"/></returns>
public static ColorMatrix CreateLightnessFilter(float amount) public static ColorMatrix CreateLightnessFilter(float amount)
{ {
Guard.MustBeBetweenOrEqualTo(amount, -1F, 1F, nameof(amount)); Guard.MustBeGreaterThanOrEqualTo(amount, 0, nameof(amount));
amount--;
return new ColorMatrix return new ColorMatrix
{ {
M11 = 1F, M11 = 1F,
M22 = 1F, M22 = 1F,
M33 = 1F, M33 = 1F,
M44 = 1F, M44 = 1F,
M51 = amount, M51 = amount,
M52 = amount, M52 = amount,
M53 = amount M53 = amount
}; };
} }
/// <summary> /// <summary>

18
src/ImageSharp/Processing/Processors/Filters/LightnessProcessor.cs

@ -4,23 +4,27 @@
namespace SixLabors.ImageSharp.Processing.Processors.Filters namespace SixLabors.ImageSharp.Processing.Processors.Filters
{ {
/// <summary> /// <summary>
/// Applies a lightness filter matrix using /// Applies a lightness filter matrix using the given amount.
/// </summary> /// </summary>
public sealed class LightnessProcessor : FilterProcessor public sealed class LightnessProcessor : FilterProcessor
{ {
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="LightnessProcessor"/> class. /// Initializes a new instance of the <see cref="LightnessProcessor"/> class.
/// </summary> /// </summary>
/// <param name="lightness">Lightness of image</param> /// <remarks>
public LightnessProcessor(float lightness) /// A value of <value>0</value> will create an image that is completely black. A value of <value>1</value> leaves the input unchanged.
: base(KnownFilterMatrices.CreateLightnessFilter(lightness)) /// Other values are linear multipliers on the effect. Values of an amount over 1 are allowed, providing lighter results.
/// </remarks>
/// <param name="amount">The proportion of the conversion. Must be greater than or equal to 0.</param>
public LightnessProcessor(float amount)
: base(KnownFilterMatrices.CreateLightnessFilter(amount))
{ {
this.Lightness = lightness; this.Amount = amount;
} }
/// <summary> /// <summary>
/// Gets Lightness of image. /// Gets the proportion of the conversion
/// </summary> /// </summary>
public float Lightness { get; } public float Amount { get; }
} }
} }

4
tests/ImageSharp.Tests/Processing/Filters/LightnessTest.cs

@ -15,7 +15,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Effects
this.operations.Lightness(.5F); this.operations.Lightness(.5F);
LightnessProcessor processor = this.Verify<LightnessProcessor>(); LightnessProcessor processor = this.Verify<LightnessProcessor>();
Assert.Equal(.5F, processor.Lightness); Assert.Equal(.5F, processor.Amount);
} }
[Fact] [Fact]
@ -24,7 +24,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Effects
this.operations.Lightness(.5F, this.rect); this.operations.Lightness(.5F, this.rect);
LightnessProcessor processor = this.Verify<LightnessProcessor>(this.rect); LightnessProcessor processor = this.Verify<LightnessProcessor>(this.rect);
Assert.Equal(.5F, processor.Lightness); Assert.Equal(.5F, processor.Amount);
} }
} }
} }

4
tests/ImageSharp.Tests/Processing/Processors/Filters/LightnessTest.cs

@ -19,7 +19,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Effects
= new TheoryData<float> = new TheoryData<float>
{ {
.5F, .5F,
.5F 1.5F
}; };
[Theory] [Theory]
@ -27,4 +27,4 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Effects
public void ApplyLightnessFilter<TPixel>(TestImageProvider<TPixel> provider, float value) public void ApplyLightnessFilter<TPixel>(TestImageProvider<TPixel> provider, float value)
where TPixel : struct, IPixel<TPixel> => provider.RunValidatingProcessorTest(ctx => ctx.Lightness(value), value, this.imageComparer); where TPixel : struct, IPixel<TPixel> => provider.RunValidatingProcessorTest(ctx => ctx.Lightness(value), value, this.imageComparer);
} }
} }

2
tests/Images/External

@ -1 +1 @@
Subproject commit 99a2bc523cd4eb00e37af20d1b2088fa11564c57 Subproject commit 58b2c01f9b66dd42d2b5b040b85e6846083b5e5f
Loading…
Cancel
Save