Browse Source

Fix #1044

af/merge-core
James Jackson-South 6 years ago
parent
commit
d91bda2946
  1. 8
      src/ImageSharp.Drawing/Processing/GradientBrush.cs
  2. 4
      src/ImageSharp/ImageSharp.csproj
  3. 44
      tests/ImageSharp.Tests/Drawing/FillLinearGradientBrushTests.cs
  4. 2
      tests/Images/External

8
src/ImageSharp.Drawing/Processing/GradientBrush.cs

@ -2,7 +2,7 @@
// Licensed under the Apache License, Version 2.0.
using System;
using System.Numerics;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.Primitives;
@ -120,10 +120,8 @@ namespace SixLabors.ImageSharp.Processing
float onLocalGradient = (positionOnCompleteGradient - from.Ratio) / (to.Ratio - from.Ratio);
// TODO: this should be changeble for different gradienting functions.
// TODO: Why not use Blender property?
return PixelOperations<TPixel>
.Instance.GetPixelBlender(PixelColorBlendingMode.Normal, PixelAlphaCompositionMode.SrcOver)
.Blend(from.Color.ToPixel<TPixel>(), to.Color.ToPixel<TPixel>(), onLocalGradient);
// TODO: Is the comment above still relevent?
return new Color(Vector4.Lerp((Vector4)from.Color, (Vector4)to.Color, onLocalGradient)).ToPixel<TPixel>();
}
}
}

4
src/ImageSharp/ImageSharp.csproj

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
@ -119,7 +119,7 @@
</ItemGroup>
<ItemGroup>
<!--<InternalsVisibleTo Include="SixLabors.ImageSharp.Drawing" />-->
<InternalsVisibleTo Include="SixLabors.ImageSharp.Drawing" />
</ItemGroup>
<ItemGroup>

44
tests/ImageSharp.Tests/Drawing/FillLinearGradientBrushTests.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 System;
@ -16,6 +16,8 @@ namespace SixLabors.ImageSharp.Tests.Drawing
{
using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison;
using SixLabors.Primitives;
using SixLabors.Shapes;
[GroupOutput("Drawing/GradientBrushes")]
public class FillLinearGradientBrushTests
@ -392,5 +394,43 @@ namespace SixLabors.ImageSharp.Tests.Drawing
false,
false);
}
[Theory]
[WithBlankImages(200, 200, PixelTypes.Rgba32)]
public void GradientsWithTransparencyOnExistingBackground<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel>
{
provider.VerifyOperation(
image =>
{
image.Mutate(i => i.Fill(Color.Red));
image.Mutate(ApplyGloss);
});
void ApplyGloss(IImageProcessingContext ctx)
{
Size size = ctx.GetCurrentSize();
IPathCollection glossPath = BuildGloss(size.Width, size.Height);
var graphicsOptions = new GraphicsOptions(true)
{
ColorBlendingMode = PixelColorBlendingMode.Normal,
AlphaCompositionMode = PixelAlphaCompositionMode.SrcAtop
};
var linearGradientBrush = new LinearGradientBrush(new Point(0, 0), new Point(0, size.Height / 2), GradientRepetitionMode.Repeat, new ColorStop(0, Color.White.WithAlpha(0.5f)), new ColorStop(1, Color.White.WithAlpha(0.25f)));
ctx.Fill(graphicsOptions, linearGradientBrush, glossPath);
}
IPathCollection BuildGloss(int imageWidth, int imageHeight)
{
var pathBuilder = new PathBuilder();
pathBuilder.AddLine(new PointF(0, 0), new PointF(imageWidth, 0));
pathBuilder.AddLine(new PointF(imageWidth, 0), new PointF(imageWidth, imageHeight * 0.4f));
pathBuilder.AddBezier(new PointF(imageWidth, imageHeight * 0.4f), new PointF(imageWidth / 2, imageHeight * 0.6f), new PointF(0, imageHeight * 0.4f));
pathBuilder.CloseFigure();
return new PathCollection(pathBuilder.Build());
}
}
}
}
}

2
tests/Images/External

@ -1 +1 @@
Subproject commit 563ec6f7774734ba39924174c8961705a1ea6fa2
Subproject commit f0c4033667bd23ad9dde82ccb625c232d402ee05
Loading…
Cancel
Save