diff --git a/openVulkanoCpp/Math/RGBA5551.hpp b/openVulkanoCpp/Math/RGBA5551.hpp index d3f698f..e45c6c0 100644 --- a/openVulkanoCpp/Math/RGBA5551.hpp +++ b/openVulkanoCpp/Math/RGBA5551.hpp @@ -7,15 +7,16 @@ #pragma once #include "Math/Math.hpp" +#include 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(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 || std::is_signed_v, bool> = true> + RGBA5551(const Math::Vector4& color) + : b(Make5(color.b)), g(Make5(color.g)), r(Make5(color.r)), a(color.a > 128) + { + } + + template, bool> = true> + RGBA5551(const Math::Vector4& 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 || std::is_signed_v, bool> = true> + Math::Vector4 Get4() + { + return { GetR(), GetG(), GetB(), GetA() }; + } + + template, bool> = true> + Math::Vector4 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, bool> = true> + RGBA5551& operator=(const Math::Vector4& color) + { + SetR_Normalized(color.r); + SetG_Normalized(color.g); + SetB_Normalized(color.b); + SetA_Normalized(color.a); + return *this; + } + + template || std::is_signed_v, bool> = true> + RGBA5551& operator=(const Math::Vector4& color) + { + SetR(color.r); + SetG(color.g); + SetB(color.b); + SetA(color.a); + return *this; + } }; + + static_assert(sizeof(RGBA5551) == 2); } \ No newline at end of file