mirror of https://github.com/SixLabors/ImageSharp
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
119 lines
4.4 KiB
119 lines
4.4 KiB
// <copyright file="ColorConversionTests.cs" company="James Jackson-South">
|
|
// Copyright (c) James Jackson-South and contributors.
|
|
// Licensed under the Apache License, Version 2.0.
|
|
// </copyright>
|
|
|
|
namespace ImageSharp.Tests.Drawing
|
|
{
|
|
using System.IO;
|
|
using Xunit;
|
|
using Drawing;
|
|
using ImageSharp.Drawing;
|
|
using System.Numerics;
|
|
|
|
using ImageSharp.PixelFormats;
|
|
|
|
using SixLabors.Shapes;
|
|
|
|
public class SolidComplexPolygonTests : FileTestBase
|
|
{
|
|
[Fact]
|
|
public void ImageShouldBeOverlayedByPolygonOutline()
|
|
{
|
|
string path = this.CreateOutputDirectory("Drawing", "ComplexPolygon");
|
|
Polygon simplePath = new Polygon(new LinearLineSegment(
|
|
new Vector2(10, 10),
|
|
new Vector2(200, 150),
|
|
new Vector2(50, 300)));
|
|
|
|
Polygon hole1 = new Polygon(new LinearLineSegment(
|
|
new Vector2(37, 85),
|
|
new Vector2(93, 85),
|
|
new Vector2(65, 137)));
|
|
IPath clipped = simplePath.Clip(hole1);
|
|
// var clipped = new Rectangle(10, 10, 100, 100).Clip(new Rectangle(20, 0, 20, 20));
|
|
using (Image<Rgba32> image = new Image<Rgba32>(500, 500))
|
|
{
|
|
image
|
|
.BackgroundColor(Rgba32.Blue)
|
|
.Fill(Rgba32.HotPink, clipped)
|
|
.Save($"{path}/Simple.png");
|
|
|
|
using (PixelAccessor<Rgba32> sourcePixels = image.Lock())
|
|
{
|
|
Assert.Equal(Rgba32.HotPink, sourcePixels[20, 35]);
|
|
|
|
//inside hole
|
|
Assert.Equal(Rgba32.Blue, sourcePixels[60, 100]);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
[Fact]
|
|
public void ImageShouldBeOverlayedPolygonOutlineWithOverlap()
|
|
{
|
|
string path = this.CreateOutputDirectory("Drawing", "ComplexPolygon");
|
|
Polygon simplePath = new Polygon(new LinearLineSegment(
|
|
new Vector2(10, 10),
|
|
new Vector2(200, 150),
|
|
new Vector2(50, 300)));
|
|
|
|
Polygon hole1 = new Polygon(new LinearLineSegment(
|
|
new Vector2(37, 85),
|
|
new Vector2(130, 40),
|
|
new Vector2(65, 137)));
|
|
|
|
using (Image<Rgba32> image = new Image<Rgba32>(500, 500))
|
|
{
|
|
image
|
|
.BackgroundColor(Rgba32.Blue)
|
|
.Fill(Rgba32.HotPink, simplePath.Clip(hole1))
|
|
.Save($"{path}/SimpleOverlapping.png");
|
|
|
|
using (PixelAccessor<Rgba32> sourcePixels = image.Lock())
|
|
{
|
|
Assert.Equal(Rgba32.HotPink, sourcePixels[20, 35]);
|
|
|
|
//inside hole
|
|
Assert.Equal(Rgba32.Blue, sourcePixels[60, 100]);
|
|
}
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void ImageShouldBeOverlayedPolygonOutlineWithOpacity()
|
|
{
|
|
string path = this.CreateOutputDirectory("Drawing", "ComplexPolygon");
|
|
Polygon simplePath = new Polygon(new LinearLineSegment(
|
|
new Vector2(10, 10),
|
|
new Vector2(200, 150),
|
|
new Vector2(50, 300)));
|
|
|
|
Polygon hole1 = new Polygon(new LinearLineSegment(
|
|
new Vector2(37, 85),
|
|
new Vector2(93, 85),
|
|
new Vector2(65, 137)));
|
|
Rgba32 color = new Rgba32(Rgba32.HotPink.R, Rgba32.HotPink.G, Rgba32.HotPink.B, 150);
|
|
|
|
using (Image<Rgba32> image = new Image<Rgba32>(500, 500))
|
|
{
|
|
image
|
|
.BackgroundColor(Rgba32.Blue)
|
|
.Fill(color, simplePath.Clip(hole1))
|
|
.Save($"{path}/Opacity.png");
|
|
|
|
//shift background color towards forground color by the opacity amount
|
|
Rgba32 mergedColor = new Rgba32(Vector4.Lerp(Rgba32.Blue.ToVector4(), Rgba32.HotPink.ToVector4(), 150f / 255f));
|
|
|
|
using (PixelAccessor<Rgba32> sourcePixels = image.Lock())
|
|
{
|
|
Assert.Equal(mergedColor, sourcePixels[20, 35]);
|
|
|
|
//inside hole
|
|
Assert.Equal(Rgba32.Blue, sourcePixels[60, 100]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|