finished RGB565

This commit is contained in:
Metehan Tuncbilek
2024-08-08 14:26:47 +03:00
parent 3370f0ed85
commit 2df7280057

View File

@@ -17,9 +17,9 @@ namespace OpenVulkano::Math
static uint16_t Make6(uint8_t val) { return val * 63.0f / 255.0f; }
static uint8_t Unmake5(uint16_t val) { return static_cast<uint8_t>(val * 255.0f / 31.0f); }
static uint8_t Unmake6(uint16_t val) { return static_cast<uint8_t>(val * 255.0f / 63.0f); }
static float Unmake5ToFloat(uint16_t val) {return std::clamp(val / 31.0f, 0.0f, 1.0f); }
static float Unmake5ToFloat(uint16_t val) { return std::clamp(val / 31.0f, 0.0f, 1.0f); }
static float Unmake6ToFloat(uint16_t val) { return std::clamp(val / 63.0f, 0.0f, 1.0f); }
static uint16_t Make5FromFloat(float val) { return (uint16_t)std::clamp(val * 31.0f, 0.0f, 31.f); }
static uint16_t Make5FromFloat(float val) { return (uint16_t) std::clamp(val * 31.0f, 0.0f, 31.f); }
static uint16_t Make6FromFloat(float val) { return (uint16_t) std::clamp(val * 63.0f, 0.0f, 63.f); }
public:
@@ -34,39 +34,312 @@ namespace OpenVulkano::Math
};
};
RGB565(Math::Vector4uc color = { 0, 0, 0, 1 }) : b(Make5(color.b)), g(Make6(color.g)), r(Make5(color.r)) {}
RGB565(Math::Vector3uc color = { 0, 0, 0 }) : b(Make5(color.b)), g(Make6(color.g)), r(Make5(color.r)) {}
RGB565(Math::Vector4uc_SIMD color = { 0, 0, 0, 1 }) : b(Make5(color.b)), g(Make6(color.g)), r(Make5(color.r)) {}
RGB565(Math::Vector3uc_SIMD color = { 0, 0, 0 }) : b(Make5(color.b)), g(Make6(color.g)), r(Make5(color.r)) {}
RGB565(Math::Vector4f color = {0.f, 0.f, 0.f, 1.f}) : b(Make5FromFloat(color.b)), g(Make6FromFloat(color.g)), r(Make5FromFloat(color.r)) {}
RGB565(Math::Vector3f color = { 0.f, 0.f, 0.f }) : b(Make5FromFloat(color.b)), g(Make6FromFloat(color.g)), r(Make5FromFloat(color.r)) {}
RGB565(Math::Vector3f_SIMD color = { 0.f, 0.f, 0.f }) : b(Make5FromFloat(color.b)), g(Make6FromFloat(color.g)), r(Make5FromFloat(color.r)) {}
RGB565(Math::Vector4f_SIMD color = { 0.f, 0.f, 0.f, 1.f }) : b(Make5FromFloat(color.b)), g(Make6FromFloat(color.g)), r(Make5FromFloat(color.r)) {}
template<typename T, typename = std::enable_if_t<std::is_unsigned_v<T> || std::is_signed_v<T>>>
RGB565(const Math::Vector3<T>& color) : b(Make5(color.b)), g(Make6(color.g)), r(Make5(color.r))
{
}
template<typename T, typename = std::enable_if_t<std::is_unsigned_v<T> || std::is_signed_v<T>>>
RGB565(const Math::Vector4<T>& color) : b(Make5(color.b)), g(Make6(color.g)), r(Make5(color.r))
{
}
template<typename T, typename = std::enable_if_t<std::is_unsigned_v<T> || std::is_signed_v<T>>>
RGB565(const Math::Vector3_SIMD<T>& color) : b(Make5(color.b)), g(Make6(color.g)), r(Make5(color.r))
{
}
template<typename T, typename = std::enable_if_t<std::is_unsigned_v<T> || std::is_signed_v<T>>>
RGB565(const Math::Vector4_SIMD<T>& color) : b(Make5(color.b)), g(Make6(color.g)), r(Make5(color.r))
{
}
template<typename T, typename = std::enable_if_t<std::is_floating_point_v<T>>>
RGB565(const Math::Vector3<T>& color)
: b(Make5FromFloat(color.b)), g(Make6FromFloat(color.g)), r(Make5FromFloat(color.r))
{
}
template<typename T, typename = std::enable_if_t<std::is_floating_point_v<T>>>
RGB565(const Math::Vector4<T>& color)
: b(Make5FromFloat(color.b)), g(Make6FromFloat(color.g)), r(Make5FromFloat(color.r))
{
}
template<typename T, typename = std::enable_if_t<std::is_floating_point_v<T>>>
RGB565(const Math::Vector3_SIMD<T>& color)
: b(Make5FromFloat(color.b)), g(Make6FromFloat(color.g)), r(Make5FromFloat(color.r))
{
}
template<typename T, typename = std::enable_if_t<std::is_floating_point_v<T>>>
RGB565(const Math::Vector4_SIMD<T>& color)
: b(Make5FromFloat(color.b)), g(Make6FromFloat(color.g)), r(Make5FromFloat(color.r))
{
}
uint8_t GetR() { return Unmake5(r); }
uint8_t GetG() { return Unmake6(g); }
uint8_t GetB() { return Unmake5(b); }
Math::Vector4uc GetColor() { return { GetR(), GetG(), GetB(), 1 }; }
template<typename T, typename = std::enable_if_t<std::is_unsigned_v<T> || std::is_signed_v<T>>>
Math::Vector3<T> Get3()
{
return { GetR(), GetG(), GetB() };
}
template<typename T, typename = std::enable_if_t<std::is_unsigned_v<T> || std::is_signed_v<T>>>
Math::Vector4<T> Get4()
{
return { GetR(), GetG(), GetB(), 1 };
}
template<typename T, typename = std::enable_if_t<std::is_unsigned_v<T> || std::is_signed_v<T>>>
Math::Vector3_SIMD<T> GetSIMD()
{
return { GetR(), GetG(), GetB() };
}
template<typename T, typename = std::enable_if_t<std::is_unsigned_v<T> || std::is_signed_v<T>>>
Math::Vector4_SIMD<T> Get4SIMD()
{
return { GetR(), GetG(), GetB(), 1 };
}
template<typename T, typename = std::enable_if_t<std::is_floating_point_v<T>>> Math::Vector3<T> Get3Normalized()
{
return { GetRFloat(), GetGFloat(), GetBFloat() };
}
template<typename T, typename = std::enable_if_t<std::is_floating_point_v<T>>> Math::Vector4<T> Get4Normalized()
{
return { GetRFloat(), GetGFloat(), GetBFloat(), 1 };
}
template<typename T, typename = std::enable_if_t<std::is_floating_point_v<T>>>
Math::Vector3_SIMD<T> Get3NormalizedSIMD()
{
return { GetRFloat(), GetGFloat(), GetBFloat() };
}
template<typename T, typename = std::enable_if_t<std::is_floating_point_v<T>>>
Math::Vector4_SIMD<T> Get4NormalizedSIMD()
{
return { GetRFloat(), GetGFloat(), GetBFloat(), 1 };
}
void SetR(uint8_t red) { r = Make5(red); }
void SetG(uint8_t green) { g = Make6(green); }
void SetB(uint8_t blue) { b = Make5(blue); }
void SetColor(Math::Vector4uc color) { b = Make5(color.b); g = Make6(color.g); r = Make5(color.r); }
template<typename T, typename = std::enable_if_t<std::is_unsigned_v<T> || std::is_signed_v<T>>>
void Set(const Math::Vector3<T>& color)
{
r = Make5(color.r);
g = Make6(color.g);
b = Make5(color.b);
}
template<typename T, typename = std::enable_if_t<std::is_unsigned_v<T> || std::is_signed_v<T>>>
void Set(const Math::Vector4<T>& color)
{
r = Make5(color.r);
g = Make6(color.g);
b = Make5(color.b);
}
template<typename T, typename = std::enable_if_t<std::is_floating_point_v<T>>>
void SetNormalized(const Math::Vector3<T>& color)
{
r = Make5FromFloat(color.r);
g = Make6FromFloat(color.g);
b = Make5FromFloat(color.b);
}
template<typename T, typename = std::enable_if_t<std::is_floating_point_v<T>>>
void SetNormalized(const Math::Vector4<T>& color)
{
r = Make5FromFloat(color.r);
g = Make6FromFloat(color.g);
b = Make5FromFloat(color.b);
}
float GetRFloat() { return Unmake5ToFloat(r); }
float GetGFloat() { return Unmake6ToFloat(g); }
float GetBFloat() { return Unmake5ToFloat(b); }
Math::Vector4f GetColorFloat() { return { GetRFloat(), GetGFloat(), GetBFloat(), 1.0f }; }
void SetRNormalized(float red) { r = Make5FromFloat(red); }
void SetGNormalized(float green) { g = Make6FromFloat(green); }
void SetBNormalized(float blue) { b = Make5FromFloat(blue); }
void SetColorNormalized(Math::Vector4f color) { b = Make5FromFloat(color.b); g = Make6FromFloat(color.g); r = Make5FromFloat(color.r); }
RGB565& operator=(const RGB565& other)
{
value = other.value;
return *this;
}
RGB565& operator=(RGB565&& other) noexcept
{
value = other.value;
return *this;
}
template<typename T, typename = std::enable_if_t<std::is_unsigned_v<T> || std::is_signed_v<T>>>
RGB565& operator=(const Vector3<T>& color)
{
Set(color);
return *this;
}
template<typename T, typename = std::enable_if_t<std::is_unsigned_v<T> || std::is_signed_v<T>>>
RGB565& operator=(const Vector4<T>& color)
{
Set(color);
return *this;
}
template<typename T, typename = std::enable_if_t<std::is_unsigned_v<T> || std::is_signed_v<T>>>
RGB565& operator=(const Vector3_SIMD<T>& color)
{
Set(color);
return *this;
}
template<typename T, typename = std::enable_if_t<std::is_unsigned_v<T> || std::is_signed_v<T>>>
RGB565& operator=(const Vector4_SIMD<T>& color)
{
Set(color);
return *this;
}
template<typename T, typename = std::enable_if_t<std::is_floating_point_v<T>>>
RGB565& operator=(const Vector3<T>& color)
{
SetNormalized(color);
return *this;
}
template<typename T, typename = std::enable_if_t<std::is_floating_point_v<T>>>
RGB565& operator=(const Vector4<T>& color)
{
SetNormalized(color);
return *this;
}
template<typename T, typename = std::enable_if_t<std::is_floating_point_v<T>>>
RGB565& operator=(const Vector3_SIMD<T>& color)
{
SetNormalized(color);
return *this;
}
template<typename T, typename = std::enable_if_t<std::is_floating_point_v<T>>>
RGB565& operator=(const Vector4_SIMD<T>& color)
{
SetNormalized(color);
return *this;
}
template<typename T, typename = std::enable_if_t<std::is_unsigned_v<T> || std::is_signed_v<T>>>
RGB565& operator+=(const Vector3<T>& color)
{
Set(Get3<T>() + color);
return *this;
}
template<typename T, typename = std::enable_if_t<std::is_unsigned_v<T> || std::is_signed_v<T>>>
RGB565& operator+=(const Vector4<T>& color)
{
Set(Get4<T>() + color);
return *this;
}
template<typename T, typename = std::enable_if_t<std::is_unsigned_v<T> || std::is_signed_v<T>>>
RGB565& operator+=(const Vector3_SIMD<T>& color)
{
Set(Get3SIMD<T>() + color);
return *this;
}
template<typename T, typename = std::enable_if_t<std::is_unsigned_v<T> || std::is_signed_v<T>>>
RGB565& operator+=(const Vector4_SIMD<T>& color)
{
Set(Get4SIMD<T>() + color);
return *this;
}
template<typename T, typename = std::enable_if_t<std::is_floating_point_v<T>>>
RGB565& operator+=(const Vector3<T>& color)
{
SetNormalized(Get3Normalized<T>() + color);
return *this;
}
template<typename T, typename = std::enable_if_t<std::is_floating_point_v<T>>>
RGB565& operator+=(const Vector4<T>& color)
{
SetNormalized(Get4Normalized<T>() + color);
return *this;
}
template<typename T, typename = std::enable_if_t<std::is_floating_point_v<T>>>
RGB565& operator+=(const Vector3_SIMD<T>& color)
{
SetNormalized(Get3NormalizedSIMD<T>() + color);
return *this;
}
template<typename T, typename = std::enable_if_t<std::is_floating_point_v<T>>>
RGB565& operator+=(const Vector4_SIMD<T>& color)
{
SetNormalized(Get4NormalizedSIMD<T>() + color);
return *this;
}
template<typename T, typename = std::enable_if_t<std::is_unsigned_v<T> || std::is_signed_v<T>>>
RGB565& operator-=(const Vector3<T>& color)
{
Set(Get3<T>() - color);
return *this;
}
template<typename T, typename = std::enable_if_t<std::is_unsigned_v<T> || std::is_signed_v<T>>>
RGB565& operator-=(const Vector4<T>& color)
{
Set(Get4<T>() - color);
return *this;
}
template<typename T, typename = std::enable_if_t<std::is_unsigned_v<T> || std::is_signed_v<T>>>
RGB565& operator-=(const Vector3_SIMD<T>& color)
{
Set(Get3SIMD<T>() - color);
return *this;
}
template<typename T, typename = std::enable_if_t<std::is_unsigned_v<T> || std::is_signed_v<T>>>
RGB565& operator-=(const Vector4_SIMD<T>& color)
{
Set(Get4SIMD<T>() - color);
return *this;
}
template<typename T, typename = std::enable_if_t<std::is_floating_point_v<T>>>
RGB565& operator-=(const Vector3<T>& color)
{
SetNormalized(Get3Normalized<T>() - color);
return *this;
}
template<typename T, typename = std::enable_if_t<std::is_floating_point_v<T>>>
RGB565& operator-=(const Vector4<T>& color)
{
SetNormalized(Get4Normalized<T>() - color);
return *this;
}
template<typename T, typename = std::enable_if_t<std::is_floating_point_v<T>>>
RGB565& operator-=(const Vector3_SIMD<T>& color)
{
SetNormalized(Get3NormalizedSIMD<T>() - color);
return *this;
}
template<typename T, typename = std::enable_if_t<std::is_floating_point_v<T>>>
RGB565& operator-=(const Vector4_SIMD<T>& color)
{
SetNormalized(Get4NormalizedSIMD<T>() - color);
return *this;
}
bool operator==(const RGB565& other) const { return value == other.value; }
bool operator!=(const RGB565& other) const { return value != other.value; }
};
static_assert(sizeof(RGB565) == 2);