diff --git a/src/ImageSharp/Colors/Color.cs b/src/ImageSharp/Colors/Color.cs
index 1435377a57..c6c33edda0 100644
--- a/src/ImageSharp/Colors/Color.cs
+++ b/src/ImageSharp/Colors/Color.cs
@@ -219,7 +219,7 @@ namespace ImageSharp
/// The on the left side of the operand.
/// The on the right side of the operand.
///
- /// True if the parameter is equal to the parameter; otherwise, false.
+ /// True if the parameter is not equal to the parameter; otherwise, false.
///
public static bool operator !=(Color left, Color right)
{
diff --git a/src/ImageSharp/Colors/PackedPixel/Alpha8.cs b/src/ImageSharp/Colors/PackedPixel/Alpha8.cs
index 72011f30db..5f2bf86d45 100644
--- a/src/ImageSharp/Colors/PackedPixel/Alpha8.cs
+++ b/src/ImageSharp/Colors/PackedPixel/Alpha8.cs
@@ -50,7 +50,7 @@ namespace ImageSharp
/// The on the left side of the operand.
/// The on the right side of the operand.
///
- /// True if the parameter is equal to the parameter; otherwise, false.
+ /// True if the parameter is not equal to the parameter; otherwise, false.
///
public static bool operator !=(Alpha8 left, Alpha8 right)
{
diff --git a/src/ImageSharp/Colors/PackedPixel/Bgr565.cs b/src/ImageSharp/Colors/PackedPixel/Bgr565.cs
new file mode 100644
index 0000000000..23558f305c
--- /dev/null
+++ b/src/ImageSharp/Colors/PackedPixel/Bgr565.cs
@@ -0,0 +1,171 @@
+//
+// Copyright (c) James Jackson-South and contributors.
+// Licensed under the Apache License, Version 2.0.
+//
+
+namespace ImageSharp
+{
+ using System;
+ using System.Numerics;
+
+ ///
+ /// Packed vector type containing unsigned normalized values ranging from 0 to 1.
+ /// The x and z components use 5 bits, and the y component uses 6 bits.
+ ///
+ public struct Bgr565 : IPackedPixel, IEquatable
+ {
+ ///
+ /// Initializes a new instance of the struct.
+ ///
+ /// The x-component
+ /// The y-component
+ /// The z-component
+ public Bgr565(float x, float y, float z)
+ {
+ this.PackedValue = Pack(x, y, z);
+ }
+
+ ///
+ /// Initializes a new instance of the struct.
+ ///
+ ///
+ /// The vector containing the components for the packed value.
+ ///
+ public Bgr565(Vector3 vector)
+ {
+ this.PackedValue = Pack(vector.X, vector.Y, vector.Z);
+ }
+
+ ///
+ public ushort PackedValue { get; set; }
+
+ ///
+ /// Compares two objects for equality.
+ ///
+ /// The on the left side of the operand.
+ /// The on the right side of the operand.
+ ///
+ /// True if the parameter is equal to the parameter; otherwise, false.
+ ///
+ public static bool operator ==(Bgr565 left, Bgr565 right)
+ {
+ return left.PackedValue == right.PackedValue;
+ }
+
+ ///
+ /// Compares two objects for equality.
+ ///
+ /// The on the left side of the operand.
+ /// The on the right side of the operand.
+ ///
+ /// True if the parameter is not equal to the parameter; otherwise, false.
+ ///
+ public static bool operator !=(Bgr565 left, Bgr565 right)
+ {
+ return left.PackedValue != right.PackedValue;
+ }
+
+ ///
+ /// Expands the packed representation into a .
+ /// The vector components are typically expanded in least to greatest significance order.
+ ///
+ /// The .
+ public Vector3 ToVector3()
+ {
+ return new Vector3(
+ ((this.PackedValue >> 11) & 0x1F) * (1F / 31F),
+ ((this.PackedValue >> 5) & 0x3F) * (1F / 63F),
+ (this.PackedValue & 0x1F) * (1F / 31F));
+ }
+
+ ///
+ public void PackFromVector4(Vector4 vector)
+ {
+ this.PackedValue = Pack(vector.X, vector.Y, vector.Z);
+ }
+
+ ///
+ public Vector4 ToVector4()
+ {
+ return new Vector4(this.ToVector3(), 1F);
+ }
+
+ ///
+ public void PackFromBytes(byte x, byte y, byte z, byte w)
+ {
+ this.PackFromVector4(new Vector4(x, y, z, w) / 255F);
+ }
+
+ ///
+ public void ToBytes(byte[] bytes, int startIndex, ComponentOrder componentOrder)
+ {
+ Vector4 vector = this.ToVector4() * 255F;
+
+ switch (componentOrder)
+ {
+ case ComponentOrder.ZYX:
+ bytes[startIndex] = (byte)vector.Z;
+ bytes[startIndex + 1] = (byte)vector.Y;
+ bytes[startIndex + 2] = (byte)vector.X;
+ break;
+ case ComponentOrder.ZYXW:
+ bytes[startIndex] = (byte)vector.Z;
+ bytes[startIndex + 1] = (byte)vector.Y;
+ bytes[startIndex + 2] = (byte)vector.X;
+ bytes[startIndex + 3] = (byte)vector.W;
+ break;
+ case ComponentOrder.XYZ:
+ bytes[startIndex] = (byte)vector.X;
+ bytes[startIndex + 1] = (byte)vector.Y;
+ bytes[startIndex + 2] = (byte)vector.Z;
+ break;
+ case ComponentOrder.XYZW:
+ bytes[startIndex] = (byte)vector.X;
+ bytes[startIndex + 1] = (byte)vector.Y;
+ bytes[startIndex + 2] = (byte)vector.Z;
+ bytes[startIndex + 3] = (byte)vector.W;
+ break;
+ default:
+ throw new NotSupportedException();
+ }
+ }
+
+ ///
+ public override bool Equals(object obj)
+ {
+ return (obj is Bgr565) && this.Equals((Bgr565)obj);
+ }
+
+ ///
+ public bool Equals(Bgr565 other)
+ {
+ return this.PackedValue == other.PackedValue;
+ }
+
+ ///
+ public override string ToString()
+ {
+ return this.ToVector3().ToString();
+ }
+
+ ///
+ public override int GetHashCode()
+ {
+ return this.PackedValue.GetHashCode();
+ }
+
+ ///
+ /// Packs a components into a .
+ ///
+ /// The x-component
+ /// The y-component
+ /// The z-component
+ /// The containing the packed values.
+ private static ushort Pack(float x, float y, float z)
+ {
+ return (ushort)((((int)Math.Round(x.Clamp(0, 1) * 31F) & 0x1F) << 11) |
+ (((int)Math.Round(y.Clamp(0, 1) * 63F) & 0x3F) << 5) |
+ ((int)Math.Round(z.Clamp(0, 1) * 31F) & 0x1F));
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/ImageSharp/Colors/PackedPixel/README.md b/src/ImageSharp/Colors/PackedPixel/README.md
new file mode 100644
index 0000000000..61500de68d
--- /dev/null
+++ b/src/ImageSharp/Colors/PackedPixel/README.md
@@ -0,0 +1,3 @@
+Pixel formats adapted and extended from:
+
+https://github.com/MonoGame/MonoGame
\ No newline at end of file
diff --git a/tests/ImageSharp.Tests/Colors/PackedVectorTests.cs b/tests/ImageSharp.Tests/Colors/PackedVectorTests.cs
index dd3e5e3b13..22bb179319 100644
--- a/tests/ImageSharp.Tests/Colors/PackedVectorTests.cs
+++ b/tests/ImageSharp.Tests/Colors/PackedVectorTests.cs
@@ -5,6 +5,7 @@
namespace ImageSharp.Tests.Colors
{
+ using System;
using System.Numerics;
using Xunit;
@@ -31,6 +32,7 @@ namespace ImageSharp.Tests.Colors
// Test ordering
Vector4 vector = new Alpha8(.5F).ToVector4();
+
Assert.Equal(vector.X, 0);
Assert.Equal(vector.Y, 0);
Assert.Equal(vector.Z, 0);
@@ -53,5 +55,72 @@ namespace ImageSharp.Tests.Colors
new Alpha8(.5F).ToBytes(rgb, 0, ComponentOrder.ZYXW);
Assert.Equal(bgra, new byte[] { 0, 0, 0, 128 });
}
+
+ [Fact]
+ public void Bgr565()
+ {
+ // Test the limits.
+ Assert.Equal(0x0, new Bgr565(Vector3.Zero).PackedValue);
+ Assert.Equal(0xFFFF, new Bgr565(Vector3.One).PackedValue);
+
+ // Test ToVector3.
+ Assert.True(Equal(Vector3.One, new Bgr565(Vector3.One).ToVector3()));
+ Assert.True(Equal(Vector3.Zero, new Bgr565(Vector3.Zero).ToVector3()));
+ Assert.True(Equal(Vector3.UnitX, new Bgr565(Vector3.UnitX).ToVector3()));
+ Assert.True(Equal(Vector3.UnitY, new Bgr565(Vector3.UnitY).ToVector3()));
+ Assert.True(Equal(Vector3.UnitZ, new Bgr565(Vector3.UnitZ).ToVector3()));
+
+ // Test clamping.
+ Assert.True(Equal(Vector3.Zero, new Bgr565(Vector3.One * -1234F).ToVector3()));
+ Assert.True(Equal(Vector3.One, new Bgr565(Vector3.One * 1234F).ToVector3()));
+
+ // Make sure the swizzle is correct.
+ Assert.Equal(0xF800, new Bgr565(Vector3.UnitX).PackedValue);
+ Assert.Equal(0x07E0, new Bgr565(Vector3.UnitY).PackedValue);
+ Assert.Equal(0x001F, new Bgr565(Vector3.UnitZ).PackedValue);
+
+ float x = 0.1F;
+ float y = -0.3F;
+ float z = 0.5F;
+ Assert.Equal(6160, new Bgr565(x, y, z).PackedValue);
+
+ byte[] rgb = new byte[3];
+ byte[] rgba = new byte[4];
+ byte[] bgr = new byte[3];
+ byte[] bgra = new byte[4];
+
+ new Bgr565(x, y, z).ToBytes(rgb, 0, ComponentOrder.XYZ);
+ Assert.Equal(rgb, new byte[] { 24, 0, 131 });
+
+ new Bgr565(x, y, z).ToBytes(rgba, 0, ComponentOrder.XYZW);
+ Assert.Equal(rgba, new byte[] { 24, 0, 131, 255 });
+
+ new Bgr565(x, y, z).ToBytes(bgr, 0, ComponentOrder.ZYX);
+ Assert.Equal(bgr, new byte[] { 131, 0, 24 });
+
+ new Bgr565(x, y, z).ToBytes(bgra, 0, ComponentOrder.ZYXW);
+ Assert.Equal(bgra, new byte[] { 131, 0, 24, 255 });
+ }
+
+ // Comparison helpers with small tolerance to allow for floating point rounding during computations.
+ public static bool Equal(float a, float b)
+ {
+ return Math.Abs(a - b) < 1e-5;
+ }
+
+ public static bool Equal(Vector2 a, Vector2 b)
+ {
+ return Equal(a.X, b.X) && Equal(a.Y, b.Y);
+ }
+
+ public static bool Equal(Vector3 a, Vector3 b)
+ {
+ return Equal(a.X, b.X) && Equal(a.Y, b.Y) && Equal(a.Z, b.Z);
+ }
+
+ public static bool Equal(Vector4 a, Vector4 b)
+ {
+ return Equal(a.X, b.X) && Equal(a.Y, b.Y) && Equal(a.Z, b.Z) && Equal(a.W, b.W);
+ }
}
}
\ No newline at end of file