diff --git a/include/omath/color.hpp b/include/omath/color.hpp index 7de2efe..7d72036 100644 --- a/include/omath/color.hpp +++ b/include/omath/color.hpp @@ -164,6 +164,26 @@ namespace omath return {to_im_vec4()}; } #endif + [[nodiscard]] std::string to_string() const noexcept + { + return std::format("[r:{}, g:{}, b:{}, a:{}]", + static_cast(x * 255.f), + static_cast(y * 255.f), + static_cast(z * 255.f), + static_cast(w * 255.f)); + } + [[nodiscard]] std::wstring to_wstring() const noexcept + { + const auto ascii_string = to_string(); + return {ascii_string.cbegin(), ascii_string.cend()}; + } + + // ReSharper disable once CppInconsistentNaming + [[nodiscard]] std::u8string to_u8string() const noexcept + { + const auto ascii_string = to_string(); + return {ascii_string.cbegin(), ascii_string.cend()}; + } }; } // namespace omath template<> @@ -179,25 +199,14 @@ struct std::formatter // NOLINT(*-dcl58-cpp) [[nodiscard]] static auto format(const omath::Color& col, FormatContext& ctx) { - if constexpr (std::is_same_v) - return std::format_to(ctx.out(), "[r:{}, g:{}, b:{}, a:{}]", - static_cast(col.x * 255.f), - static_cast(col.y * 255.f), - static_cast(col.z * 255.f), - static_cast(col.w * 255.f)); - if constexpr (std::is_same_v) - return std::format_to(ctx.out(), L"[r:{}, g:{}, b:{}, a:{}]", - static_cast(col.x * 255.f), - static_cast(col.y * 255.f), - static_cast(col.z * 255.f), - static_cast(col.w * 255.f)); + if constexpr (std::is_same_v) + return std::format_to(ctx.out(), "{}", col.to_string()); + if constexpr (std::is_same_v) + return std::format_to(ctx.out(), L"{}", col.to_wstring()); - if constexpr (std::is_same_v) - return std::format_to(ctx.out(), u8"[r:{}, g:{}, b:{}, a:{}]", - static_cast(col.x * 255.f), - static_cast(col.y * 255.f), - static_cast(col.z * 255.f), - static_cast(col.w * 255.f)); - std::unreachable(); + if constexpr (std::is_same_v) + return std::format_to(ctx.out(), u8"{}", col.to_u8string()); + + return std::unreachable(); } }; \ No newline at end of file