Add UnityEngine/System.Drawing color conversions

This commit is contained in:
Oliver Booth 2022-05-16 10:41:53 +01:00
parent e23aa13fda
commit 5aea71465a
No known key found for this signature in database
GPG Key ID: 32A00B35503AF634
5 changed files with 102 additions and 1 deletions

View File

@ -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()`

View File

@ -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()
{

View File

@ -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()
{

View File

@ -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);
}
/// <summary>
/// Converts the current color to a <see cref="System.Drawing.Color" />.
/// </summary>
/// <param name="color">The color to convert.</param>
/// <returns>The converted color.</returns>
[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);
}
/// <summary>
/// Converts the current color to a <see cref="Color32" />.
/// </summary>
/// <param name="color">The color to convert.</param>
/// <returns>The converted color.</returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Color32 ToUnityColor32(this System.Drawing.Color color)
{
return new Color32(color.R, color.G, color.B, color.A);
}
/// <summary>
/// Returns a vector whose red, green, and blue components are the same as the specified color, and whose alpha component
/// is a new value.

View File

@ -21,6 +21,35 @@ public static class ColorExtensions
return new Color(1f - color.r, 1f - color.g, 1f - color.b, color.a);
}
/// <summary>
/// Converts the current color to a <see cref="System.Drawing.Color" />.
/// </summary>
/// <param name="color">The color to convert.</param>
/// <returns>The converted color.</returns>
[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)
);
}
/// <summary>
/// Converts the current color to a <see cref="Color" />.
/// </summary>
/// <param name="color">The color to convert.</param>
/// <returns>The converted color.</returns>
[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);
}
/// <summary>
/// Returns a vector whose red, green, and blue components are the same as the specified color, and whose alpha component
/// is a new value.