Browse Source

Fix incorrect gradient color-stop calculation (#864)

* Fix incorrect gradient color-stop calculation

* Update submodule

* Add multi-stop gradient test

* Add missing reference image
pull/879/head
Poyo 7 years ago
committed by James Jackson-South
parent
commit
a7f9a8e9e1
  1. 2
      src/ImageSharp.Drawing/Processing/GradientBrushBase{TPixel}.cs
  2. 47
      tests/ImageSharp.Tests/Drawing/FillLinearGradientBrushTests.cs
  3. 2
      tests/Images/External

2
src/ImageSharp.Drawing/Processing/GradientBrushBase{TPixel}.cs

@ -121,7 +121,7 @@ namespace SixLabors.ImageSharp.Processing
{
var fromAsVector = from.Color.ToVector4();
var toAsVector = to.Color.ToVector4();
float onLocalGradient = (positionOnCompleteGradient - from.Ratio) / to.Ratio;
float onLocalGradient = (positionOnCompleteGradient - from.Ratio) / (to.Ratio - from.Ratio);
// TODO: this should be changeble for different gradienting functions
Vector4 result = PorterDuffFunctions.NormalSrcOver(

47
tests/ImageSharp.Tests/Drawing/FillLinearGradientBrushTests.cs

@ -350,5 +350,52 @@ namespace SixLabors.ImageSharp.Tests.Drawing
false,
false);
}
[Theory]
[WithBlankImages(200, 200, PixelTypes.Rgba32, 0, 0, 199, 199, new[] { 0f, .25f, .5f, .75f, 1f }, new[] { 0, 1, 2, 3, 4 })]
public void MultiplePointGradients<TPixel>(
TestImageProvider<TPixel> provider,
int startX, int startY,
int endX, int endY,
float[] stopPositions,
int[] stopColorCodes)
where TPixel : struct, IPixel<TPixel>
{
TPixel[] colors =
{
NamedColors<TPixel>.Black, NamedColors<TPixel>.Blue, NamedColors<TPixel>.Red,
NamedColors<TPixel>.White, NamedColors<TPixel>.Lime
};
var coloringVariant = new StringBuilder();
var colorStops = new ColorStop<TPixel>[stopPositions.Length];
for (int i = 0; i < stopPositions.Length; i++)
{
TPixel color = colors[stopColorCodes[i % colors.Length]];
float position = stopPositions[i];
colorStops[i] = new ColorStop<TPixel>(position, color);
Rgba32 rgba = default;
color.ToRgba32(ref rgba);
coloringVariant.AppendFormat(CultureInfo.InvariantCulture, "{0}@{1};", rgba.ToHex(), position);
}
FormattableString variant = $"({startX},{startY})_TO_({endX},{endY})__[{coloringVariant}]";
provider.VerifyOperation(
image =>
{
var unicolorLinearGradientBrush = new LinearGradientBrush<TPixel>(
new SixLabors.Primitives.Point(startX, startY),
new SixLabors.Primitives.Point(endX, endY),
GradientRepetitionMode.None,
colorStops);
image.Mutate(x => x.Fill(unicolorLinearGradientBrush));
},
variant,
false,
false);
}
}
}

2
tests/Images/External

@ -1 +1 @@
Subproject commit 9d71985545bb827270ca34af3f78e4c220560ef1
Subproject commit bc2765fb949d5d4ddba289b29174022bb94646de
Loading…
Cancel
Save