using System; namespace Prism.UnitTests { public class GradientTableTests : UnitTestSharp.TestFixture { private static Gradient BlackToWhite() { return Gradient.Between(Colors.Black, Colors.White); } private static Gradient BlackUntilHalfThenWhite() { return new Gradient( GradientSpace.Hsv, new GradientStop(0, Colors.Black), new GradientStop(0.5, Colors.Black), new GradientStop(0.5, Colors.White), new GradientStop(1, Colors.White)); } public static int LevelsApart(int first, int second) { Color left = Color.FromArgb(first); Color right = Color.FromArgb(second); return Math.Max( Math.Max(Math.Abs(left.Red - right.Red), Math.Abs(left.Green - right.Green)), Math.Max(Math.Abs(left.Blue - right.Blue), Math.Abs(left.Alpha - right.Alpha))); } public class CtorTests : UnitTestSharp.TestFixture { private static Gradient Fire() { return new Gradient( GradientSpace.LinearLight, new GradientStop(0, Colors.White), new GradientStop(0.3, Colors.Orange), new GradientStop(0.7, Color.FromArgb(120, 20, 10)), new GradientStop(1, Colors.Black)); } public void ANullGradient_Throws() { CheckThrow(typeof(ArgumentNullException)); new GradientTable(null); } public void AConstantGradient_HasKnotsAtJustTheEnds() { CheckEqual(2, new GradientTable(Gradient.Constant(Colors.Red)).Count); } public void AGradientThatIsALineBetweenItsEnds_HasKnotsAtJustTheEnds() { CheckEqual(2, new GradientTable(Gradient.Between(Colors.Black, Colors.White, GradientSpace.Hsv)).Count); } public void ACurvedGradient_HasMoreKnots() { CheckEqual(15, new GradientTable(BlackToWhite()).Count); } public void ACurvedGradient_StaysUnderToleratedLevelsFromTheUnroundedGradient() { Gradient fire = Fire(); var table = new GradientTable(fire); foreach (double position in new[] { 0.01, 0.05, 0.15, 0.29, 0.4, 0.69, 0.8, 0.99 }) { CheckTrue(LevelsApart(fire.Evaluate(position).ToArgb(), table.Sample(position)) <= GradientTable.ToleratedLevels); } } public void ABumpBetweenTwoMidpointsOnTheSameLine_IsKept() { var pulse = new Gradient( GradientSpace.Hsv, new GradientStop(0, Colors.Black), new GradientStop(0.25, Colors.White), new GradientStop(0.5, Colors.Black), new GradientStop(1, Colors.Black)); var table = new GradientTable(pulse); CheckEqual(4, table.Count); CheckEqual(Colors.White.ToArgb(), table.Sample(0.25)); } public void AStartPastZeroAndAnEndBeforeOne_AddKnotsAtZeroAndOne() { var gradient = new Gradient( GradientSpace.Hsv, new GradientStop(0.25, Colors.Red), new GradientStop(0.75, Colors.Red)); CheckEqual(4, new GradientTable(gradient).Count); } public void AHardEdge_HasTwoKnotsAtTheSamePosition() { CheckEqual(4, new GradientTable(BlackUntilHalfThenWhite()).Count); } public void AJumpInsideASpan_StopsHalvingAtTheDepthLimit() { var gradient = Gradient.Between(Color.FromArgb(0, 255, 0, 0), Colors.Blue, GradientSpace.Hsv); CheckEqual(14, new GradientTable(gradient).Count); } } public class CountTests : UnitTestSharp.TestFixture { public void IsTheNumberOfKnots() { CheckEqual(15, new GradientTable(BlackToWhite()).Count); } } public class SampleTests : UnitTestSharp.TestFixture { private static readonly GradientTable BlackToWhiteInHsv = new GradientTable( Gradient.Between(Colors.Black, Colors.White, GradientSpace.Hsv)); public void TheEnds_AreTheGradientsFirstAndLastColors() { CheckEqual(Colors.Black.ToArgb(), BlackToWhiteInHsv.Sample(0)); CheckEqual(Colors.White.ToArgb(), BlackToWhiteInHsv.Sample(1)); } public void ABetweenPosition_BlendsTheNeighboringKnots() { CheckEqual(Color.FromArgb(64, 64, 64).ToArgb(), BlackToWhiteInHsv.Sample(0.25)); } public void AFallingChannel_BlendsDown() { var table = new GradientTable(Gradient.Between(Colors.Yellow, Colors.Red, GradientSpace.Hsv)); CheckEqual(Color.FromArgb(255, 191, 0).ToArgb(), table.Sample(0.25)); } public void AlphaBlendsOnItsOwn() { var table = new GradientTable(Gradient.Between( Color.FromArgb(255, 128, 128, 128), Color.FromArgb(0, 128, 128, 128), GradientSpace.Hsv)); CheckEqual(Color.FromArgb(128, 128, 128, 128).ToArgb(), table.Sample(0.5)); } public void AtAStop_IsThatStopsColor() { var table = new GradientTable(new Gradient( GradientSpace.LinearLight, new GradientStop(0.2, Colors.Red), new GradientStop(0.6, Colors.Blue))); CheckEqual(Colors.Red.ToArgb(), table.Sample(0.2)); CheckEqual(Colors.Blue.ToArgb(), table.Sample(0.6)); } public void BeforeTheFirstStop_IsTheFirstColor() { var table = new GradientTable(new Gradient( GradientSpace.LinearLight, new GradientStop(0.2, Colors.Red), new GradientStop(0.6, Colors.Blue))); CheckEqual(Colors.Red.ToArgb(), table.Sample(0)); CheckEqual(Colors.Red.ToArgb(), table.Sample(0.1)); } public void AfterTheLastStop_IsTheLastColor() { var table = new GradientTable(new Gradient( GradientSpace.LinearLight, new GradientStop(0.2, Colors.Red), new GradientStop(0.6, Colors.Blue))); CheckEqual(Colors.Blue.ToArgb(), table.Sample(0.9)); CheckEqual(Colors.Blue.ToArgb(), table.Sample(1)); } public void AHardEdge_IsExactlyAtItsPosition() { var table = new GradientTable(BlackUntilHalfThenWhite()); CheckEqual(Colors.Black.ToArgb(), table.Sample(0.4999999)); CheckEqual(Colors.White.ToArgb(), table.Sample(0.5)); } public void PositionsOutsideZeroToOne_AreClamped() { CheckEqual(Colors.Black.ToArgb(), BlackToWhiteInHsv.Sample(-4)); CheckEqual(Colors.White.ToArgb(), BlackToWhiteInHsv.Sample(4)); } public void NaN_IsTheFirstColor() { CheckEqual(Colors.Black.ToArgb(), BlackToWhiteInHsv.Sample(double.NaN)); } } } }