#region License
/* FNA - XNA4 Reimplementation for Desktop Platforms
* Copyright 2009-2016 Ethan Lee and the MonoGame Team
*
* Released under the Microsoft Public License.
* See LICENSE for details.
*/
#endregion
#region Using Statements
using System;
#endregion
namespace Microsoft.Xna.Framework.Graphics.PackedVector
{
///
/// 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 : IPackedVector, IEquatable, IPackedVector
{
#region Public Properties
///
/// Gets and sets the packed value.
///
[CLSCompliant(false)]
public UInt16 PackedValue
{
get
{
return packedValue;
}
set
{
packedValue = value;
}
}
#endregion
#region Private Variables
private UInt16 packedValue;
#endregion
#region Public Constructors
///
/// Creates a new instance of Bgr565.
///
/// The x component
/// The y component
/// The z component
public Bgr565(float x, float y, float z)
{
packedValue = Pack(x, y, z);
}
///
/// Creates a new instance of Bgr565.
///
///
/// Vector containing the components for the packed vector.
///
public Bgr565(Vector3 vector)
{
packedValue = Pack(vector.X, vector.Y, vector.Z);
}
#endregion
#region Public Methods
///
/// Gets the packed vector in Vector3 format.
///
/// The packed vector in Vector3 format
public Vector3 ToVector3()
{
return new Vector3(
(float) (((packedValue >> 11) & 0x1F) * (1.0f / 31.0f)),
(float) (((packedValue >> 5) & 0x3F) * (1.0f / 63.0f)),
(float) ((packedValue & 0x1F) * (1.0f / 31.0f))
);
}
#endregion
#region IPackedVector Methods
///
/// Sets the packed vector from a Vector4.
///
/// Vector containing the components.
void IPackedVector.PackFromVector4(Vector4 vector)
{
packedValue = (UInt16) (
(((int) (vector.X * 31.0f) & 0x1F) << 11) |
(((int) (vector.Y * 63.0f) & 0x3F) << 5) |
((int) (vector.Z * 31.0f) & 0x1F)
);
}
///
/// Gets the packed vector in Vector4 format.
///
/// The packed vector in Vector4 format
Vector4 IPackedVector.ToVector4()
{
return new Vector4(ToVector3(), 1.0f);
}
#endregion
#region Public Static Operators and Override Methods
///
/// Compares an object with the packed vector.
///
/// The object to compare.
/// True if the object is equal to the packed vector.
public override bool Equals(object obj)
{
return (obj is Bgr565) && Equals((Bgr565) obj);
}
///
/// Compares another Bgr565 packed vector with the packed vector.
///
/// The Bgr565 packed vector to compare.
/// True if the packed vectors are equal.
public bool Equals(Bgr565 other)
{
return packedValue == other.packedValue;
}
///
/// Gets a string representation of the packed vector.
///
/// A string representation of the packed vector.
public override string ToString()
{
return ToVector3().ToString();
}
///
/// Gets a hash code of the packed vector.
///
/// The hash code for the packed vector.
public override int GetHashCode()
{
return packedValue.GetHashCode();
}
public static bool operator ==(Bgr565 lhs, Bgr565 rhs)
{
return lhs.packedValue == rhs.packedValue;
}
public static bool operator !=(Bgr565 lhs, Bgr565 rhs)
{
return lhs.packedValue != rhs.packedValue;
}
#endregion
#region Private Static Pack Method
private static UInt16 Pack(float x, float y, float z)
{
return (UInt16) (
(((int) (MathHelper.Clamp(x, 0, 1) * 31.0f) & 0x1F) << 11) |
(((int) (MathHelper.Clamp(y, 0, 1) * 63.0f) & 0x3F) << 5) |
((int) (MathHelper.Clamp(z, 0, 1) * 31.0f) & 0x1F)
);
}
#endregion
}
}