From 5aea71465aa9d3e3a59f39762f32a27a1ae60f5d Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Mon, 16 May 2022 10:41:53 +0100 Subject: [PATCH] Add UnityEngine/System.Drawing color conversions --- CHANGELOG.md | 4 +++ .../Assets/Tests/Drawing/Color32Tests.cs | 24 ++++++++++++++- .../Assets/Tests/Drawing/ColorTests.cs | 22 ++++++++++++++ X10D.Unity/src/Drawing/Color32Extensions.cs | 24 +++++++++++++++ X10D.Unity/src/Drawing/ColorExtensions.cs | 29 +++++++++++++++++++ 5 files changed, 102 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ff52f08..9cf81f3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,10 @@ - X10D: Added `Vector2.Deconstruct()` - X10D: Added `Vector3.Deconstruct()` - X10D: Added `Vector4.Deconstruct()` +- X10D.Unity: Added `System.Drawing.Color.ToUnityColor()` +- X10D.Unity: Added `System.Drawing.Color.ToUnityColor32()` +- X10D.Unity: Added `Color.ToSystemDrawingColor()` +- X10D.Unity: Added `Color32.ToSystemDrawingColor()` - X10D.Unity: Added `Vector2.Deconstruct()` - X10D.Unity: Added `Vector3.Deconstruct()` - X10D.Unity: Added `Vector4.Deconstruct()` diff --git a/X10D.Unity.Tests/Assets/Tests/Drawing/Color32Tests.cs b/X10D.Unity.Tests/Assets/Tests/Drawing/Color32Tests.cs index 7da3e95..e98af61 100644 --- a/X10D.Unity.Tests/Assets/Tests/Drawing/Color32Tests.cs +++ b/X10D.Unity.Tests/Assets/Tests/Drawing/Color32Tests.cs @@ -1,4 +1,4 @@ -using System.Collections; +using System.Collections; using NUnit.Framework; using UnityEngine; using UnityEngine.TestTools; @@ -43,6 +43,28 @@ namespace X10D.Unity.Tests.Drawing yield break; } + [UnityTest] + public IEnumerator ToSystemDrawingColor_ShouldReturnEquivalentColor() + { + System.Drawing.Color expected = System.Drawing.Color.FromArgb(255, 255, 255); + System.Drawing.Color actual = White.ToSystemDrawingColor(); + + Assert.AreEqual(expected, actual); + + yield break; + } + + [UnityTest] + public IEnumerator ToUnityColor32_ShouldReturnEquivalentColor() + { + Color32 expected = White; + Color32 actual = System.Drawing.Color.FromArgb(255, 255, 255).ToUnityColor32(); + + Assert.AreEqual(expected, actual); + + yield break; + } + [UnityTest] public IEnumerator WithA0_ShouldReturnSameColor_GivenWhite() { diff --git a/X10D.Unity.Tests/Assets/Tests/Drawing/ColorTests.cs b/X10D.Unity.Tests/Assets/Tests/Drawing/ColorTests.cs index c27ad2c..989d309 100644 --- a/X10D.Unity.Tests/Assets/Tests/Drawing/ColorTests.cs +++ b/X10D.Unity.Tests/Assets/Tests/Drawing/ColorTests.cs @@ -43,6 +43,28 @@ namespace X10D.Unity.Tests.Drawing yield break; } + [UnityTest] + public IEnumerator ToSystemDrawingColor_ShouldReturnEquivalentColor() + { + System.Drawing.Color expected = System.Drawing.Color.FromArgb(255, 255, 255); + System.Drawing.Color actual = White.ToSystemDrawingColor(); + + Assert.AreEqual(expected, actual); + + yield break; + } + + [UnityTest] + public IEnumerator ToUnityColor_ShouldReturnEquivalentColor() + { + Color expected = White; + Color actual = System.Drawing.Color.FromArgb(255, 255, 255).ToUnityColor(); + + Assert.AreEqual(expected, actual); + + yield break; + } + [UnityTest] public IEnumerator WithA0_ShouldReturnSameColor_GivenWhite() { diff --git a/X10D.Unity/src/Drawing/Color32Extensions.cs b/X10D.Unity/src/Drawing/Color32Extensions.cs index 363f14d..b62b1ec 100644 --- a/X10D.Unity/src/Drawing/Color32Extensions.cs +++ b/X10D.Unity/src/Drawing/Color32Extensions.cs @@ -21,6 +21,30 @@ public static class Color32Extensions return new Color32((byte)(255 - color.r), (byte)(255 - color.g), (byte)(255 - color.b), color.a); } + /// + /// Converts the current color to a . + /// + /// The color to convert. + /// The converted color. + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static System.Drawing.Color ToSystemDrawingColor(this Color32 color) + { + return System.Drawing.Color.FromArgb(color.a, color.r, color.g, color.b); + } + + /// + /// Converts the current color to a . + /// + /// The color to convert. + /// The converted color. + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Color32 ToUnityColor32(this System.Drawing.Color color) + { + return new Color32(color.R, color.G, color.B, color.A); + } + /// /// Returns a vector whose red, green, and blue components are the same as the specified color, and whose alpha component /// is a new value. diff --git a/X10D.Unity/src/Drawing/ColorExtensions.cs b/X10D.Unity/src/Drawing/ColorExtensions.cs index 95a6561..1276a61 100644 --- a/X10D.Unity/src/Drawing/ColorExtensions.cs +++ b/X10D.Unity/src/Drawing/ColorExtensions.cs @@ -21,6 +21,35 @@ public static class ColorExtensions return new Color(1f - color.r, 1f - color.g, 1f - color.b, color.a); } + /// + /// Converts the current color to a . + /// + /// The color to convert. + /// The converted color. + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static System.Drawing.Color ToSystemDrawingColor(this Color color) + { + return System.Drawing.Color.FromArgb( + (int)(color.a * 255f), + (int)(color.r * 255f), + (int)(color.g * 255f), + (int)(color.b * 255f) + ); + } + + /// + /// Converts the current color to a . + /// + /// The color to convert. + /// The converted color. + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Color ToUnityColor(this System.Drawing.Color color) + { + return new Color(color.R / 255f, color.G / 255f, color.B / 255f, color.A / 255f); + } + /// /// Returns a vector whose red, green, and blue components are the same as the specified color, and whose alpha component /// is a new value.