diff options
author | rtk0c <[email protected]> | 2022-06-30 21:38:53 -0700 |
---|---|---|
committer | rtk0c <[email protected]> | 2022-06-30 21:38:53 -0700 |
commit | 7fe47a9d5b1727a61dc724523b530762f6d6ba19 (patch) | |
tree | e95be6e66db504ed06d00b72c579565bab873277 /app/source/Cplt/Utils/Color.hpp | |
parent | 2cf952088d375ac8b2f45b144462af0953436cff (diff) |
Restructure project
Diffstat (limited to 'app/source/Cplt/Utils/Color.hpp')
-rw-r--r-- | app/source/Cplt/Utils/Color.hpp | 202 |
1 files changed, 202 insertions, 0 deletions
diff --git a/app/source/Cplt/Utils/Color.hpp b/app/source/Cplt/Utils/Color.hpp new file mode 100644 index 0000000..15fe6a1 --- /dev/null +++ b/app/source/Cplt/Utils/Color.hpp @@ -0,0 +1,202 @@ +#pragma once + +#include <Cplt/Utils/Math.hpp> +#include <Cplt/Utils/Vector.hpp> +#include <Cplt/Utils/fwd.hpp> + +#include <imgui.h> +#include <algorithm> +#include <cstdint> +#include <limits> + +class RgbaColor +{ +public: + uint8_t r; + uint8_t g; + uint8_t b; + uint8_t a; + +public: + constexpr RgbaColor() noexcept + : r{ 255 } + , g{ 255 } + , b{ 255 } + , a{ 255 } + { + } + + constexpr RgbaColor(float r, float g, float b, float a = 1.0f) noexcept + : r{ static_cast<uint8_t>(r * 255.0f) } + , g{ static_cast<uint8_t>(g * 255.0f) } + , b{ static_cast<uint8_t>(b * 255.0f) } + , a{ static_cast<uint8_t>(a * 255.0f) } + { + } + + constexpr RgbaColor(int r, int g, int b, int a = 255) noexcept + : r{ static_cast<uint8_t>(r & 0xFF) } + , g{ static_cast<uint8_t>(g & 0xFF) } + , b{ static_cast<uint8_t>(b & 0xFF) } + , a{ static_cast<uint8_t>(a & 0xFF) } + { + } + + constexpr RgbaColor(uint32_t rgba) noexcept + : r{ static_cast<uint8_t>((rgba >> 0) & 0xFF) } + , g{ static_cast<uint8_t>((rgba >> 8) & 0xFF) } + , b{ static_cast<uint8_t>((rgba >> 16) & 0xFF) } + , a{ static_cast<uint8_t>((rgba >> 24) & 0xFF) } + { + } + + constexpr uint32_t GetScalar() const noexcept + { + uint32_t res = 0; + res |= r << 24; + res |= g << 16; + res |= b << 8; + res |= a; + return res; + } + + constexpr void SetScalar(uint32_t scalar) noexcept + { + r = (scalar >> 0) & 0xFF; + g = (scalar >> 8) & 0xFF; + b = (scalar >> 16) & 0xFF; + a = (scalar >> 24) & 0xFF; + } + + constexpr float GetNormalizedRed() const noexcept + { + return r / 255.0f; + } + + constexpr float GetNormalizedGreen() const noexcept + { + return g / 255.0f; + } + + constexpr float GetNormalizedBlue() const noexcept + { + return b / 255.0f; + } + + constexpr float GetNormalizedAlpha() const noexcept + { + return a / 255.0f; + } + + constexpr Vec4i AsVec4i() const noexcept + { + return Vec4i{ r, g, b, a }; + } + + constexpr Vec4f AsVec4f() const noexcept + { + return Vec4f{ + GetNormalizedRed(), + GetNormalizedGreen(), + GetNormalizedBlue(), + GetNormalizedAlpha(), + }; + } + + ImVec4 AsImVec() const + { + auto v = AsVec4f(); + return ImVec4{ v.x, v.y, v.z, v.w }; + } + + ImColor AsImColor() const + { + auto v = AsVec4f(); + return ImColor{ v.x, v.y, v.z, v.w }; + } + + ImU32 AsImU32() const + { + ImU32 res; + res |= r << IM_COL32_R_SHIFT; + res |= g << IM_COL32_G_SHIFT; + res |= b << IM_COL32_B_SHIFT; + res |= a << IM_COL32_A_SHIFT; + return res; + } + + constexpr void SetVec(const Vec4f& vec) noexcept + { + r = (uint8_t)(vec.x * 255.0f); + g = (uint8_t)(vec.y * 255.0f); + b = (uint8_t)(vec.z * 255.0f); + a = (uint8_t)(vec.w * 255.0f); + } + + // Forward declaring because cyclic reference between RgbaColor and HsvColor + constexpr HsvColor ToHsv() const noexcept; + + friend constexpr bool operator==(const RgbaColor&, const RgbaColor&) noexcept = default; +}; + +class HsvColor +{ +public: + float h; + float s; + float v; + float a; + +public: + constexpr HsvColor() noexcept + : h{ 0.0f } + , s{ 0.0f } + , v{ 1.0f } + , a{ 1.0f } + { + } + + constexpr HsvColor(float h, float s, float v, float a) noexcept + : h{ h } + , s{ s } + , v{ v } + , a{ a } + { + } + + // Forward declaring because cyclic reference between RgbaColor and HsvColor + constexpr RgbaColor ToRgba() const noexcept; +}; + +constexpr HsvColor RgbaColor::ToHsv() const noexcept +{ + float fr = GetNormalizedRed(); + float fg = GetNormalizedBlue(); + float fb = GetNormalizedGreen(); + float fa = GetNormalizedAlpha(); + + auto p = fg < fb ? Vec4f{ fb, fg, -1, 2.0f / 3.0f } : Vec4f{ fg, fb, 0, -1.0f / 3.0f }; + auto q = fr < p.x ? Vec4f{ p.x, p.y, p.w, fr } : Vec4f{ fr, p.y, p.z, p.x }; + float c = q.x - std::min(q.w, q.y); + float h = MathUtils::Abs((q.w - q.y) / (6 * c + std::numeric_limits<float>::epsilon()) + q.z); + + Vec3f hcv{ h, c, q.x }; + float s = hcv.y / (hcv.z + std::numeric_limits<float>::epsilon()); + return HsvColor(hcv.x, s, hcv.z, fa); +} + +constexpr RgbaColor HsvColor::ToRgba() const noexcept +{ + float r = MathUtils::Abs(h * 6 - 3) - 1; + float g = 2 - MathUtils::Abs(h * 6 - 2); + float b = 2 - MathUtils::Abs(h * 6 - 4); + + auto rgb = Vec3f{ + std::clamp(r, 0.0f, 1.0f), + std::clamp(g, 0.0f, 1.0f), + std::clamp(b, 0.0f, 1.0f), + }; + auto vc = (rgb - Vec3f{ 0, 0, 0 }) * s + Vec3f{ 1, 1, 1 } * v; + + return RgbaColor(vc.x, vc.y, vc.z, a); +} |