1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-22 23:58:48 +00:00

Add Color.Inverted (#54)

This commit is contained in:
Oliver Booth 2022-05-08 19:29:59 +01:00
parent 6c24231a2c
commit aa30abea93
No known key found for this signature in database
GPG Key ID: 32A00B35503AF634
8 changed files with 211 additions and 0 deletions

View File

@ -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);
}
}

View File

@ -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;
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 877c5a68b0dd44c68aae01463ae26b26
timeCreated: 1652035626

View File

@ -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;
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 61df0ae6778a4ab084e688f13adfc29c
timeCreated: 1652035747

View File

@ -0,0 +1,23 @@
using System.Diagnostics.Contracts;
using System.Runtime.CompilerServices;
using UnityEngine;
namespace X10D.Unity.Drawing;
/// <summary>
/// Drawing-related extensions for <see cref="Color32" />.
/// </summary>
public static class Color32Extensions
{
/// <summary>
/// Returns a new <see cref="Color32" /> with the red, green, and blue components inverted. Alpha is not affected.
/// </summary>
/// <param name="color">The color to invert.</param>
/// <returns>The inverted color.</returns>
[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);
}
}

View File

@ -0,0 +1,23 @@
using System.Diagnostics.Contracts;
using System.Runtime.CompilerServices;
using UnityEngine;
namespace X10D.Unity.Drawing;
/// <summary>
/// Drawing-related extensions for <see cref="Color" />.
/// </summary>
public static class ColorExtensions
{
/// <summary>
/// Returns a new <see cref="Color" /> with the red, green, and blue components inverted. Alpha is not affected.
/// </summary>
/// <param name="color">The color to invert.</param>
/// <returns>The inverted color.</returns>
[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);
}
}

View File

@ -0,0 +1,27 @@
using System.Diagnostics.Contracts;
using System.Drawing;
using System.Runtime.CompilerServices;
namespace X10D.Drawing;
/// <summary>
/// Drawing-related extensions for <see cref="Color" />.
/// </summary>
public static class ColorExtensions
{
/// <summary>
/// Returns a new <see cref="Color" /> with the red, green, and blue components inverted. Alpha is not affected.
/// </summary>
/// <param name="color">The color to invert.</param>
/// <returns>The inverted color.</returns>
[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);
}
}