From 20ae2b4dd18716c20c1c66f0a6375fe0eae7c4a4 Mon Sep 17 00:00:00 2001 From: Orange Date: Tue, 26 Aug 2025 12:35:40 +0300 Subject: [PATCH] Adds formatters for Angle and Color Adds formatters for `omath::Angle` and `omath::Color` to allow for easy formatting using `std::format`. This allows users to easily output Angle and Color values in a human-readable format. --- include/omath/angle.hpp | 15 +++++++++++++++ include/omath/color.hpp | 18 ++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/include/omath/angle.hpp b/include/omath/angle.hpp index e7d0879..a13b27f 100644 --- a/include/omath/angle.hpp +++ b/include/omath/angle.hpp @@ -6,6 +6,7 @@ #include "omath/angles.hpp" #include #include +#include namespace omath { @@ -149,3 +150,17 @@ namespace omath } }; } // namespace omath +template +struct std::formatter> // NOLINT(*-dcl58-cpp) +{ + [[nodiscard]] + static constexpr auto parse(std::format_parse_context& ctx) + { + return ctx.begin(); + } + [[nodiscard]] + static auto format(const omath::Angle& deg, std::format_context& ctx) + { + return std::format_to(ctx.out(), "{}deg", deg.as_degrees()); + } +}; \ No newline at end of file diff --git a/include/omath/color.hpp b/include/omath/color.hpp index f620807..ba28d08 100644 --- a/include/omath/color.hpp +++ b/include/omath/color.hpp @@ -167,3 +167,21 @@ namespace omath #endif }; } // namespace omath +template<> +struct std::formatter // NOLINT(*-dcl58-cpp) +{ + [[nodiscard]] + static constexpr auto parse(std::format_parse_context& ctx) + { + return ctx.begin(); + } + [[nodiscard]] + static auto format(const omath::Color& col, std::format_context& ctx) + { + 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)); + } +}; \ No newline at end of file