Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions src/ImageSharp/PixelFormats/PixelImplementations/Bgra32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,50 @@ public Bgra32(byte r, byte g, byte b, byte a)
this.A = a;
}

/// <summary>
/// Initializes a new instance of the <see cref="Bgra32"/> struct from normalized components.
/// </summary>
/// <param name="r">The red component.</param>
/// <param name="g">The green component.</param>
/// <param name="b">The blue component.</param>
/// <param name="a">The alpha component.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Bgra32(float r, float g, float b, float a = 1)
: this(new Vector4(r, g, b, a))
{
}

/// <summary>
/// Initializes a new instance of the <see cref="Bgra32"/> struct from normalized RGB components with opaque alpha.
/// </summary>
/// <param name="vector">The red, green, and blue components.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Bgra32(Vector3 vector)
: this(new Vector4(vector, 1F))
{
}

/// <summary>
/// Initializes a new instance of the <see cref="Bgra32"/> struct from normalized RGBA components.
/// </summary>
/// <param name="vector">The red, green, blue, and alpha components.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Bgra32(Vector4 vector)
: this() => this = Pack(vector);

/// <summary>
/// Initializes a new instance of the <see cref="Bgra32"/> struct from its packed representation.
/// </summary>
/// <remarks>
/// The bytes use B, G, R, A memory order. Integer interpretation follows the platform byte order.
/// On little-endian systems, the integer layout is <c>0xAARRGGBB</c>.
/// This constructor copies the stored bits without color conversion or alpha association.
/// </remarks>
/// <param name="packed">The packed component bits.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Bgra32(uint packed)
: this() => this.PackedValue = packed;

/// <summary>
/// Gets or sets the packed representation of the Bgra32 struct.
/// </summary>
Expand Down
38 changes: 38 additions & 0 deletions tests/ImageSharp.Tests/PixelFormats/Bgra32Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,44 @@ namespace SixLabors.ImageSharp.Tests.PixelFormats;
[Trait("Category", "PixelFormats")]
public class Bgra32Tests
{
/// <summary>
/// Verifies that packed construction retains the stored bits and BGRA byte order.
/// </summary>
[Theory]
[InlineData(0U)]
[InlineData(uint.MaxValue)]
[InlineData(0xA1B2_C3D4U)]
public void Constructor_FromPackedValue(uint packed)
{
Bgra32 pixel = new(packed);
byte[] bytes = BitConverter.GetBytes(packed);

Assert.Equal(packed, pixel.PackedValue);
Assert.Equal(bytes[0], pixel.B);
Assert.Equal(bytes[1], pixel.G);
Assert.Equal(bytes[2], pixel.R);
Assert.Equal(bytes[3], pixel.A);
}

/// <summary>
/// Verifies normalized component and vector constructors, including bounds and implicit opaque alpha.
/// </summary>
[Theory]
[InlineData(1F, 0.1F, 0.133F, 0.5F, 255, 26, 34, 128)]
[InlineData(-1F, 2F, float.NaN, float.PositiveInfinity, 0, 255, 0, 255)]
public void Constructor_FromNormalizedComponents(float r, float g, float b, float a, byte expectedR, byte expectedG, byte expectedB, byte expectedA)
{
Bgra32 expected = new(expectedR, expectedG, expectedB, expectedA);

Assert.Equal(expected, new Bgra32(r, g, b, a));
Assert.Equal(expected, new Bgra32(new Vector4(r, g, b, a)));

expected.A = byte.MaxValue;

Assert.Equal(expected, new Bgra32(r, g, b));
Assert.Equal(expected, new Bgra32(new Vector3(r, g, b)));
}

/// <summary>
/// Tests the equality operators for equality.
/// </summary>
Expand Down
Loading