diff --git a/src/ImageSharp/PixelFormats/Bgra32.cs b/src/ImageSharp/PixelFormats/Bgra32.cs
index 20dfda504..de660c05b 100644
--- a/src/ImageSharp/PixelFormats/Bgra32.cs
+++ b/src/ImageSharp/PixelFormats/Bgra32.cs
@@ -38,6 +38,16 @@ namespace SixLabors.ImageSharp.PixelFormats
///
public byte A;
+ ///
+ /// The maximum byte value.
+ ///
+ private static readonly Vector4 MaxBytes = new Vector4(255);
+
+ ///
+ /// The half vector value.
+ ///
+ private static readonly Vector4 Half = new Vector4(0.5F);
+
///
/// Initializes a new instance of the struct.
///
@@ -141,16 +151,14 @@ namespace SixLabors.ImageSharp.PixelFormats
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void PackFromVector4(Vector4 vector)
{
- var rgba = default(Rgba32);
- rgba.PackFromVector4(vector);
- this.PackFromRgba32(rgba);
+ this.Pack(ref vector);
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Vector4 ToVector4()
{
- return this.ToRgba32().ToVector4();
+ return new Vector4(this.R, this.G, this.B, this.A) / MaxBytes;
}
///
@@ -243,5 +251,21 @@ namespace SixLabors.ImageSharp.PixelFormats
/// The RGBA value
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Bgra32 ToBgra32() => this;
+
+ ///
+ /// Packs a into a color.
+ ///
+ /// The vector containing the values to pack.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ private void Pack(ref Vector4 vector) {
+ vector *= MaxBytes;
+ vector += Half;
+ vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes);
+
+ this.R = (byte)vector.X;
+ this.G = (byte)vector.Y;
+ this.B = (byte)vector.Z;
+ this.A = (byte)vector.W;
+ }
}
}
\ No newline at end of file