mirror of
https://github.com/oliverbooth/X10D
synced 2024-11-09 23:45:42 +00:00
Add NextColorRgb and NextColorArgb
This commit is contained in:
parent
54b22ab6d9
commit
40a75e62c0
37
X10D.Tests/src/Drawing/RandomTests.cs
Normal file
37
X10D.Tests/src/Drawing/RandomTests.cs
Normal file
@ -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<ArgumentNullException>(() => 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<ArgumentNullException>(() => random!.NextColorRgb());
|
||||
}
|
||||
}
|
@ -8,25 +8,36 @@ namespace X10D.Drawing;
|
||||
public static class RandomExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Returns a random color.
|
||||
/// Returns a color of random components for red, green, and blue.
|
||||
/// </summary>
|
||||
/// <param name="random">The <see cref="System.Random" /> instance.</param>
|
||||
/// <returns>A <see cref="Color" /> whose red, green, and blue components are all random, and whose alpha is 255</returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="random" /> is <see langword="null" />.</exception>
|
||||
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));
|
||||
/// <summary>
|
||||
/// Returns a color composed of random components for apha, red, green, and blue.
|
||||
/// </summary>
|
||||
/// <param name="random">The <see cref="System.Random" /> instance.</param>
|
||||
/// <returns>A <see cref="Color" /> whose alpha, red, green, and blue components are all random.</returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="random" /> is <see langword="null" />.</exception>
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user