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

Add Color.With* methods (resolves #55)

This commit is contained in:
Oliver Booth 2022-05-09 10:01:03 +01:00
parent 176ad9fa09
commit d904daf431
No known key found for this signature in database
GPG Key ID: 32A00B35503AF634
7 changed files with 348 additions and 1 deletions

View File

@ -4,6 +4,10 @@
### Added ### Added
- Reintroduced Unity support - Reintroduced Unity support
- X10D: Added `Color.Inverted()` - X10D: Added `Color.Inverted()`
- X10D: Added `Color.WithA()`
- X10D: Added `Color.WithB()`
- X10D: Added `Color.WithG()`
- X10D: Added `Color.WithR()`
- X10D: Added `ICollection<T>.ClearAndDisposeAll()` - X10D: Added `ICollection<T>.ClearAndDisposeAll()`
- X10D: Added `ICollection<T>.ClearAndDisposeAllAsync()` - X10D: Added `ICollection<T>.ClearAndDisposeAllAsync()`
- X10D: Added `IEnumerable<T>.For()` (#50) - X10D: Added `IEnumerable<T>.For()` (#50)
@ -14,7 +18,15 @@
- X10D: Added `Rune.IsEmoji` - X10D: Added `Rune.IsEmoji`
- X10D: Added `string.IsEmoji` - X10D: Added `string.IsEmoji`
- X10D.Unity: Added `Color.Inverted()` - X10D.Unity: Added `Color.Inverted()`
- X10D.Unity: Added `Color.WithA()`
- X10D.Unity: Added `Color.WithB()`
- X10D.Unity: Added `Color.WithG()`
- X10D.Unity: Added `Color.WithR()`
- X10D.Unity: Added `Color32.Inverted()` - X10D.Unity: Added `Color32.Inverted()`
- X10D.Unity: Added `Color32.WithA()`
- X10D.Unity: Added `Color32.WithB()`
- X10D.Unity: Added `Color32.WithG()`
- X10D.Unity: Added `Color32.WithR()`
- X10D.Unity: Added `Component.GetComponentsInChildrenOnly<T>()` - X10D.Unity: Added `Component.GetComponentsInChildrenOnly<T>()`
- X10D.Unity: Added `GameObject.GetComponentsInChildrenOnly<T>()` - X10D.Unity: Added `GameObject.GetComponentsInChildrenOnly<T>()`
- X10D.Unity: Added `GameObject.LookAt(GameObject[, Vector3])` - X10D.Unity: Added `GameObject.LookAt(GameObject[, Vector3])`

View File

@ -1,4 +1,4 @@
using System.Drawing; using System.Drawing;
using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestTools.UnitTesting;
using X10D.Drawing; using X10D.Drawing;
@ -37,4 +37,33 @@ public class ColorTests
Assert.AreEqual(expected, actual); Assert.AreEqual(expected, actual);
} }
[TestMethod]
public void WithA0_ShouldReturnSameColor_GivenWhite()
{
Color transparent = Color.FromArgb(0, 255, 255, 255);
Assert.AreEqual(transparent, White.WithA(0));
Assert.AreEqual(transparent, transparent.WithA(0));
}
[TestMethod]
public void WithB0_ShouldReturnYellow_GivenWhite()
{
Assert.AreEqual(Yellow, White.WithB(0));
Assert.AreEqual(Yellow, Yellow.WithB(0));
}
[TestMethod]
public void WithG0_ShouldReturnMagenta_GivenWhite()
{
Assert.AreEqual(Magenta, White.WithG(0));
Assert.AreEqual(Magenta, Magenta.WithG(0));
}
[TestMethod]
public void WithR0_ShouldReturnCyan_GivenWhite()
{
Assert.AreEqual(Cyan, White.WithR(0));
Assert.AreEqual(Cyan, Cyan.WithR(0));
}
} }

View File

@ -42,5 +42,42 @@ namespace X10D.Unity.Tests.Drawing
yield break; yield break;
} }
[UnityTest]
public IEnumerator WithA0_ShouldReturnSameColor_GivenWhite()
{
var transparent = new Color32(255, 255, 255, 0);
Assert.AreEqual(transparent, White.WithA(0));
Assert.AreEqual(transparent, transparent.WithA(0));
yield break;
}
[UnityTest]
public IEnumerator WithB0_ShouldReturnYellow_GivenWhite()
{
Assert.AreEqual(Yellow, White.WithB(0));
Assert.AreEqual(Yellow, Yellow.WithB(0));
yield break;
}
[UnityTest]
public IEnumerator WithG0_ShouldReturnMagenta_GivenWhite()
{
Assert.AreEqual(Magenta, White.WithG(0));
Assert.AreEqual(Magenta, Magenta.WithG(0));
yield break;
}
[UnityTest]
public IEnumerator WithR0_ShouldReturnCyan_GivenWhite()
{
Assert.AreEqual(Cyan, White.WithR(0));
Assert.AreEqual(Cyan, Cyan.WithR(0));
yield break;
}
} }
} }

View File

@ -42,5 +42,42 @@ namespace X10D.Unity.Tests.Drawing
yield break; yield break;
} }
[UnityTest]
public IEnumerator WithA0_ShouldReturnSameColor_GivenWhite()
{
var transparent = new Color(1, 1, 1, 0);
Assert.AreEqual(transparent, White.WithA(0));
Assert.AreEqual(transparent, transparent.WithA(0));
yield break;
}
[UnityTest]
public IEnumerator WithB0_ShouldReturnYellow_GivenWhite()
{
Assert.AreEqual(Yellow, White.WithB(0));
Assert.AreEqual(Yellow, Yellow.WithB(0));
yield break;
}
[UnityTest]
public IEnumerator WithG0_ShouldReturnMagenta_GivenWhite()
{
Assert.AreEqual(Magenta, White.WithG(0));
Assert.AreEqual(Magenta, Magenta.WithG(0));
yield break;
}
[UnityTest]
public IEnumerator WithR0_ShouldReturnCyan_GivenWhite()
{
Assert.AreEqual(Cyan, White.WithR(0));
Assert.AreEqual(Cyan, Cyan.WithR(0));
yield break;
}
} }
} }

View File

@ -20,4 +20,76 @@ public static class Color32Extensions
{ {
return new Color32((byte)(255 - color.r), (byte)(255 - color.g), (byte)(255 - color.b), color.a); return new Color32((byte)(255 - color.r), (byte)(255 - color.g), (byte)(255 - 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.
/// </summary>
/// <param name="color">The color to copy.</param>
/// <param name="a">The new alpha component value.</param>
/// <returns>
/// A new instance of <see cref="Color" /> whose <see cref="Color32.r" />, <see cref="Color32.g" />, and
/// <see cref="Color32.b" /> components are the same as that of <paramref name="color" />, and whose
/// <see cref="Color32.a" /> component is <paramref name="a" />.
/// </returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Color32 WithA(this Color32 color, byte a)
{
return color with {a = a};
}
/// <summary>
/// Returns a vector whose red, green, and alpha components are the same as the specified color, and whose blue component
/// is a new value.
/// </summary>
/// <param name="color">The color to copy.</param>
/// <param name="b">The new blue component value.</param>
/// <returns>
/// A new instance of <see cref="Color" /> whose <see cref="Color32.r" />, <see cref="Color32.g" />, and
/// <see cref="Color32.a" /> components are the same as that of <paramref name="color" />, and whose
/// <see cref="Color32.b" /> component is <paramref name="b" />.
/// </returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Color32 WithB(this Color32 color, byte b)
{
return color with {b = b};
}
/// <summary>
/// Returns a vector whose red, blue, and alpha components are the same as the specified color, and whose green component
/// is a new value.
/// </summary>
/// <param name="color">The color to copy.</param>
/// <param name="g">The new green component value.</param>
/// <returns>
/// A new instance of <see cref="Color32" /> whose <see cref="Color32.r" />, <see cref="Color32.g" />, and
/// <see cref="Color32.b" /> components are the same as that of <paramref name="color" />, and whose
/// <see cref="Color32.g" /> component is <paramref name="g" />.
/// </returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Color32 WithG(this Color32 color, byte g)
{
return color with {g = g};
}
/// <summary>
/// Returns a vector whose green, blue, and alpha components are the same as the specified color, and whose red component
/// is a new value.
/// </summary>
/// <param name="color">The color to copy.</param>
/// <param name="r">The new red component value.</param>
/// <returns>
/// A new instance of <see cref="Color32" /> whose <see cref="Color32.g" />, <see cref="Color32.b" />, and
/// <see cref="Color32.a" /> components are the same as that of <paramref name="color" />, and whose
/// <see cref="Color32.r" /> component is <paramref name="r" />.
/// </returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Color32 WithR(this Color32 color, byte r)
{
return color with {r = r};
}
} }

View File

@ -20,4 +20,76 @@ public static class ColorExtensions
{ {
return new Color(1f - color.r, 1f - color.g, 1f - color.b, color.a); return new Color(1f - color.r, 1f - color.g, 1f - 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.
/// </summary>
/// <param name="color">The color to copy.</param>
/// <param name="a">The new alpha component value.</param>
/// <returns>
/// A new instance of <see cref="Color" /> whose <see cref="Color.r" />, <see cref="Color.g" />, and
/// <see cref="Color.b" /> components are the same as that of <paramref name="color" />, and whose
/// <see cref="Color.a" /> component is <paramref name="a" />.
/// </returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Color WithA(this Color color, float a)
{
return color with {a = a};
}
/// <summary>
/// Returns a vector whose red, green, and alpha components are the same as the specified color, and whose blue component
/// is a new value.
/// </summary>
/// <param name="color">The color to copy.</param>
/// <param name="b">The new blue component value.</param>
/// <returns>
/// A new instance of <see cref="Color" /> whose <see cref="Color.r" />, <see cref="Color.g" />, and
/// <see cref="Color.a" /> components are the same as that of <paramref name="color" />, and whose
/// <see cref="Color.b" /> component is <paramref name="b" />.
/// </returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Color WithB(this Color color, float b)
{
return color with {b = b};
}
/// <summary>
/// Returns a vector whose red, blue, and alpha components are the same as the specified color, and whose green component
/// is a new value.
/// </summary>
/// <param name="color">The color to copy.</param>
/// <param name="g">The new green component value.</param>
/// <returns>
/// A new instance of <see cref="Color" /> whose <see cref="Color.r" />, <see cref="Color.b" />, and
/// <see cref="Color.a" /> components are the same as that of <paramref name="color" />, and whose
/// <see cref="Color.g" /> component is <paramref name="g" />.
/// </returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Color WithG(this Color color, float g)
{
return color with {g = g};
}
/// <summary>
/// Returns a vector whose green, blue, and alpha components are the same as the specified color, and whose red component
/// is a new value.
/// </summary>
/// <param name="color">The color to copy.</param>
/// <param name="r">The new red component value.</param>
/// <returns>
/// A new instance of <see cref="Color" /> whose <see cref="Color.g" />, <see cref="Color.b" />, and
/// <see cref="Color.a" /> components are the same as that of <paramref name="color" />, and whose
/// <see cref="Color.r" /> component is <paramref name="r" />.
/// </returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Color WithR(this Color color, float r)
{
return color with {r = r};
}
} }

View File

@ -24,4 +24,92 @@ public static class ColorExtensions
{ {
return Color.FromArgb(color.A, 255 - color.R, 255 - color.G, 255 - color.B); return Color.FromArgb(color.A, 255 - color.R, 255 - color.G, 255 - color.B);
} }
/// <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.
/// </summary>
/// <param name="color">The color to copy.</param>
/// <param name="a">The new alpha component value.</param>
/// <returns>
/// A new instance of <see cref="Color" /> whose <see cref="Color.R" />, <see cref="Color.G" />, and
/// <see cref="Color.B" /> components are the same as that of <paramref name="color" />, and whose
/// <see cref="Color.A" /> component is <paramref name="a" />.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static Color WithA(this Color color, int a)
{
return Color.FromArgb(a, color.R, color.G, color.B);
}
/// <summary>
/// Returns a vector whose red, green, and alpha components are the same as the specified color, and whose blue component
/// is a new value.
/// </summary>
/// <param name="color">The color to copy.</param>
/// <param name="b">The new blue component value.</param>
/// <returns>
/// A new instance of <see cref="Color" /> whose <see cref="Color.R" />, <see cref="Color.G" />, and
/// <see cref="Color.A" /> components are the same as that of <paramref name="color" />, and whose
/// <see cref="Color.B" /> component is <paramref name="b" />.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static Color WithB(this Color color, int b)
{
return Color.FromArgb(color.A, color.R, color.G, b);
}
/// <summary>
/// Returns a vector whose red, blue, and alpha components are the same as the specified color, and whose green component
/// is a new value.
/// </summary>
/// <param name="color">The color to copy.</param>
/// <param name="g">The new green component value.</param>
/// <returns>
/// A new instance of <see cref="Color" /> whose <see cref="Color.R" />, <see cref="Color.B" />, and
/// <see cref="Color.A" /> components are the same as that of <paramref name="color" />, and whose
/// <see cref="Color.G" /> component is <paramref name="g" />.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static Color WithG(this Color color, int g)
{
return Color.FromArgb(color.A, color.R, g, color.B);
}
/// <summary>
/// Returns a vector whose green, blue, and alpha components are the same as the specified color, and whose red component
/// is a new value.
/// </summary>
/// <param name="color">The color to copy.</param>
/// <param name="r">The new red component value.</param>
/// <returns>
/// A new instance of <see cref="Color" /> whose <see cref="Color.G" />, <see cref="Color.B" />, and
/// <see cref="Color.A" /> components are the same as that of <paramref name="color" />, and whose
/// <see cref="Color.R" /> component is <paramref name="r" />.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static Color WithR(this Color color, int r)
{
return Color.FromArgb(color.A, r, color.G, color.B);
}
} }