Browse Source

Darken Lighten

af/merge-core
James Jackson-South 9 years ago
parent
commit
9c8bd2f178
  1. 36
      src/ImageSharp/Colors/ColorTransforms.cs
  2. 14
      tests/ImageSharp.Tests/Colors/ColorTransformTests.cs

36
src/ImageSharp/Colors/ColorTransforms.cs

@ -150,6 +150,42 @@ namespace ImageSharp
return new Color(Pack(ref result));
}
/// <summary>
/// Selects the darker of the backdrop and source colors.
/// The backdrop is replaced with the source where the source is darker; otherwise, it is left unchanged.
/// </summary>
/// <param name="backdrop">The backdrop color.</param>
/// <param name="source">The source color.</param>
/// <returns>
/// The <see cref="Color"/>.
/// </returns>
public static Color Darken(Color backdrop, Color source)
{
Vector4 vb = backdrop.ToVector4();
Vector4 vs = source.ToVector4();
Vector4 result = Vector4.Min(vb, vs);
result.W = vb.W;
return new Color(Pack(ref result));
}
/// <summary>
/// Selects the lighter of the backdrop and source colors.
/// The backdrop is replaced with the source where the source is lighter; otherwise, it is left unchanged.
/// </summary>
/// <param name="backdrop">The backdrop color.</param>
/// <param name="source">The source color.</param>
/// <returns>
/// The <see cref="Color"/>.
/// </returns>
public static Color Lighten(Color backdrop, Color source)
{
Vector4 vb = backdrop.ToVector4();
Vector4 vs = source.ToVector4();
Vector4 result = Vector4.Max(vb, vs);
result.W = vb.W;
return new Color(Pack(ref result));
}
/// <summary>
/// Linearly interpolates from one color to another based on the given weighting.
/// </summary>

14
tests/ImageSharp.Tests/Colors/ColorTransformTests.cs

@ -56,5 +56,19 @@ namespace ImageSharp.Tests.Colors
Color overlay = Color.Overlay(Backdrop, Source);
Assert.True(overlay == new Color(153, 82, 0));
}
[Fact]
public void Darken()
{
Color darken = Color.Darken(Backdrop, Source);
Assert.True(darken == new Color(0, 102, 0));
}
[Fact]
public void Lighten()
{
Color lighten = Color.Lighten(Backdrop, Source);
Assert.True(lighten == new Color(204, 102, 153));
}
}
}

Loading…
Cancel
Save