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.
234 lines
10 KiB
234 lines
10 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;
|
|
using System.IO;
|
|
|
|
using ImageSharp.Drawing;
|
|
using ImageSharp.Drawing.Brushes;
|
|
|
|
using Xunit;
|
|
|
|
public class FillPatternBrushTests : FileTestBase
|
|
{
|
|
private void Test(string name, Rgba32 background, IBrush<Rgba32> brush, Rgba32[,] expectedPattern)
|
|
{
|
|
string path = this.CreateOutputDirectory("Fill", "PatternBrush");
|
|
using (Image image = new Image(20, 20))
|
|
{
|
|
image
|
|
.Fill(background)
|
|
.Fill(brush);
|
|
|
|
using (FileStream output = File.OpenWrite($"{path}/{name}.png"))
|
|
{
|
|
image.Save(output);
|
|
}
|
|
|
|
using (PixelAccessor<Rgba32> sourcePixels = image.Lock())
|
|
{
|
|
// lets pick random spots to start checking
|
|
Random r = new Random();
|
|
Fast2DArray<Rgba32> expectedPatternFast = new Fast2DArray<Rgba32>(expectedPattern);
|
|
int xStride = expectedPatternFast.Width;
|
|
int yStride = expectedPatternFast.Height;
|
|
int offsetX = r.Next(image.Width / xStride) * xStride;
|
|
int offsetY = r.Next(image.Height / yStride) * yStride;
|
|
for (int x = 0; x < xStride; x++)
|
|
{
|
|
for (int y = 0; y < yStride; y++)
|
|
{
|
|
int actualX = x + offsetX;
|
|
int actualY = y + offsetY;
|
|
Rgba32 expected = expectedPatternFast[y, x]; // inverted pattern
|
|
Rgba32 actual = sourcePixels[actualX, actualY];
|
|
if (expected != actual)
|
|
{
|
|
Assert.True(false, $"Expected {expected} but found {actual} at ({actualX},{actualY})");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
using (FileStream output = File.OpenWrite($"{path}/{name}x4.png"))
|
|
{
|
|
image.Resize(80, 80).Save(output);
|
|
}
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void ImageShouldBeFloodFilledWithPercent10()
|
|
{
|
|
this.Test("Percent10", Rgba32.Blue, Brushes.Percent10(Rgba32.HotPink, Rgba32.LimeGreen),
|
|
new[,]
|
|
{
|
|
{ Rgba32.HotPink , Rgba32.LimeGreen, Rgba32.LimeGreen, Rgba32.LimeGreen},
|
|
{ Rgba32.LimeGreen, Rgba32.LimeGreen, Rgba32.LimeGreen, Rgba32.LimeGreen},
|
|
{ Rgba32.LimeGreen, Rgba32.LimeGreen, Rgba32.HotPink , Rgba32.LimeGreen},
|
|
{ Rgba32.LimeGreen, Rgba32.LimeGreen, Rgba32.LimeGreen, Rgba32.LimeGreen}
|
|
});
|
|
}
|
|
|
|
[Fact]
|
|
public void ImageShouldBeFloodFilledWithPercent10Transparent()
|
|
{
|
|
Test("Percent10_Transparent", Rgba32.Blue, Brushes.Percent10(Rgba32.HotPink),
|
|
new Rgba32[,] {
|
|
{ Rgba32.HotPink , Rgba32.Blue, Rgba32.Blue, Rgba32.Blue},
|
|
{ Rgba32.Blue, Rgba32.Blue, Rgba32.Blue, Rgba32.Blue},
|
|
{ Rgba32.Blue, Rgba32.Blue, Rgba32.HotPink , Rgba32.Blue},
|
|
{ Rgba32.Blue, Rgba32.Blue, Rgba32.Blue, Rgba32.Blue}
|
|
});
|
|
}
|
|
|
|
[Fact]
|
|
public void ImageShouldBeFloodFilledWithPercent20()
|
|
{
|
|
Test("Percent20", Rgba32.Blue, Brushes.Percent20(Rgba32.HotPink, Rgba32.LimeGreen),
|
|
new Rgba32[,] {
|
|
{ Rgba32.HotPink , Rgba32.LimeGreen, Rgba32.LimeGreen, Rgba32.LimeGreen},
|
|
{ Rgba32.LimeGreen, Rgba32.LimeGreen, Rgba32.HotPink , Rgba32.LimeGreen},
|
|
{ Rgba32.HotPink , Rgba32.LimeGreen, Rgba32.LimeGreen, Rgba32.LimeGreen},
|
|
{ Rgba32.LimeGreen, Rgba32.LimeGreen, Rgba32.HotPink , Rgba32.LimeGreen}
|
|
});
|
|
}
|
|
|
|
[Fact]
|
|
public void ImageShouldBeFloodFilledWithPercent20_transparent()
|
|
{
|
|
Test("Percent20_Transparent", Rgba32.Blue, Brushes.Percent20(Rgba32.HotPink),
|
|
new Rgba32[,] {
|
|
{ Rgba32.HotPink , Rgba32.Blue, Rgba32.Blue, Rgba32.Blue},
|
|
{ Rgba32.Blue, Rgba32.Blue, Rgba32.HotPink , Rgba32.Blue},
|
|
{ Rgba32.HotPink , Rgba32.Blue, Rgba32.Blue, Rgba32.Blue},
|
|
{ Rgba32.Blue, Rgba32.Blue, Rgba32.HotPink , Rgba32.Blue}
|
|
});
|
|
}
|
|
|
|
[Fact]
|
|
public void ImageShouldBeFloodFilledWithHorizontal()
|
|
{
|
|
Test("Horizontal", Rgba32.Blue, Brushes.Horizontal(Rgba32.HotPink, Rgba32.LimeGreen),
|
|
new Rgba32[,] {
|
|
{ Rgba32.LimeGreen , Rgba32.LimeGreen, Rgba32.LimeGreen, Rgba32.LimeGreen},
|
|
{ Rgba32.HotPink, Rgba32.HotPink, Rgba32.HotPink , Rgba32.HotPink},
|
|
{ Rgba32.LimeGreen , Rgba32.LimeGreen, Rgba32.LimeGreen, Rgba32.LimeGreen},
|
|
{ Rgba32.LimeGreen, Rgba32.LimeGreen, Rgba32.LimeGreen , Rgba32.LimeGreen}
|
|
});
|
|
}
|
|
|
|
[Fact]
|
|
public void ImageShouldBeFloodFilledWithHorizontal_transparent()
|
|
{
|
|
Test("Horizontal_Transparent", Rgba32.Blue, Brushes.Horizontal(Rgba32.HotPink),
|
|
new Rgba32[,] {
|
|
{ Rgba32.Blue , Rgba32.Blue, Rgba32.Blue, Rgba32.Blue},
|
|
{ Rgba32.HotPink, Rgba32.HotPink, Rgba32.HotPink , Rgba32.HotPink},
|
|
{ Rgba32.Blue , Rgba32.Blue, Rgba32.Blue, Rgba32.Blue},
|
|
{ Rgba32.Blue, Rgba32.Blue, Rgba32.Blue , Rgba32.Blue}
|
|
});
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
public void ImageShouldBeFloodFilledWithMin()
|
|
{
|
|
Test("Min", Rgba32.Blue, Brushes.Min(Rgba32.HotPink, Rgba32.LimeGreen),
|
|
new Rgba32[,] {
|
|
{ Rgba32.LimeGreen , Rgba32.LimeGreen, Rgba32.LimeGreen, Rgba32.LimeGreen},
|
|
{ Rgba32.LimeGreen , Rgba32.LimeGreen, Rgba32.LimeGreen, Rgba32.LimeGreen},
|
|
{ Rgba32.LimeGreen, Rgba32.LimeGreen, Rgba32.LimeGreen , Rgba32.LimeGreen},
|
|
{ Rgba32.HotPink, Rgba32.HotPink, Rgba32.HotPink , Rgba32.HotPink}
|
|
});
|
|
}
|
|
|
|
[Fact]
|
|
public void ImageShouldBeFloodFilledWithMin_transparent()
|
|
{
|
|
Test("Min_Transparent", Rgba32.Blue, Brushes.Min(Rgba32.HotPink),
|
|
new Rgba32[,] {
|
|
{ Rgba32.Blue , Rgba32.Blue, Rgba32.Blue, Rgba32.Blue},
|
|
{ Rgba32.Blue , Rgba32.Blue, Rgba32.Blue, Rgba32.Blue},
|
|
{ Rgba32.Blue, Rgba32.Blue, Rgba32.Blue , Rgba32.Blue},
|
|
{ Rgba32.HotPink, Rgba32.HotPink, Rgba32.HotPink , Rgba32.HotPink},
|
|
});
|
|
}
|
|
|
|
[Fact]
|
|
public void ImageShouldBeFloodFilledWithVertical()
|
|
{
|
|
Test("Vertical", Rgba32.Blue, Brushes.Vertical(Rgba32.HotPink, Rgba32.LimeGreen),
|
|
new Rgba32[,] {
|
|
{ Rgba32.LimeGreen, Rgba32.HotPink, Rgba32.LimeGreen, Rgba32.LimeGreen},
|
|
{ Rgba32.LimeGreen, Rgba32.HotPink, Rgba32.LimeGreen, Rgba32.LimeGreen},
|
|
{ Rgba32.LimeGreen, Rgba32.HotPink, Rgba32.LimeGreen, Rgba32.LimeGreen},
|
|
{ Rgba32.LimeGreen, Rgba32.HotPink, Rgba32.LimeGreen, Rgba32.LimeGreen}
|
|
});
|
|
}
|
|
|
|
[Fact]
|
|
public void ImageShouldBeFloodFilledWithVertical_transparent()
|
|
{
|
|
Test("Vertical_Transparent", Rgba32.Blue, Brushes.Vertical(Rgba32.HotPink),
|
|
new Rgba32[,] {
|
|
{ Rgba32.Blue, Rgba32.HotPink, Rgba32.Blue, Rgba32.Blue},
|
|
{ Rgba32.Blue, Rgba32.HotPink, Rgba32.Blue, Rgba32.Blue},
|
|
{ Rgba32.Blue, Rgba32.HotPink, Rgba32.Blue, Rgba32.Blue},
|
|
{ Rgba32.Blue, Rgba32.HotPink, Rgba32.Blue, Rgba32.Blue}
|
|
});
|
|
}
|
|
|
|
[Fact]
|
|
public void ImageShouldBeFloodFilledWithForwardDiagonal()
|
|
{
|
|
Test("ForwardDiagonal", Rgba32.Blue, Brushes.ForwardDiagonal(Rgba32.HotPink, Rgba32.LimeGreen),
|
|
new Rgba32[,] {
|
|
{ Rgba32.LimeGreen, Rgba32.LimeGreen, Rgba32.LimeGreen, Rgba32.HotPink},
|
|
{ Rgba32.LimeGreen, Rgba32.LimeGreen, Rgba32.HotPink, Rgba32.LimeGreen},
|
|
{ Rgba32.LimeGreen, Rgba32.HotPink, Rgba32.LimeGreen, Rgba32.LimeGreen},
|
|
{ Rgba32.HotPink, Rgba32.LimeGreen, Rgba32.LimeGreen, Rgba32.LimeGreen}
|
|
});
|
|
}
|
|
|
|
[Fact]
|
|
public void ImageShouldBeFloodFilledWithForwardDiagonal_transparent()
|
|
{
|
|
Test("ForwardDiagonal_Transparent", Rgba32.Blue, Brushes.ForwardDiagonal(Rgba32.HotPink),
|
|
new Rgba32[,] {
|
|
{ Rgba32.Blue, Rgba32.Blue, Rgba32.Blue, Rgba32.HotPink},
|
|
{ Rgba32.Blue, Rgba32.Blue, Rgba32.HotPink, Rgba32.Blue},
|
|
{ Rgba32.Blue, Rgba32.HotPink, Rgba32.Blue, Rgba32.Blue},
|
|
{ Rgba32.HotPink, Rgba32.Blue, Rgba32.Blue, Rgba32.Blue}
|
|
});
|
|
}
|
|
|
|
[Fact]
|
|
public void ImageShouldBeFloodFilledWithBackwardDiagonal()
|
|
{
|
|
Test("BackwardDiagonal", Rgba32.Blue, Brushes.BackwardDiagonal(Rgba32.HotPink, Rgba32.LimeGreen),
|
|
new Rgba32[,] {
|
|
{ Rgba32.HotPink, Rgba32.LimeGreen, Rgba32.LimeGreen, Rgba32.LimeGreen},
|
|
{ Rgba32.LimeGreen, Rgba32.HotPink, Rgba32.LimeGreen, Rgba32.LimeGreen},
|
|
{ Rgba32.LimeGreen, Rgba32.LimeGreen, Rgba32.HotPink, Rgba32.LimeGreen},
|
|
{ Rgba32.LimeGreen, Rgba32.LimeGreen, Rgba32.LimeGreen, Rgba32.HotPink}
|
|
});
|
|
}
|
|
|
|
[Fact]
|
|
public void ImageShouldBeFloodFilledWithBackwardDiagonal_transparent()
|
|
{
|
|
Test("BackwardDiagonal_Transparent", Rgba32.Blue, Brushes.BackwardDiagonal(Rgba32.HotPink),
|
|
new Rgba32[,] {
|
|
{ Rgba32.HotPink, Rgba32.Blue, Rgba32.Blue, Rgba32.Blue},
|
|
{ Rgba32.Blue, Rgba32.HotPink, Rgba32.Blue, Rgba32.Blue},
|
|
{ Rgba32.Blue, Rgba32.Blue, Rgba32.HotPink, Rgba32.Blue},
|
|
{ Rgba32.Blue, Rgba32.Blue, Rgba32.Blue, Rgba32.HotPink}
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|