#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, using
/// 4 bits each for x, y, z, and w.
///
public struct Bgra4444 : IPackedVector, IEquatable
{
#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 Bgra4444.
///
/// The x component
/// The y component
/// The z component
/// The w component
public Bgra4444(float x, float y, float z, float w)
{
packedValue = Pack(x, y, z, w);
}
///
/// Creates a new instance of Bgra4444.
///
///
/// Vector containing the components for the packed vector.
///
public Bgra4444(Vector4 vector)
{
packedValue = Pack(vector.X, vector.Y, vector.Z, vector.W);
}
#endregion
#region Public Methods
///
/// Gets the packed vector in Vector4 format.
///
/// The packed vector in Vector4 format
public Vector4 ToVector4()
{
const float maxVal = 1 / 15.0f;
return new Vector4(
((packedValue >> 8) & 0x0F) * maxVal,
((packedValue >> 4) & 0x0F) * maxVal,
(packedValue & 0x0F) * maxVal,
((packedValue >> 12) & 0x0F) * maxVal
);
}
#endregion
#region IPackedVector Methods
///
/// Sets the packed vector from a Vector4.
///
/// Vector containing the components.
void IPackedVector.PackFromVector4(Vector4 vector)
{
packedValue = Pack(vector.X, vector.Y, vector.Z, vector.W);
}
#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 Bgra4444) && Equals((Bgra4444) obj);
}
///
/// Compares another Bgra4444 packed vector with the packed vector.
///
/// The Bgra4444 packed vector to compare.
/// True if the packed vectors are equal.
public bool Equals(Bgra4444 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 ToVector4().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 ==(Bgra4444 lhs, Bgra4444 rhs)
{
return lhs.packedValue == rhs.packedValue;
}
public static bool operator !=(Bgra4444 lhs, Bgra4444 rhs)
{
return lhs.packedValue != rhs.packedValue;
}
#endregion
#region Private Static Pack Method
private static UInt16 Pack(float x, float y, float z, float w)
{
return (UInt16) (
(((int) (MathHelper.Clamp(w, 0, 1) * 15.0f) & 0x0F) << 12) |
(((int) (MathHelper.Clamp(x, 0, 1) * 15.0f) & 0x0F) << 8) |
(((int) (MathHelper.Clamp(y, 0, 1) * 15.0f) & 0x0F) << 4) |
((int) (MathHelper.Clamp(z, 0, 1) * 15.0f) & 0x0F)
);
}
#endregion
}
}