mirror of
https://github.com/oliverbooth/X10D
synced 2024-11-22 19:28:48 +00:00
Add GetClosestConsoleColor (#66)
This commit is contained in:
parent
46bfa17b93
commit
c0395feba3
@ -4,6 +4,7 @@
|
||||
### Added
|
||||
- X10D: Added `MathUtility.InverseLerp(float, float, float)` and `MathUtility.InverseLerp(double, double, double)`
|
||||
- X10D: Added `Circle`, `CircleF`, `Cuboid`, `Ellipse`, `EllipseF`, `Line3D`, `Line`, `LineF`, `Polygon`, `PolygonF`, `Polyhedron`, and `Sphere`, to complement System.Drawing structs such as `Point` and `Rectangle`
|
||||
- X10D: Added `Color.GetClosestConsoleColor()`
|
||||
- X10D: Added `DateTime.GetIso8601WeekOfYear()` and `DateTimeOffset.GetIso8601WeekOfYear()`
|
||||
- X10D: Added `DirectoryInfo.Clear([bool])`
|
||||
- X10D: Added `IList<T>.RemoveRange(Range)`
|
||||
@ -33,7 +34,9 @@
|
||||
- X10D.Unity: Added `DebugEx`, which mimics `UnityEngine.Debug` while offering more useful primitive drawing methods
|
||||
- X10D.Unity: Added `System.Drawing.Color.ToUnityColor()`
|
||||
- X10D.Unity: Added `System.Drawing.Color.ToUnityColor32()`
|
||||
- X10D.Unity: Added `Color.GetClosestConsoleColor()`
|
||||
- X10D.Unity: Added `Color.ToSystemDrawingColor()`
|
||||
- X10D.Unity: Added `Color32.GetClosestConsoleColor()`
|
||||
- X10D.Unity: Added `Color32.ToSystemDrawingColor()`
|
||||
- X10D.Unity: Added `Point.ToUnityVector2()`
|
||||
- X10D.Unity: Added `Point.ToUnityVector2Int()`
|
||||
|
@ -16,6 +16,155 @@ public class ColorTests
|
||||
private static readonly Color Magenta = Color.FromArgb(255, 0, 255);
|
||||
private static readonly Color Yellow = Color.FromArgb(255, 255, 0);
|
||||
|
||||
[TestMethod]
|
||||
public void GetClosestConsoleColor_ShouldReturnClosestColor_GivenValidColor()
|
||||
{
|
||||
Assert.AreEqual(ConsoleColor.White, Color.Transparent.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.White, Color.AliceBlue.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.White, Color.AntiqueWhite.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.Cyan, Color.Aqua.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.DarkGray, Color.Aquamarine.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.White, Color.Azure.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.White, Color.Beige.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.White, Color.Bisque.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.Black, Color.Black.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.White, Color.BlanchedAlmond.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.Blue, Color.Blue.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.DarkMagenta, Color.BlueViolet.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.DarkRed, Color.Brown.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.DarkGray, Color.BurlyWood.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.Gray, Color.CadetBlue.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.Yellow, Color.Chartreuse.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.DarkYellow, Color.Chocolate.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.DarkYellow, Color.Coral.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.DarkGray, Color.CornflowerBlue.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.White, Color.Cornsilk.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.Red, Color.Crimson.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.Cyan, Color.Cyan.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.DarkBlue, Color.DarkBlue.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.DarkCyan, Color.DarkCyan.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.DarkYellow, Color.DarkGoldenrod.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.DarkGray, Color.DarkGray.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.DarkGreen, Color.DarkGreen.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.DarkGray, Color.DarkKhaki.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.DarkMagenta, Color.DarkMagenta.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.Gray, Color.DarkOliveGreen.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.DarkYellow, Color.DarkOrange.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.DarkMagenta, Color.DarkOrchid.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.DarkRed, Color.DarkRed.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.DarkGray, Color.DarkSalmon.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.DarkGray, Color.DarkSeaGreen.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.Gray, Color.DarkSlateBlue.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.DarkGreen, Color.DarkSlateGray.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.Cyan, Color.DarkTurquoise.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.DarkMagenta, Color.DarkViolet.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.Magenta, Color.DeepPink.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.Cyan, Color.DeepSkyBlue.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.Gray, Color.DimGray.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.Cyan, Color.DodgerBlue.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.DarkRed, Color.Firebrick.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.White, Color.FloralWhite.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.Green, Color.ForestGreen.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.Magenta, Color.Fuchsia.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.White, Color.Gainsboro.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.White, Color.GhostWhite.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.Yellow, Color.Gold.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.DarkYellow, Color.Goldenrod.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.Gray, Color.Gray.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.Green, Color.Green.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.Yellow, Color.GreenYellow.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.White, Color.Honeydew.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.DarkGray, Color.HotPink.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.Gray, Color.IndianRed.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.DarkMagenta, Color.Indigo.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.White, Color.Ivory.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.DarkGray, Color.Khaki.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.White, Color.Lavender.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.White, Color.LavenderBlush.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.Yellow, Color.LawnGreen.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.White, Color.LemonChiffon.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.DarkGray, Color.LightBlue.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.DarkGray, Color.LightCoral.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.White, Color.LightCyan.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.White, Color.LightGoldenrodYellow.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.DarkGray, Color.LightGreen.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.DarkGray, Color.LightGray.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.DarkGray, Color.LightPink.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.DarkGray, Color.LightSalmon.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.DarkCyan, Color.LightSeaGreen.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.DarkGray, Color.LightSkyBlue.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.Gray, Color.LightSlateGray.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.DarkGray, Color.LightSteelBlue.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.White, Color.LightYellow.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.Green, Color.Lime.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.Green, Color.LimeGreen.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.White, Color.Linen.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.Magenta, Color.Magenta.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.DarkRed, Color.Maroon.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.DarkGray, Color.MediumAquamarine.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.Blue, Color.MediumBlue.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.DarkGray, Color.MediumOrchid.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.DarkGray, Color.MediumPurple.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.DarkCyan, Color.MediumSeaGreen.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.DarkGray, Color.MediumSlateBlue.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.Cyan, Color.MediumSpringGreen.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.Cyan, Color.MediumTurquoise.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.DarkMagenta, Color.MediumVioletRed.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.DarkBlue, Color.MidnightBlue.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.White, Color.MintCream.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.White, Color.MistyRose.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.White, Color.Moccasin.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.White, Color.NavajoWhite.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.DarkBlue, Color.Navy.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.White, Color.OldLace.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.Gray, Color.Olive.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.Gray, Color.OliveDrab.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.DarkYellow, Color.Orange.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.Red, Color.OrangeRed.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.DarkGray, Color.Orchid.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.White, Color.PaleGoldenrod.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.DarkGray, Color.PaleGreen.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.White, Color.PaleTurquoise.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.DarkGray, Color.PaleVioletRed.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.White, Color.PapayaWhip.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.White, Color.PeachPuff.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.DarkYellow, Color.Peru.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.White, Color.Pink.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.DarkGray, Color.Plum.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.DarkGray, Color.PowderBlue.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.DarkMagenta, Color.Purple.GetClosestConsoleColor());
|
||||
#if NET6_0_OR_GREATER
|
||||
Assert.AreEqual(ConsoleColor.DarkMagenta, Color.RebeccaPurple.GetClosestConsoleColor());
|
||||
#endif
|
||||
Assert.AreEqual(ConsoleColor.Red, Color.Red.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.DarkGray, Color.RosyBrown.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.DarkCyan, Color.RoyalBlue.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.DarkRed, Color.SaddleBrown.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.DarkGray, Color.Salmon.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.DarkYellow, Color.SandyBrown.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.DarkCyan, Color.SeaGreen.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.White, Color.SeaShell.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.DarkRed, Color.Sienna.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.DarkGray, Color.Silver.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.DarkGray, Color.SkyBlue.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.Gray, Color.SlateBlue.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.Gray, Color.SlateGray.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.White, Color.Snow.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.DarkCyan, Color.SpringGreen.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.Gray, Color.SteelBlue.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.DarkGray, Color.Tan.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.DarkCyan, Color.Teal.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.DarkGray, Color.Thistle.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.DarkYellow, Color.Tomato.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.Cyan, Color.Turquoise.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.DarkGray, Color.Violet.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.White, Color.Wheat.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.White, Color.White.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.White, Color.WhiteSmoke.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.Yellow, Color.Yellow.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.Gray, Color.YellowGreen.GetClosestConsoleColor());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Inverted_ShouldReturnInvertedColor()
|
||||
{
|
||||
|
@ -1,4 +1,5 @@
|
||||
using System.Collections;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using NUnit.Framework;
|
||||
using UnityEngine;
|
||||
using UnityEngine.TestTools;
|
||||
@ -17,6 +18,26 @@ namespace X10D.Unity.Tests.Drawing
|
||||
private static readonly Color32 Magenta = new(255, 0, 255, 255);
|
||||
private static readonly Color32 Yellow = new(255, 255, 0, 255);
|
||||
|
||||
[UnityTest]
|
||||
public IEnumerator GetClosestConsoleColor_ShouldReturnClosestColor_GivenValidColor()
|
||||
{
|
||||
// I know it's just casting... but aim for 100% coverage babyyyy
|
||||
|
||||
Assert.AreEqual(ConsoleColor.Red, ((Color32)Color.red).GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.Green, ((Color32)Color.green).GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.Blue, ((Color32)Color.blue).GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.White, ((Color32)Color.white).GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.Black, ((Color32)Color.black).GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.Yellow, ((Color32)Color.yellow).GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.Cyan, ((Color32)Color.cyan).GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.Magenta, ((Color32)Color.magenta).GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.Gray, ((Color32)Color.gray).GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.Gray, ((Color32)Color.grey).GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.Black, ((Color32)Color.clear).GetClosestConsoleColor());
|
||||
|
||||
yield break;
|
||||
}
|
||||
|
||||
[UnityTest]
|
||||
public IEnumerator Inverted_ShouldReturnInvertedColor()
|
||||
{
|
||||
|
@ -1,4 +1,7 @@
|
||||
using System.Collections;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using NUnit.Framework;
|
||||
using UnityEngine;
|
||||
using UnityEngine.TestTools;
|
||||
@ -17,6 +20,24 @@ namespace X10D.Unity.Tests.Drawing
|
||||
private static readonly Color Magenta = new(1, 0, 1);
|
||||
private static readonly Color Yellow = new(1, 1, 0);
|
||||
|
||||
[UnityTest]
|
||||
public IEnumerator GetClosestConsoleColor_ShouldReturnClosestColor_GivenValidColor()
|
||||
{
|
||||
Assert.AreEqual(ConsoleColor.Red, Color.red.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.Green, Color.green.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.Blue, Color.blue.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.White, Color.white.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.Black, Color.black.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.Yellow, Color.yellow.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.Cyan, Color.cyan.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.Magenta, Color.magenta.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.Gray, Color.gray.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.Gray, Color.grey.GetClosestConsoleColor());
|
||||
Assert.AreEqual(ConsoleColor.Black, Color.clear.GetClosestConsoleColor());
|
||||
|
||||
yield break;
|
||||
}
|
||||
|
||||
[UnityTest]
|
||||
public IEnumerator Inverted_ShouldReturnInvertedColor()
|
||||
{
|
||||
|
@ -1,6 +1,7 @@
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Runtime.CompilerServices;
|
||||
using UnityEngine;
|
||||
using X10D.Drawing;
|
||||
|
||||
namespace X10D.Unity.Drawing;
|
||||
|
||||
@ -9,6 +10,18 @@ namespace X10D.Unity.Drawing;
|
||||
/// </summary>
|
||||
public static class Color32Extensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Returns a <see cref="ConsoleColor" /> which most closely resembles the current color.
|
||||
/// </summary>
|
||||
/// <param name="color">The source color.</param>
|
||||
/// <returns>The closest <see cref="ConsoleColor" />.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static ConsoleColor GetClosestConsoleColor(this Color32 color)
|
||||
{
|
||||
return color.ToSystemDrawingColor().GetClosestConsoleColor();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a new <see cref="Color32" /> with the red, green, and blue components inverted. Alpha is not affected.
|
||||
/// </summary>
|
||||
|
@ -1,6 +1,7 @@
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Runtime.CompilerServices;
|
||||
using UnityEngine;
|
||||
using X10D.Drawing;
|
||||
|
||||
namespace X10D.Unity.Drawing;
|
||||
|
||||
@ -9,6 +10,18 @@ namespace X10D.Unity.Drawing;
|
||||
/// </summary>
|
||||
public static class ColorExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Returns a <see cref="ConsoleColor" /> which most closely resembles the current color.
|
||||
/// </summary>
|
||||
/// <param name="color">The source color.</param>
|
||||
/// <returns>The closest <see cref="ConsoleColor" />.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static ConsoleColor GetClosestConsoleColor(this Color color)
|
||||
{
|
||||
return color.ToSystemDrawingColor().GetClosestConsoleColor();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a new <see cref="Color" /> with the red, green, and blue components inverted. Alpha is not affected.
|
||||
/// </summary>
|
||||
|
@ -9,6 +9,58 @@ namespace X10D.Drawing;
|
||||
/// </summary>
|
||||
public static class ColorExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Returns a <see cref="ConsoleColor" /> which most closely resembles the current color.
|
||||
/// </summary>
|
||||
/// <param name="color">The source color.</param>
|
||||
/// <returns>The closest <see cref="ConsoleColor" />.</returns>
|
||||
/// <author>Glenn Slayden, https://stackoverflow.com/a/12340136/1467293</author>
|
||||
[Pure]
|
||||
#if NETSTANDARD2_1
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
#else
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
||||
#endif
|
||||
public static ConsoleColor GetClosestConsoleColor(this Color color)
|
||||
{
|
||||
ConsoleColor result = 0;
|
||||
double red = color.R;
|
||||
double green = color.G;
|
||||
double blue = color.B;
|
||||
var delta = double.MaxValue;
|
||||
|
||||
#if NET5_0_OR_GREATER
|
||||
foreach (ConsoleColor consoleColor in Enum.GetValues<ConsoleColor>())
|
||||
#else
|
||||
foreach (ConsoleColor consoleColor in Enum.GetValues(typeof(ConsoleColor)))
|
||||
#endif
|
||||
{
|
||||
#if NET5_0_OR_GREATER
|
||||
string name = Enum.GetName(consoleColor)!;
|
||||
#else
|
||||
string name = Enum.GetName(typeof(ConsoleColor), consoleColor)!;
|
||||
#endif
|
||||
Color currentColor = Color.FromName(name == "DarkYellow" ? "Orange" : name); // bug fix
|
||||
double r = currentColor.R - red;
|
||||
double g = currentColor.G - green;
|
||||
double b = currentColor.B - blue;
|
||||
double t = r * r + g * g + b * b;
|
||||
|
||||
if (t == 0.0)
|
||||
{
|
||||
return consoleColor;
|
||||
}
|
||||
|
||||
if (t < delta)
|
||||
{
|
||||
delta = t;
|
||||
result = consoleColor;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a new <see cref="Color" /> with the red, green, and blue components inverted. Alpha is not affected.
|
||||
/// </summary>
|
||||
|
Loading…
Reference in New Issue
Block a user