using System; using UnitTestSharp; namespace Prism.Glass.UnitTests { public class HueDiscTests : TestFixture { private const double Tolerance = 1e-12; public static bool Near(double expected, double actual) { return Math.Abs(expected - actual) < Tolerance; } public class HueAtTests : TestFixture { public void TheRight_IsRed() { CheckTrue(Near(0, HueDisc.HueAt(1, 0))); } public void TheTop_IsAQuarterTurn() { CheckTrue(Near(0.25, HueDisc.HueAt(0, -1))); } public void TheLeft_IsAHalfTurn() { CheckTrue(Near(0.5, HueDisc.HueAt(-1, 0))); } public void TheBottom_IsThreeQuartersOfATurn() { CheckTrue(Near(0.75, HueDisc.HueAt(0, 1))); } public void TheUpperRight_IsAnEighthOfATurn() { CheckTrue(Near(0.125, HueDisc.HueAt(1, -1))); } public void TheDistanceFromTheCenter_DoesNotMatter() { CheckTrue(Near(HueDisc.HueAt(1, -1), HueDisc.HueAt(0.01, -0.01))); } public void TheCenter_IsZero() { CheckEqual(0.0, HueDisc.HueAt(0, 0)); } } public class SaturationAtTests : TestFixture { public void TheCenter_IsZero() { CheckEqual(0.0, HueDisc.SaturationAt(0, 0)); } public void InsideTheDisc_IsTheDistanceFromTheCenter() { CheckTrue(Near(0.5, HueDisc.SaturationAt(0.3, 0.4))); } public void TheRim_IsOne() { CheckTrue(Near(1, HueDisc.SaturationAt(0, -1))); } public void OutsideTheDisc_IsHeldToOne() { CheckEqual(1.0, HueDisc.SaturationAt(3, 4)); } } public class PickTests : TestFixture { public void TakesHueAndSaturationFromThePoint() { Hsv picked = HueDisc.Pick(0, -0.5, new Hsv(0.9, 0.1, 0.5, 0.25)); CheckTrue(Near(0.25, picked.Hue)); CheckTrue(Near(0.5, picked.Saturation)); } public void KeepsTheValueAndAlpha() { Hsv picked = HueDisc.Pick(0, -0.5, new Hsv(0.9, 0.1, 0.5, 0.25)); CheckEqual(0.5, picked.Value); CheckEqual(0.25, picked.Alpha); } public void TheCenter_KeepsTheCurrentHue() { Hsv picked = HueDisc.Pick(0, 0, new Hsv(0.9, 0.1, 0.5)); CheckEqual(0.9, picked.Hue); CheckEqual(0.0, picked.Saturation); } public void APointOutsideTheDisc_HoldsSaturationAtOne() { Hsv picked = HueDisc.Pick(2, 0, new Hsv(0.9, 0.1, 0.5)); CheckEqual(0.0, picked.Hue); CheckEqual(1.0, picked.Saturation); } } public class PointOfTests : TestFixture { public void RedAtFullSaturation_IsTheRightRim() { HueDisc.PointOf(new Hsv(0, 1, 1), out double x, out double y); CheckTrue(Near(1, x)); CheckTrue(Near(0, y)); } public void AQuarterTurnAtHalfSaturation_IsHalfwayUp() { HueDisc.PointOf(new Hsv(0.25, 0.5, 1), out double x, out double y); CheckTrue(Near(0, x)); CheckTrue(Near(-0.5, y)); } public void AHalfTurnAtFullSaturation_IsTheLeftRim() { HueDisc.PointOf(new Hsv(0.5, 1, 1), out double x, out double y); CheckTrue(Near(-1, x)); CheckTrue(Near(0, y)); } public void AGray_IsTheCenter() { HueDisc.PointOf(new Hsv(0.3, 0, 1), out double x, out double y); CheckEqual(0.0, x); CheckEqual(0.0, y); } public void PickingTheReturnedPoint_GivesTheColorBack() { var color = new Hsv(0.3, 0.6, 0.8, 0.5); HueDisc.PointOf(color, out double x, out double y); Hsv picked = HueDisc.Pick(x, y, color); CheckTrue(Near(color.Hue, picked.Hue)); CheckTrue(Near(color.Saturation, picked.Saturation)); } } public class FillTests : TestFixture { private static byte[] Disc(int diameter, double value) { var pixels = new byte[Rgba.ByteCount(diameter, diameter)]; HueDisc.Fill(pixels, diameter, value); return pixels; } public void TheCenterOfAnOddDisc_IsWhiteAtFullValue() { CheckEqual("#FFFFFFFF", PixelText.At(Disc(5, 1), width: 5, column: 2, row: 2)); } public void TheRightSide_IsRedWashedByTheSaturation() { CheckEqual("#FF3333FF", PixelText.At(Disc(5, 1), width: 5, column: 4, row: 2)); } public void TheLeftSide_IsCyanWashedByTheSaturation() { CheckEqual("#33FFFFFF", PixelText.At(Disc(5, 1), width: 5, column: 0, row: 2)); } public void TheTop_IsAYellowishGreenAtAQuarterTurn() { CheckEqual("#99FF33FF", PixelText.At(Disc(5, 1), width: 5, column: 2, row: 0)); } public void TheBottom_IsAVioletAtThreeQuartersOfATurn() { CheckEqual("#9933FFFF", PixelText.At(Disc(5, 1), width: 5, column: 2, row: 4)); } public void AQuarterValue_ScalesEveryChannel() { CheckEqual("#400D0DFF", PixelText.At(Disc(5, 0.25), width: 5, column: 4, row: 2)); } public void ZeroValue_IsBlackEverywhereInside() { CheckEqual("#000000FF", PixelText.At(Disc(5, 0), width: 5, column: 4, row: 2)); } public void ACornerPixelOnTheRim_IsPartlyCoveredAndHoldsFullSaturation() { CheckEqual("#00FF402C", PixelText.At(Disc(5, 1), width: 5, column: 0, row: 0)); } public void ACornerPixelFarOutside_IsTransparent() { CheckEqual("#00FF4000", PixelText.At(Disc(9, 1), width: 9, column: 0, row: 0)); } public void ABufferOfAnotherLength_Throws() { CheckThrow(typeof(ArgumentException)); HueDisc.Fill(new byte[10], diameter: 5, value: 1); } public void ADiameterOfZero_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); HueDisc.Fill(new byte[0], diameter: 0, value: 1); } public void AValueAboveOne_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); HueDisc.Fill(new byte[Rgba.ByteCount(5, 5)], diameter: 5, value: 2); } } } }