Completed implementation of RGBA5551 class
This commit is contained in:
@@ -7,15 +7,16 @@
|
||||
#pragma once
|
||||
|
||||
#include "Math/Math.hpp"
|
||||
#include <algorithm>
|
||||
|
||||
namespace OpenVulkano::Math
|
||||
{
|
||||
class RGBA5551
|
||||
{
|
||||
uint16_t Make5(uint8_t val)
|
||||
{
|
||||
return val * 31.0f / 255.0f;
|
||||
}
|
||||
static uint16_t Make5(uint8_t val) { return 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); }
|
||||
|
||||
public:
|
||||
union
|
||||
@@ -30,11 +31,83 @@ namespace OpenVulkano::Math
|
||||
};
|
||||
};
|
||||
|
||||
RGBA5551(Math::Vector4uc color = {0, 0, 0, 1})
|
||||
: b(Make5(color.b))
|
||||
, g(Make5(color.g))
|
||||
, r(Make5(color.r))
|
||||
, a(color.a > 128)
|
||||
{}
|
||||
};
|
||||
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_unsigned_v<T> || std::is_signed_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::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; }
|
||||
|
||||
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; }
|
||||
|
||||
void SetR(uint8_t red) { r = Make5(red); }
|
||||
void SetG(uint8_t green) { g = Make5(green); }
|
||||
void SetB(uint8_t blue) { b = Make5(blue); }
|
||||
void SetA(uint8_t alpha) { a = (alpha > 128); }
|
||||
|
||||
void SetR_Normalized(float red) { r = Make5FromFloat(red); }
|
||||
void SetG_Normalized(float green) { g = Make5FromFloat(green); }
|
||||
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()
|
||||
{
|
||||
return { GetR(), GetG(), GetB(), GetA() };
|
||||
}
|
||||
|
||||
template<typename T, std::enable_if_t<std::is_floating_point_v<T>, bool> = true>
|
||||
Math::Vector4<T> Get4Normalized()
|
||||
{
|
||||
return { GetR_Normalized(), GetG_Normalized(), GetB_Normalized(), GetA_Normalized() };
|
||||
}
|
||||
|
||||
RGBA5551& operator=(const RGBA5551& other)
|
||||
{
|
||||
value = other.value;
|
||||
return *this;
|
||||
}
|
||||
|
||||
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::Vector4<T>& color)
|
||||
{
|
||||
SetR_Normalized(color.r);
|
||||
SetG_Normalized(color.g);
|
||||
SetB_Normalized(color.b);
|
||||
SetA_Normalized(color.a);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T, std::enable_if_t<std::is_unsigned_v<T> || std::is_signed_v<T>, bool> = true>
|
||||
RGBA5551& operator=(const Math::Vector4<T>& color)
|
||||
{
|
||||
SetR(color.r);
|
||||
SetG(color.g);
|
||||
SetB(color.b);
|
||||
SetA(color.a);
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
|
||||
static_assert(sizeof(RGBA5551) == 2);
|
||||
}
|
||||
Reference in New Issue
Block a user