Skip to content

Commit

Permalink
Merge pull request #4 from NoahStolk/add-color-pack-utility-methods
Browse files Browse the repository at this point in the history
Add color pack utility methods
  • Loading branch information
NoahStolk authored Jun 12, 2024
2 parents fc174d0 + 42a0ca3 commit 211743f
Show file tree
Hide file tree
Showing 10 changed files with 298 additions and 232 deletions.
21 changes: 21 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,27 @@

This library uses [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 0.8.0

### Added

- Added utility methods to pack `Rgba` data into an `int` and vice versa:
- `ToRgbaInt`
- `ToArgbInt`
- `FromRgbaInt`
- `FromArgbInt`

### Changed

- Renamed `Color` to `Rgba` to avoid conflicts with System.Drawing.
- Renamed `ReadColor` in `BinaryReaderExtensions` to `ReadRgba`.
- Renamed `RandomColor` in `RandomExtensions` to `RandomRgba`.
- The `A` parameter in the `Rgba` constructor is now optional.

### Removed

- Removed `ReadableColorForBrightness` from `Rgba`.

## 0.7.0

### Added
Expand Down
30 changes: 30 additions & 0 deletions src/Detach.Tests/Tests/Numerics/RgbaTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Detach.Numerics;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Numerics;

namespace Detach.Tests.Tests.Numerics;

[TestClass]
public class RgbaTests
{
[DataTestMethod]
[DataRow(0, 0, 0, 0)]
[DataRow(1, 2, 3, 4)]
[DataRow(5, 6, 7, 8)]
[DataRow(255, 6, 255, 8)]
[DataRow(255, 255, 255, 255)]
public void RgbaConversions(int r, int g, int b, int a)
{
Rgba expectedRgba = new((byte)r, (byte)g, (byte)b, (byte)a);
Rgba expectedRgb = expectedRgba with { A = byte.MaxValue };

Assert.AreEqual(expectedRgba, Rgba.FromVector4(new Vector4(r / 255f, g / 255f, b / 255f, a / 255f)));
Assert.AreEqual(expectedRgb, Rgba.FromVector3(new Vector3(r / 255f, g / 255f, b / 255f)));

Assert.AreEqual(expectedRgba, Rgba.FromRgbaInt(expectedRgba.ToRgbaInt()));
Assert.AreEqual(expectedRgb, Rgba.FromRgbaInt(expectedRgb.ToRgbaInt()));

Assert.AreEqual(expectedRgba, Rgba.FromArgbInt(expectedRgba.ToArgbInt()));
Assert.AreEqual(expectedRgb, Rgba.FromArgbInt(expectedRgb.ToArgbInt()));
}
}
2 changes: 1 addition & 1 deletion src/Detach/Detach.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<Copyright>Copyright © Noah Stolk</Copyright>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/NoahStolk/Detach</RepositoryUrl>
<Version>0.7.0</Version>
<Version>0.8.0</Version>
</PropertyGroup>

<ItemGroup Label="Static code analysis">
Expand Down
4 changes: 2 additions & 2 deletions src/Detach/Extensions/BinaryReaderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ public static Matrix4x4 ReadMatrix4x4(this BinaryReader br)
br.ReadSingle(), br.ReadSingle(), br.ReadSingle(), br.ReadSingle());
}

public static Color ReadColor(this BinaryReader br)
public static Rgba ReadRgba(this BinaryReader br)
{
return new Color(br.ReadByte(), br.ReadByte(), br.ReadByte(), br.ReadByte());
return new Rgba(br.ReadByte(), br.ReadByte(), br.ReadByte(), br.ReadByte());
}

public static List<T> ReadLengthPrefixedList<T>(this BinaryReader br, Func<BinaryReader, T> reader)
Expand Down
10 changes: 5 additions & 5 deletions src/Detach/Extensions/BinaryWriterExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ public static void Write(this BinaryWriter bw, Matrix4x4 matrix)
bw.Write(matrix.M44);
}

public static void Write(this BinaryWriter bw, Color color)
public static void Write(this BinaryWriter bw, Rgba rgba)
{
bw.Write(color.R);
bw.Write(color.G);
bw.Write(color.B);
bw.Write(color.A);
bw.Write(rgba.R);
bw.Write(rgba.G);
bw.Write(rgba.B);
bw.Write(rgba.A);
}

public static void WriteLengthPrefixedList<T>(this BinaryWriter bw, List<T> list, Action<BinaryWriter, T> writer)
Expand Down
32 changes: 16 additions & 16 deletions src/Detach/Extensions/RandomExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -486,33 +486,33 @@ public static Vector4 RandomVector4(this Random random, Vector4 minValue, Vector
}

/// <summary>
/// Returns a random <see cref="Color" /> with values that are greater than or equal to the corresponding min parameters, and less than the corresponding max parameters.
/// Returns a random <see cref="Rgba" /> with values that are greater than or equal to the corresponding min parameters, and less than the corresponding max parameters.
/// </summary>
/// <param name="random">The <see cref="Random" /> instance.</param>
/// <param name="minValueX">The minimum X value for the <see cref="Color" />.</param>
/// <param name="maxValueX">The maximum X value for the <see cref="Color" />.</param>
/// <param name="minValueY">The minimum Y value for the <see cref="Color" />.</param>
/// <param name="maxValueY">The maximum Y value for the <see cref="Color" />.</param>
/// <param name="minValueZ">The minimum Z value for the <see cref="Color" />.</param>
/// <param name="maxValueZ">The maximum Z value for the <see cref="Color" />.</param>
/// <param name="minValueW">The minimum W value for the <see cref="Color" />.</param>
/// <param name="maxValueW">The maximum W value for the <see cref="Color" />.</param>
/// <returns>The random <see cref="Color" />.</returns>
public static Color RandomColor(this Random random, byte minValueX, byte maxValueX, byte minValueY, byte maxValueY, byte minValueZ, byte maxValueZ, byte minValueW, byte maxValueW)
/// <param name="minValueX">The minimum X value for the <see cref="Rgba" />.</param>
/// <param name="maxValueX">The maximum X value for the <see cref="Rgba" />.</param>
/// <param name="minValueY">The minimum Y value for the <see cref="Rgba" />.</param>
/// <param name="maxValueY">The maximum Y value for the <see cref="Rgba" />.</param>
/// <param name="minValueZ">The minimum Z value for the <see cref="Rgba" />.</param>
/// <param name="maxValueZ">The maximum Z value for the <see cref="Rgba" />.</param>
/// <param name="minValueW">The minimum W value for the <see cref="Rgba" />.</param>
/// <param name="maxValueW">The maximum W value for the <see cref="Rgba" />.</param>
/// <returns>The random <see cref="Rgba" />.</returns>
public static Rgba RandomRgba(this Random random, byte minValueX, byte maxValueX, byte minValueY, byte maxValueY, byte minValueZ, byte maxValueZ, byte minValueW, byte maxValueW)
{
return new Color(random.RandomByte(minValueX, maxValueX), random.RandomByte(minValueY, maxValueY), random.RandomByte(minValueZ, maxValueZ), random.RandomByte(minValueW, maxValueW));
return new Rgba(random.RandomByte(minValueX, maxValueX), random.RandomByte(minValueY, maxValueY), random.RandomByte(minValueZ, maxValueZ), random.RandomByte(minValueW, maxValueW));
}

/// <summary>
/// Returns a random <see cref="Detach.Numerics.Color" /> with values that are all greater than or equal to <paramref name="minValue" />, and less than <paramref name="maxValue" />.
/// Returns a random <see cref="Rgba" /> with values that are all greater than or equal to <paramref name="minValue" />, and less than <paramref name="maxValue" />.
/// </summary>
/// <param name="random">The <see cref="Random" /> instance.</param>
/// <param name="minValue">The minimum value.</param>
/// <param name="maxValue">The maximum value.</param>
/// <returns>The random <see cref="Color" />.</returns>
public static Color RandomColor(this Random random, Color minValue, Color maxValue)
/// <returns>The random <see cref="Rgba" />.</returns>
public static Rgba RandomRgba(this Random random, Rgba minValue, Rgba maxValue)
{
return random.RandomColor(minValue.R, maxValue.R, minValue.G, maxValue.G, minValue.B, maxValue.B, minValue.A, maxValue.A);
return random.RandomRgba(minValue.R, maxValue.R, minValue.G, maxValue.G, minValue.B, maxValue.B, minValue.A, maxValue.A);
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Detach/Inline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public static ReadOnlySpan<char> Span(Quaternion value, ReadOnlySpan<char> forma
return _buffer.AsSpan(0, charsWritten);
}

public static ReadOnlySpan<char> Span(Color value, ReadOnlySpan<char> format = default, IFormatProvider? provider = default)
public static ReadOnlySpan<char> Span(Rgba value, ReadOnlySpan<char> format = default, IFormatProvider? provider = default)
{
int charsWritten = 0;
TryWrite(_buffer, ref charsWritten, value.R, format, provider);
Expand Down
179 changes: 0 additions & 179 deletions src/Detach/Numerics/Color.cs

This file was deleted.

Loading

0 comments on commit 211743f

Please sign in to comment.