From aa30abea93672fee6299480a5e5868e9f75d4e5b Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Sun, 8 May 2022 19:29:59 +0100 Subject: [PATCH] Add Color.Inverted (#54) --- X10D.Tests/src/Drawing/ColorTests.cs | 40 ++++++++++++++++ .../Assets/Tests/Drawing/Color32Tests.cs | 46 +++++++++++++++++++ .../Assets/Tests/Drawing/Color32Tests.cs.meta | 3 ++ .../Assets/Tests/Drawing/ColorTests.cs | 46 +++++++++++++++++++ .../Assets/Tests/Drawing/ColorTests.cs.meta | 3 ++ X10D.Unity/src/Drawing/Color32Extensions.cs | 23 ++++++++++ X10D.Unity/src/Drawing/ColorExtensions.cs | 23 ++++++++++ X10D/src/Drawing/ColorExtensions.cs | 27 +++++++++++ 8 files changed, 211 insertions(+) create mode 100644 X10D.Tests/src/Drawing/ColorTests.cs create mode 100644 X10D.Unity.Tests/Assets/Tests/Drawing/Color32Tests.cs create mode 100644 X10D.Unity.Tests/Assets/Tests/Drawing/Color32Tests.cs.meta create mode 100644 X10D.Unity.Tests/Assets/Tests/Drawing/ColorTests.cs create mode 100644 X10D.Unity.Tests/Assets/Tests/Drawing/ColorTests.cs.meta create mode 100644 X10D.Unity/src/Drawing/Color32Extensions.cs create mode 100644 X10D.Unity/src/Drawing/ColorExtensions.cs create mode 100644 X10D/src/Drawing/ColorExtensions.cs diff --git a/X10D.Tests/src/Drawing/ColorTests.cs b/X10D.Tests/src/Drawing/ColorTests.cs new file mode 100644 index 0000000..b58964e --- /dev/null +++ b/X10D.Tests/src/Drawing/ColorTests.cs @@ -0,0 +1,40 @@ +using System.Drawing; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using X10D.Drawing; + +namespace X10D.Tests.Drawing; + +[TestClass] +public class ColorTests +{ + [TestMethod] + public void Inverted_ShouldReturnInvertedColor() + { + Color black = Color.FromArgb(0, 0, 0); + Color white = Color.FromArgb(255, 255, 255); + Color red = Color.FromArgb(255, 0, 0); + Color green = Color.FromArgb(0, 255, 0); + Color blue = Color.FromArgb(0, 0, 255); + Color cyan = Color.FromArgb(0, 255, 255); + Color magenta = Color.FromArgb(255, 0, 255); + Color yellow = Color.FromArgb(255, 255, 0); + + Assert.AreEqual(white, black.Inverted()); + Assert.AreEqual(black, white.Inverted()); + Assert.AreEqual(red, cyan.Inverted()); + Assert.AreEqual(cyan, red.Inverted()); + Assert.AreEqual(green, magenta.Inverted()); + Assert.AreEqual(magenta, green.Inverted()); + Assert.AreEqual(yellow, blue.Inverted()); + Assert.AreEqual(blue, yellow.Inverted()); + } + + [TestMethod] + public void Inverted_ShouldIgnoreAlpha() + { + Color expected = Color.FromArgb(255, 0, 0, 0); + Color actual = Color.FromArgb(255, 255, 255, 255).Inverted(); + + Assert.AreEqual(expected, actual); + } +} diff --git a/X10D.Unity.Tests/Assets/Tests/Drawing/Color32Tests.cs b/X10D.Unity.Tests/Assets/Tests/Drawing/Color32Tests.cs new file mode 100644 index 0000000..7914299 --- /dev/null +++ b/X10D.Unity.Tests/Assets/Tests/Drawing/Color32Tests.cs @@ -0,0 +1,46 @@ +using System.Collections; +using NUnit.Framework; +using UnityEngine; +using UnityEngine.TestTools; +using X10D.Unity.Drawing; + +namespace X10D.Unity.Tests.Drawing +{ + public class Color32Tests + { + [UnityTest] + public IEnumerator Inverted_ShouldReturnInvertedColor() + { + var black = new Color32(0, 0, 0, 1); + var white = new Color32(255, 255, 255, 1); + var red = new Color32(255, 0, 0, 1); + var green = new Color32(0, 255, 0, 1); + var blue = new Color32(0, 0, 255, 1); + var cyan = new Color32(0, 255, 255, 1); + var magenta = new Color32(255, 0, 255, 1); + var yellow = new Color32(255, 255, 0, 1); + + Assert.AreEqual(white, black.Inverted()); + Assert.AreEqual(black, white.Inverted()); + Assert.AreEqual(red, cyan.Inverted()); + Assert.AreEqual(cyan, red.Inverted()); + Assert.AreEqual(green, magenta.Inverted()); + Assert.AreEqual(magenta, green.Inverted()); + Assert.AreEqual(yellow, blue.Inverted()); + Assert.AreEqual(blue, yellow.Inverted()); + + yield break; + } + + [UnityTest] + public IEnumerator Inverted_ShouldIgnoreAlpha() + { + var expected = new Color32(0, 0, 0, 255); + var actual = new Color32(255, 255, 255, 255).Inverted(); + + Assert.AreEqual(expected, actual); + + yield break; + } + } +} diff --git a/X10D.Unity.Tests/Assets/Tests/Drawing/Color32Tests.cs.meta b/X10D.Unity.Tests/Assets/Tests/Drawing/Color32Tests.cs.meta new file mode 100644 index 0000000..6a819da --- /dev/null +++ b/X10D.Unity.Tests/Assets/Tests/Drawing/Color32Tests.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 877c5a68b0dd44c68aae01463ae26b26 +timeCreated: 1652035626 \ No newline at end of file diff --git a/X10D.Unity.Tests/Assets/Tests/Drawing/ColorTests.cs b/X10D.Unity.Tests/Assets/Tests/Drawing/ColorTests.cs new file mode 100644 index 0000000..f7a6a9b --- /dev/null +++ b/X10D.Unity.Tests/Assets/Tests/Drawing/ColorTests.cs @@ -0,0 +1,46 @@ +using System.Collections; +using NUnit.Framework; +using UnityEngine; +using UnityEngine.TestTools; +using X10D.Unity.Drawing; + +namespace X10D.Unity.Tests.Drawing +{ + public class ColorTests + { + [UnityTest] + public IEnumerator Inverted_ShouldReturnInvertedColor() + { + var black = new Color(0, 0, 0); + var white = new Color(1, 1, 1); + var red = new Color(1, 0, 0); + var green = new Color(0, 1, 0); + var blue = new Color(0, 0, 1); + var cyan = new Color(0, 1, 1); + var magenta = new Color(1, 0, 1); + var yellow = new Color(1, 1, 0); + + Assert.AreEqual(white, black.Inverted()); + Assert.AreEqual(black, white.Inverted()); + Assert.AreEqual(red, cyan.Inverted()); + Assert.AreEqual(cyan, red.Inverted()); + Assert.AreEqual(green, magenta.Inverted()); + Assert.AreEqual(magenta, green.Inverted()); + Assert.AreEqual(yellow, blue.Inverted()); + Assert.AreEqual(blue, yellow.Inverted()); + + yield break; + } + + [UnityTest] + public IEnumerator Inverted_ShouldIgnoreAlpha() + { + var expected = new Color(0, 0, 0, 1); + var actual = new Color(1, 1, 1, 1).Inverted(); + + Assert.AreEqual(expected, actual); + + yield break; + } + } +} diff --git a/X10D.Unity.Tests/Assets/Tests/Drawing/ColorTests.cs.meta b/X10D.Unity.Tests/Assets/Tests/Drawing/ColorTests.cs.meta new file mode 100644 index 0000000..8ab4670 --- /dev/null +++ b/X10D.Unity.Tests/Assets/Tests/Drawing/ColorTests.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 61df0ae6778a4ab084e688f13adfc29c +timeCreated: 1652035747 \ No newline at end of file diff --git a/X10D.Unity/src/Drawing/Color32Extensions.cs b/X10D.Unity/src/Drawing/Color32Extensions.cs new file mode 100644 index 0000000..80a1c49 --- /dev/null +++ b/X10D.Unity/src/Drawing/Color32Extensions.cs @@ -0,0 +1,23 @@ +using System.Diagnostics.Contracts; +using System.Runtime.CompilerServices; +using UnityEngine; + +namespace X10D.Unity.Drawing; + +/// +/// Drawing-related extensions for . +/// +public static class Color32Extensions +{ + /// + /// Returns a new with the red, green, and blue components inverted. Alpha is not affected. + /// + /// The color to invert. + /// The inverted color. + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Color32 Inverted(this Color32 color) + { + return new Color32((byte)(255 - color.r), (byte)(255 - color.g), (byte)(255 - color.b), color.a); + } +} diff --git a/X10D.Unity/src/Drawing/ColorExtensions.cs b/X10D.Unity/src/Drawing/ColorExtensions.cs new file mode 100644 index 0000000..a74e84b --- /dev/null +++ b/X10D.Unity/src/Drawing/ColorExtensions.cs @@ -0,0 +1,23 @@ +using System.Diagnostics.Contracts; +using System.Runtime.CompilerServices; +using UnityEngine; + +namespace X10D.Unity.Drawing; + +/// +/// Drawing-related extensions for . +/// +public static class ColorExtensions +{ + /// + /// Returns a new with the red, green, and blue components inverted. Alpha is not affected. + /// + /// The color to invert. + /// The inverted color. + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Color Inverted(this Color color) + { + return new Color(1f - color.r, 1f - color.g, 1f - color.b, color.a); + } +} diff --git a/X10D/src/Drawing/ColorExtensions.cs b/X10D/src/Drawing/ColorExtensions.cs new file mode 100644 index 0000000..b335fb2 --- /dev/null +++ b/X10D/src/Drawing/ColorExtensions.cs @@ -0,0 +1,27 @@ +using System.Diagnostics.Contracts; +using System.Drawing; +using System.Runtime.CompilerServices; + +namespace X10D.Drawing; + +/// +/// Drawing-related extensions for . +/// +public static class ColorExtensions +{ + /// + /// Returns a new with the red, green, and blue components inverted. Alpha is not affected. + /// + /// The color to invert. + /// The inverted color. + [Pure] +#if NETSTANDARD2_1 + [MethodImpl(MethodImplOptions.AggressiveInlining)] +#else + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] +#endif + public static Color Inverted(this Color color) + { + return Color.FromArgb(color.A, 255 - color.R, 255 - color.G, 255 - color.B); + } +}