Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 73 additions & 27 deletions include/omath/utility/color.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,28 @@ namespace omath
float value{};
};

class Color final : public Vector4<float>
class Color final
{
Vector4<float> m_value;
public:
constexpr Color(const float r, const float g, const float b, const float a) noexcept: Vector4(r, g, b, a)
constexpr const Vector4<float>& value() const
{
clamp(0.f, 1.f);
return m_value;
}
constexpr Color(const float r, const float g, const float b, const float a) noexcept: m_value(r, g, b, a)
{
m_value.clamp(0.f, 1.f);
}

constexpr explicit Color(const Vector4<float>& value) : m_value(value)
{
m_value.clamp(0.f, 1.f);
}
constexpr explicit Color() noexcept = default;
[[nodiscard]]
constexpr static Color from_rgba(const uint8_t r, const uint8_t g, const uint8_t b, const uint8_t a) noexcept
{
return Color{Vector4(r, g, b, a) / 255.f};
return Color(Vector4<float>(r, g, b, a) / 255.f);
}

[[nodiscard]]
Expand Down Expand Up @@ -82,9 +91,9 @@ namespace omath
{
Hsv hsv_data;

const float& red = x;
const float& green = y;
const float& blue = z;
const float& red = m_value.x;
const float& green = m_value.y;
const float& blue = m_value.z;

const float max = std::max({red, green, blue});
const float min = std::min({red, green, blue});
Expand All @@ -109,11 +118,6 @@ namespace omath

return hsv_data;
}

constexpr explicit Color(const Vector4& vec) noexcept: Vector4(vec)
{
clamp(0.f, 1.f);
}
constexpr void set_hue(const float hue) noexcept
{
auto hsv = to_hsv();
Expand Down Expand Up @@ -141,7 +145,7 @@ namespace omath
constexpr Color blend(const Color& other, float ratio) const noexcept
{
ratio = std::clamp(ratio, 0.f, 1.f);
return Color(*this * (1.f - ratio) + other * ratio);
return Color(this->m_value * (1.f - ratio) + other.m_value * ratio);
}

[[nodiscard]] static constexpr Color red()
Expand All @@ -160,16 +164,26 @@ namespace omath
[[nodiscard]]
ImColor to_im_color() const noexcept
{
return {to_im_vec4()};
return {m_value.to_im_vec4()};
}
#endif
[[nodiscard]] std::string to_string() const noexcept
{
return std::format("[r:{}, g:{}, b:{}, a:{}]",
static_cast<int>(x * 255.f),
static_cast<int>(y * 255.f),
static_cast<int>(z * 255.f),
static_cast<int>(w * 255.f));
static_cast<int>(m_value.x * 255.f),
static_cast<int>(m_value.y * 255.f),
static_cast<int>(m_value.z * 255.f),
static_cast<int>(m_value.w * 255.f));
}
[[nodiscard]] std::string to_rgbf_string() const noexcept
{
return std::format("[r:{}, g:{}, b:{}, a:{}]",
m_value.x, m_value.y, m_value.z, m_value.w);
}
[[nodiscard]] std::string to_hsv_string() const noexcept
{
const auto [hue, saturation, value] = to_hsv();
return std::format("[h:{}, s:{}, v:{}]", hue, saturation, value);
}
[[nodiscard]] std::wstring to_wstring() const noexcept
{
Expand All @@ -188,23 +202,55 @@ namespace omath
template<>
struct std::formatter<omath::Color> // NOLINT(*-dcl58-cpp)
{
[[nodiscard]]
static constexpr auto parse(const std::format_parse_context& ctx)
enum class ColorFormat { rgb, rgbf, hsv };
ColorFormat color_format = ColorFormat::rgb;

constexpr auto parse(std::format_parse_context& ctx)
{
return ctx.begin();
const auto it = ctx.begin();
const auto end = ctx.end();

if (it == end || *it == '}')
return it;

const std::string_view spec(it, end);

if (spec.starts_with("rgbf"))
{
color_format = ColorFormat::rgbf;
return it + 4;
}
if (spec.starts_with("rgb"))
{
color_format = ColorFormat::rgb;
return it + 3;
}
if (spec.starts_with("hsv"))
{
color_format = ColorFormat::hsv;
return it + 3;
}

throw std::format_error("Invalid format specifier for omath::Color. Use rgb, rgbf, or hsv.");
}

template<class FormatContext>
[[nodiscard]]
static auto format(const omath::Color& col, FormatContext& ctx)
auto format(const omath::Color& col, FormatContext& ctx) const
{
std::string str;
switch (color_format)
{
case ColorFormat::rgb: str = col.to_string(); break;
case ColorFormat::rgbf: str = col.to_rgbf_string(); break;
case ColorFormat::hsv: str = col.to_hsv_string(); break;
}

if constexpr (std::is_same_v<typename FormatContext::char_type, char>)
return std::format_to(ctx.out(), "{}", col.to_string());
return std::format_to(ctx.out(), "{}", str);
if constexpr (std::is_same_v<typename FormatContext::char_type, wchar_t>)
return std::format_to(ctx.out(), L"{}", col.to_wstring());

return std::format_to(ctx.out(), L"{}", std::wstring(str.cbegin(), str.cend()));
if constexpr (std::is_same_v<typename FormatContext::char_type, char8_t>)
return std::format_to(ctx.out(), u8"{}", col.to_u8string());
return std::format_to(ctx.out(), u8"{}", std::u8string(str.cbegin(), str.cend()));

std::unreachable();
}
Expand Down
Loading
Loading