From 3e20e8f06cd76896e86a7b2fe2c99d37e023163d Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Wed, 21 Oct 2026 17:55:28 +1000 Subject: [PATCH] Add missing Bgra32 constructors --- .../PixelImplementations/Bgra32.cs | 44 +++++++++++++++++++ .../PixelFormats/Bgra32Tests.cs | 38 ++++++++++++++++ 2 files changed, 82 insertions(+) diff --git a/src/ImageSharp/PixelFormats/PixelImplementations/Bgra32.cs b/src/ImageSharp/PixelFormats/PixelImplementations/Bgra32.cs index e8405dd2b9..ea4ae409f5 100644 --- a/src/ImageSharp/PixelFormats/PixelImplementations/Bgra32.cs +++ b/src/ImageSharp/PixelFormats/PixelImplementations/Bgra32.cs @@ -73,6 +73,50 @@ public Bgra32(byte r, byte g, byte b, byte a) this.A = a; } + /// + /// Initializes a new instance of the struct from normalized components. + /// + /// The red component. + /// The green component. + /// The blue component. + /// The alpha component. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Bgra32(float r, float g, float b, float a = 1) + : this(new Vector4(r, g, b, a)) + { + } + + /// + /// Initializes a new instance of the struct from normalized RGB components with opaque alpha. + /// + /// The red, green, and blue components. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Bgra32(Vector3 vector) + : this(new Vector4(vector, 1F)) + { + } + + /// + /// Initializes a new instance of the struct from normalized RGBA components. + /// + /// The red, green, blue, and alpha components. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Bgra32(Vector4 vector) + : this() => this = Pack(vector); + + /// + /// Initializes a new instance of the struct from its packed representation. + /// + /// + /// The bytes use B, G, R, A memory order. Integer interpretation follows the platform byte order. + /// On little-endian systems, the integer layout is 0xAARRGGBB. + /// This constructor copies the stored bits without color conversion or alpha association. + /// + /// The packed component bits. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Bgra32(uint packed) + : this() => this.PackedValue = packed; + /// /// Gets or sets the packed representation of the Bgra32 struct. /// diff --git a/tests/ImageSharp.Tests/PixelFormats/Bgra32Tests.cs b/tests/ImageSharp.Tests/PixelFormats/Bgra32Tests.cs index 277975896e..c4d309eaaa 100644 --- a/tests/ImageSharp.Tests/PixelFormats/Bgra32Tests.cs +++ b/tests/ImageSharp.Tests/PixelFormats/Bgra32Tests.cs @@ -10,6 +10,44 @@ namespace SixLabors.ImageSharp.Tests.PixelFormats; [Trait("Category", "PixelFormats")] public class Bgra32Tests { + /// + /// Verifies that packed construction retains the stored bits and BGRA byte order. + /// + [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); + } + + /// + /// Verifies normalized component and vector constructors, including bounds and implicit opaque alpha. + /// + [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))); + } + /// /// Tests the equality operators for equality. ///