namespace GenericImage.PackedVectors
{
using System;
using System.Numerics;
///
/// Packed vector type containing four 8-bit unsigned normalized values ranging from 0 to 1.
///
public struct Rgba32 : IPackedVector, IEquatable
{
///
/// Initializes a new instance of the struct.
///
/// The red component.
/// The green component.
/// The blue component.
/// The alpha component.
public Rgba32(float r, float g, float b, float a)
{
Vector4 clamped = Vector4.Clamp(new Vector4(r, g, b, a), Vector4.Zero, Vector4.One) * 255f;
this.PackedValue = Pack(ref clamped);
}
///
/// Initializes a new instance of the struct.
///
///
/// The vector containing the components for the packed vector.
///
public Rgba32(Vector4 vector)
{
Vector4 clamped = Vector4.Clamp(vector, Vector4.Zero, Vector4.One) * 255f;
this.PackedValue = Pack(ref clamped);
}
///
public uint 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 current left is equal to the parameter; otherwise, false.
///
public static bool operator ==(Rgba32 left, Rgba32 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 current left is equal to the parameter; otherwise, false.
///
public static bool operator !=(Rgba32 left, Rgba32 right)
{
return left.PackedValue != right.PackedValue;
}
///
public void PackVector(Vector4 vector)
{
Vector4 clamped = Vector4.Clamp(vector, Vector4.Zero, Vector4.One) * 255f;
this.PackedValue = Pack(ref clamped);
}
///
public void PackBytes(byte x, byte y, byte z, byte w)
{
Vector4 vector = new Vector4(x, y, z, w);
this.PackedValue = Pack(ref vector);
}
///
public Vector4 ToVector4()
{
return new Vector4(
(this.PackedValue >> 16) & 0xFF,
(this.PackedValue >> 8) & 0xFF,
this.PackedValue & 0xFF,
(this.PackedValue >> 24) & 0xFF) / 255f;
}
///
public byte[] ToBytes()
{
return new[]
{
(byte)((this.PackedValue >> 16) & 0xFF),
(byte)((this.PackedValue >> 8) & 0xFF),
(byte)(this.PackedValue & 0xFF),
(byte)((this.PackedValue >> 24) & 0xFF)
};
}
///
public override bool Equals(object obj)
{
return (obj is Rgba32) && this.Equals((Rgba32)obj);
}
///
public bool Equals(Rgba32 other)
{
return this.PackedValue == other.PackedValue;
}
///
/// Gets a string representation of the packed vector.
///
/// A string representation of the packed vector.
public override string ToString()
{
return this.ToVector4().ToString();
}
///
public override int GetHashCode()
{
return this.GetHashCode(this);
}
///
/// Sets the packed representation from the given component values.
///
///
/// The vector containing the components for the packed vector.
///
///
/// The .
///
private static uint Pack(ref Vector4 vector)
{
return ((uint)Math.Round(vector.X) << 16) |
((uint)Math.Round(vector.Y) << 8) |
(uint)Math.Round(vector.Z) |
((uint)Math.Round(vector.W) << 24);
}
///
/// Returns the hash code for this instance.
///
///
/// The instance of to return the hash code for.
///
///
/// A 32-bit signed integer that is the hash code for this instance.
///
private int GetHashCode(Rgba32 packed)
{
return packed.PackedValue.GetHashCode();
}
}
}