Added RGBA5551 tests file, changed template spec to use is_integral_v, implemented assignment operators, added casting operators
This commit is contained in:
@@ -13,7 +13,7 @@ namespace OpenVulkano::Math
|
||||
{
|
||||
class RGBA5551
|
||||
{
|
||||
static uint16_t Make5(uint8_t val) { return val * 31.0f / 255.0f; }
|
||||
static uint16_t Make5(uint8_t val) { return static_cast<uint16_t>(val * 31.0f / 255.0f); }
|
||||
static uint8_t Unmake5(uint16_t val) { return static_cast<uint8_t>(val * 255.0f / 31.0f); }
|
||||
static float Unmake5ToFloat(uint16_t val) { return std::clamp(val / 31.0f, 0.0f, 1.0f); }
|
||||
static uint16_t Make5FromFloat(float val) { return (uint16_t) std::clamp(val * 31.0f, 0.0f, 31.f); }
|
||||
@@ -33,29 +33,38 @@ namespace OpenVulkano::Math
|
||||
|
||||
RGBA5551() : r(0), g(0), b(0), a(1) {}
|
||||
|
||||
RGBA5551(Math::Vector4uc color) : b(Make5(color.b)), g(Make5(color.g)), r(Make5(color.r)), a(color.a > 128) {}
|
||||
template<typename T, std::enable_if_t<std::is_integral_v<T>, bool> = true>
|
||||
RGBA5551(const Math::Vector3<T>& color) : b(Make5(color.b)), g(Make5(color.g)), r(Make5(color.r)), a(1)
|
||||
{
|
||||
}
|
||||
|
||||
template<typename T, std::enable_if_t<std::is_unsigned_v<T> || std::is_signed_v<T>, bool> = true>
|
||||
template<typename T, std::enable_if_t<std::is_integral_v<T>, bool> = true>
|
||||
RGBA5551(const Math::Vector4<T>& color)
|
||||
: b(Make5(color.b)), g(Make5(color.g)), r(Make5(color.r)), a(color.a > 128)
|
||||
{
|
||||
}
|
||||
|
||||
template<typename T, std::enable_if_t<std::is_floating_point_v<T>, bool> = true>
|
||||
RGBA5551(const Math::Vector3<T>& color)
|
||||
: b(Make5FromFloat(color.b)), g(Make5FromFloat(color.g)), r(Make5FromFloat(color.r)), a(1)
|
||||
{
|
||||
}
|
||||
|
||||
template<typename T, std::enable_if_t<std::is_floating_point_v<T>, bool> = true>
|
||||
RGBA5551(const Math::Vector4<T>& color)
|
||||
: b(Make5FromFloat(color.b)), g(Make5FromFloat(color.g)), r(Make5FromFloat(color.r)), a(color.a > 0.5f)
|
||||
{
|
||||
}
|
||||
|
||||
uint8_t GetR() { return Unmake5(r); }
|
||||
uint8_t GetG() { return Unmake5(g); }
|
||||
uint8_t GetB() { return Unmake5(b); }
|
||||
uint8_t GetA() { return a ? 255 : 0; }
|
||||
uint8_t GetR() const { return Unmake5(r); }
|
||||
uint8_t GetG() const { return Unmake5(g); }
|
||||
uint8_t GetB() const { return Unmake5(b); }
|
||||
uint8_t GetA() const { return a ? 255 : 0; }
|
||||
|
||||
float GetR_Normalized() { return Unmake5ToFloat(r); }
|
||||
float GetG_Normalized() { return Unmake5ToFloat(g); }
|
||||
float GetB_Normalized() { return Unmake5ToFloat(b); }
|
||||
float GetA_Normalized() { return a ? 1.0f : 0.0f; }
|
||||
float GetR_Normalized() const { return Unmake5ToFloat(r); }
|
||||
float GetG_Normalized() const { return Unmake5ToFloat(g); }
|
||||
float GetB_Normalized() const { return Unmake5ToFloat(b); }
|
||||
float GetA_Normalized() const { return a; }
|
||||
|
||||
void SetR(uint8_t red) { r = Make5(red); }
|
||||
void SetG(uint8_t green) { g = Make5(green); }
|
||||
@@ -67,18 +76,30 @@ namespace OpenVulkano::Math
|
||||
void SetB_Normalized(float blue) { b = Make5FromFloat(blue); }
|
||||
void SetA_Normalized(float alpha) { a = (alpha > 0.5f); }
|
||||
|
||||
template<typename T, std::enable_if_t<std::is_unsigned_v<T> || std::is_signed_v<T>, bool> = true>
|
||||
Math::Vector4<T> Get4()
|
||||
template<typename T, std::enable_if_t<std::is_integral_v<T>, bool> = true>
|
||||
Math::Vector3<T> Get3() const
|
||||
{
|
||||
return { GetR(), GetG(), GetB() };
|
||||
}
|
||||
|
||||
template<typename T, std::enable_if_t<std::is_integral_v<T>, bool> = true>
|
||||
Math::Vector4<T> Get4() const
|
||||
{
|
||||
return { GetR(), GetG(), GetB(), GetA() };
|
||||
}
|
||||
|
||||
template<typename T, std::enable_if_t<std::is_floating_point_v<T>, bool> = true>
|
||||
Math::Vector4<T> Get4Normalized()
|
||||
Math::Vector4<T> Get4Normalized() const
|
||||
{
|
||||
return { GetR_Normalized(), GetG_Normalized(), GetB_Normalized(), GetA_Normalized() };
|
||||
}
|
||||
|
||||
template<typename T, std::enable_if_t<std::is_floating_point_v<T>, bool> = true>
|
||||
Math::Vector3<T> Get3Normalized() const
|
||||
{
|
||||
return { GetR_Normalized(), GetG_Normalized(), GetB_Normalized() };
|
||||
}
|
||||
|
||||
RGBA5551& operator=(const RGBA5551& other)
|
||||
{
|
||||
value = other.value;
|
||||
@@ -88,6 +109,16 @@ namespace OpenVulkano::Math
|
||||
bool operator==(const RGBA5551& other) const { return value == other.value; }
|
||||
bool operator!=(const RGBA5551& other) const { return value != other.value; }
|
||||
|
||||
template<typename T, std::enable_if_t<std::is_floating_point_v<T>, bool> = true>
|
||||
RGBA5551& operator=(const Math::Vector3<T>& color)
|
||||
{
|
||||
SetR_Normalized(color.r);
|
||||
SetG_Normalized(color.g);
|
||||
SetB_Normalized(color.b);
|
||||
SetA_Normalized(1);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T, std::enable_if_t<std::is_floating_point_v<T>, bool> = true>
|
||||
RGBA5551& operator=(const Math::Vector4<T>& color)
|
||||
{
|
||||
@@ -98,7 +129,17 @@ namespace OpenVulkano::Math
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T, std::enable_if_t<std::is_unsigned_v<T> || std::is_signed_v<T>, bool> = true>
|
||||
template<typename T, std::enable_if_t<std::is_integral_v<T>, bool> = true>
|
||||
RGBA5551& operator=(const Math::Vector3<T>& color)
|
||||
{
|
||||
SetR(color.r);
|
||||
SetG(color.g);
|
||||
SetB(color.b);
|
||||
SetA(255);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T, std::enable_if_t<std::is_integral_v<T>, bool> = true>
|
||||
RGBA5551& operator=(const Math::Vector4<T>& color)
|
||||
{
|
||||
SetR(color.r);
|
||||
@@ -107,6 +148,30 @@ namespace OpenVulkano::Math
|
||||
SetA(color.a);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T, std::enable_if_t<std::is_integral_v<T>, bool> = true>
|
||||
operator Math::Vector3<T>() const
|
||||
{
|
||||
return Get3<T>();
|
||||
}
|
||||
|
||||
template<typename T, std::enable_if_t<std::is_floating_point_v<T>, bool> = true>
|
||||
operator Math::Vector3<T>() const
|
||||
{
|
||||
return Get3Normalized<T>();
|
||||
}
|
||||
|
||||
template<typename T, std::enable_if_t<std::is_integral_v<T>, bool> = true>
|
||||
operator Math::Vector4<T>() const
|
||||
{
|
||||
return Get4<T>();
|
||||
}
|
||||
|
||||
template<typename T, std::enable_if_t<std::is_floating_point_v<T>, bool> = true>
|
||||
operator Math::Vector4<T>() const
|
||||
{
|
||||
return Get4Normalized<T>();
|
||||
}
|
||||
};
|
||||
|
||||
static_assert(sizeof(RGBA5551) == 2);
|
||||
|
||||
Reference in New Issue
Block a user