using System; using UnitTestSharp; namespace Prism.Glass.UnitTests { public class ColorStripsTests : TestFixture { public class FillValueTests : TestFixture { private static byte[] Strip(int length, double hue, double saturation) { var pixels = new byte[Rgba.ByteCount(length, 1)]; ColorStrips.FillValue(pixels, length, hue, saturation); return pixels; } public void TheFirstPixel_IsBlack() { CheckEqual("#000000FF", PixelText.At(Strip(6, 0, 1), width: 6, column: 0, row: 0)); } public void TheLastPixel_IsTheColorAtFullValue() { CheckEqual("#00FFFFFF", PixelText.At(Strip(6, 0.5, 1), width: 6, column: 5, row: 0)); } public void InBetween_ValueRisesInEvenSteps() { byte[] pixels = Strip(6, 0, 1); CheckEqual("#330000FF", PixelText.At(pixels, width: 6, column: 1, row: 0)); CheckEqual("#660000FF", PixelText.At(pixels, width: 6, column: 2, row: 0)); CheckEqual("#990000FF", PixelText.At(pixels, width: 6, column: 3, row: 0)); CheckEqual("#CC0000FF", PixelText.At(pixels, width: 6, column: 4, row: 0)); } public void NoSaturation_IsAGrayRamp() { CheckEqual("#999999FF", PixelText.At(Strip(6, 0.5, 0), width: 6, column: 3, row: 0)); } public void ALengthOfOne_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); ColorStrips.FillValue(new byte[4], length: 1, hue: 0, saturation: 1); } public void ABufferOfAnotherLength_Throws() { CheckThrow(typeof(ArgumentException)); ColorStrips.FillValue(new byte[20], length: 6, hue: 0, saturation: 1); } public void ASaturationAboveOne_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); ColorStrips.FillValue(new byte[24], length: 6, hue: 0, saturation: 2); } } public class FillAlphaTests : TestFixture { private static byte[] Strip(int length) { var pixels = new byte[Rgba.ByteCount(length, 1)]; ColorStrips.FillAlpha(pixels, length, Color.FromArgb(128, 10, 20, 30)); return pixels; } public void TheFirstPixel_IsTransparent() { CheckEqual("#0A141E00", PixelText.At(Strip(6), width: 6, column: 0, row: 0)); } public void TheLastPixel_IsOpaqueDespiteTheColorsOwnAlpha() { CheckEqual("#0A141EFF", PixelText.At(Strip(6), width: 6, column: 5, row: 0)); } public void InBetween_AlphaRisesInEvenSteps() { byte[] pixels = Strip(6); CheckEqual("#0A141E33", PixelText.At(pixels, width: 6, column: 1, row: 0)); CheckEqual("#0A141E99", PixelText.At(pixels, width: 6, column: 3, row: 0)); } public void ALengthOfOne_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); ColorStrips.FillAlpha(new byte[4], length: 1, color: Color.FromArgb(1, 2, 3)); } public void ABufferOfAnotherLength_Throws() { CheckThrow(typeof(ArgumentException)); ColorStrips.FillAlpha(new byte[20], length: 6, color: Color.FromArgb(1, 2, 3)); } } public class FillGradientTests : TestFixture { private static readonly Color Red = Color.FromArgb(255, 0, 0); private static readonly Color Blue = Color.FromArgb(0, 0, 255); private static byte[] Strip(int length, Gradient gradient) { var pixels = new byte[Rgba.ByteCount(length, 1)]; ColorStrips.FillGradient(pixels, length, gradient); return pixels; } public void TheFirstAndLastPixels_AreTheFirstAndLastColors() { byte[] pixels = Strip(5, Gradient.Between(Red, Blue)); CheckEqual("#FF0000FF", PixelText.At(pixels, width: 5, column: 0, row: 0)); CheckEqual("#0000FFFF", PixelText.At(pixels, width: 5, column: 4, row: 0)); } public void InBetween_IsTheGradientAtThePixelsPlace() { var gradient = Gradient.Between(Red, Blue); byte[] pixels = Strip(5, gradient); CheckEqual( HexColor.Format(gradient.Evaluate(0.25), includeAlpha: true), PixelText.At(pixels, width: 5, column: 1, row: 0)); } public void AHardEdge_GoesToTheLaterColorAtThePixelItFallsOn() { var gradient = new Gradient( GradientSpace.LinearLight, new GradientStop(0.5, Red), new GradientStop(0.5, Blue)); byte[] pixels = Strip(5, gradient); CheckEqual("#FF0000FF", PixelText.At(pixels, width: 5, column: 1, row: 0)); CheckEqual("#0000FFFF", PixelText.At(pixels, width: 5, column: 2, row: 0)); } public void ATransparentStop_KeepsItsAlpha() { byte[] pixels = Strip(3, Gradient.Between(Color.FromArgb(0, 255, 0, 0), Red)); CheckEqual("#FF000000", PixelText.At(pixels, width: 3, column: 0, row: 0)); } public void WithoutAGradient_Throws() { CheckThrow(typeof(ArgumentNullException)); ColorStrips.FillGradient(new byte[8], length: 2, gradient: null); } public void ALengthOfOne_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); ColorStrips.FillGradient(new byte[4], length: 1, gradient: Gradient.Constant(Red)); } public void ABufferOfAnotherLength_Throws() { CheckThrow(typeof(ArgumentException)); ColorStrips.FillGradient(new byte[20], length: 6, gradient: Gradient.Constant(Red)); } } } }