From 40a75e62c0a542e204c34d511a2319c48cf04812 Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Tue, 26 Apr 2022 09:55:08 +0100 Subject: [PATCH] Add NextColorRgb and NextColorArgb --- X10D.Tests/src/Drawing/RandomTests.cs | 37 +++++++++++++++++++++++++++ X10D/src/Drawing/RandomExtensions.cs | 27 +++++++++++++------ 2 files changed, 56 insertions(+), 8 deletions(-) create mode 100644 X10D.Tests/src/Drawing/RandomTests.cs diff --git a/X10D.Tests/src/Drawing/RandomTests.cs b/X10D.Tests/src/Drawing/RandomTests.cs new file mode 100644 index 0000000..07d3360 --- /dev/null +++ b/X10D.Tests/src/Drawing/RandomTests.cs @@ -0,0 +1,37 @@ +using System.Drawing; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using X10D.Drawing; + +namespace X10D.Tests.Drawing; + +[TestClass] +public class RandomTests +{ + [TestMethod] + public void NextColorArgb_ShouldReturn331515e5_GivenSeed1234() + { + var random = new Random(1234); + Assert.AreEqual(Color.FromArgb(51, 21, 21, 229), random.NextColorArgb()); + } + + [TestMethod] + public void NextColorArgb_ShouldThrow_GivenNull() + { + Random? random = null; + Assert.ThrowsException(() => random!.NextColorArgb()); + } + + [TestMethod] + public void NextColorRgb_ShouldReturn1515e5_GivenSeed1234() + { + var random = new Random(1234); + Assert.AreEqual(Color.FromArgb(255, 21, 21, 229), random.NextColorRgb()); + } + + [TestMethod] + public void NextColorRgb_ShouldThrow_GivenNull() + { + Random? random = null; + Assert.ThrowsException(() => random!.NextColorRgb()); + } +} diff --git a/X10D/src/Drawing/RandomExtensions.cs b/X10D/src/Drawing/RandomExtensions.cs index 8f07e41..32cf1e8 100644 --- a/X10D/src/Drawing/RandomExtensions.cs +++ b/X10D/src/Drawing/RandomExtensions.cs @@ -8,25 +8,36 @@ namespace X10D.Drawing; public static class RandomExtensions { /// - /// Returns a random color. + /// Returns a color of random components for red, green, and blue. /// /// The instance. /// A whose red, green, and blue components are all random, and whose alpha is 255 /// is . - public static Color NextColor(this Random random) + public static Color NextColorRgb(this Random random) { if (random is null) { throw new ArgumentNullException(nameof(random)); } - int seed = random.Next(); - var seededRandom = new Random(seed); + int rgb = random.Next(); + return Color.FromArgb(0xFF, (byte)(rgb >> 16 & 0xFF), (byte)(rgb >> 8 & 0xFF), (byte)(rgb & 0xFF)); + } - var r = (byte)(seededRandom.Next() % (byte.MaxValue + 1)); - var g = (byte)(seededRandom.Next() % (byte.MaxValue + 1)); - var b = (byte)(seededRandom.Next() % (byte.MaxValue + 1)); + /// + /// Returns a color composed of random components for apha, red, green, and blue. + /// + /// The instance. + /// A whose alpha, red, green, and blue components are all random. + /// is . + public static Color NextColorArgb(this Random random) + { + if (random is null) + { + throw new ArgumentNullException(nameof(random)); + } - return Color.FromArgb(r, g, b); + int argb = random.Next(); + return Color.FromArgb(argb); } }