From cfc80471a27f820027a8d0872c61898ff3bb8dab Mon Sep 17 00:00:00 2001 From: Bruno Nicoletti Date: Sun, 17 Mar 2024 13:39:35 +0000 Subject: [PATCH 1/3] Added simple arithmatic operators to artist::colour and a test --- lib/include/artist/color.hpp | 25 +++++++++++++++++++++++++ test/main.cpp | 19 ++++++++++++++++++- 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/lib/include/artist/color.hpp b/lib/include/artist/color.hpp index 3f3fa4aa..f9fbf56b 100755 --- a/lib/include/artist/color.hpp +++ b/lib/include/artist/color.hpp @@ -34,6 +34,11 @@ namespace cycfi::artist constexpr bool operator==(color const& a, color const& b); constexpr bool operator!=(color const& a, color const& b); + constexpr color operator+(color const& a, color const& b); + constexpr color operator-(color const& a, color const& b); + constexpr color operator*(color const& a, float b); + constexpr color operator*(float a, color const& b); + //////////////////////////////////////////////////////////////////////////// // Inlines //////////////////////////////////////////////////////////////////////////// @@ -176,6 +181,26 @@ namespace cycfi::artist return r; } + constexpr color operator+(color const& a, color const& b) + { + return color(a.red + b.red, a.green + b.green, a.blue + b.blue, a.alpha + b.alpha*(1.0-a.alpha)); + } + + constexpr color operator-(color const& a, color const& b) + { + return color(a.red - b.red, a.green - b.green, a.blue - b.blue, a.alpha + b.alpha*(1.0-a.alpha)); + } + + constexpr color operator*(color const& a, float b) + { + return color(a.red * b, a.green * b, a.blue * b, a.alpha); + } + + constexpr color operator*(float a, color const& b) + { + return color(a * b.red, a * b.green, a * b.blue, b.alpha); + } + //////////////////////////////////////////////////////////////////////////// // Common colors //////////////////////////////////////////////////////////////////////////// diff --git a/test/main.cpp b/test/main.cpp index ca1870de..8db62175 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -986,4 +986,21 @@ TEST_CASE("Misc") } - +TEST_CASE("Color Maths") +{ + color a(0.2, 0.25, 0.5, 1.0); + color b(0.5, 0.6, 0.1, 1.0); + + auto check = [&](color result, color check) + { + REQUIRE_THAT(result.red, Catch::WithinRel(check.red, 0.001f)); + REQUIRE_THAT(result.green, Catch::WithinRel(check.green, 0.001f)); + REQUIRE_THAT(result.blue, Catch::WithinRel(check.blue, 0.001f)); + REQUIRE_THAT(result.alpha, Catch::WithinRel(check.alpha, 0.001f)); + }; + + check(a + b, color(0.7, 0.85, 0.6, 1.0)); + check(a - b, color(-0.3, -0.35, 0.4, 1.0)); + check(a * 2, color(0.4, 0.5, 1.0, 1.0)); + check(2 * a, color(0.4, 0.5, 1.0, 1.0)); +} From 2e74705762b668d417eaf04c60b5fb8555f4f09d Mon Sep 17 00:00:00 2001 From: bjn Date: Fri, 28 Jun 2024 21:00:54 +0100 Subject: [PATCH 2/3] Copied axis point and rect methods over from elements:master branch --- lib/include/artist/point.hpp | 23 +++++++++++++++++++++++ lib/include/artist/rect.hpp | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/lib/include/artist/point.hpp b/lib/include/artist/point.hpp index 06f295f7..9154ad6b 100755 --- a/lib/include/artist/point.hpp +++ b/lib/include/artist/point.hpp @@ -8,6 +8,16 @@ namespace cycfi::artist { + //////////////////////////////////////////////////////////////////////////// + // Axis + //////////////////////////////////////////////////////////////////////////// + enum class axis : bool { x = false, y = true}; + + inline constexpr axis other(axis a) + { + return axis(not bool(a)); + } + //////////////////////////////////////////////////////////////////////////// // Points //////////////////////////////////////////////////////////////////////////// @@ -25,6 +35,9 @@ namespace cycfi::artist constexpr point move_to(float x, float y) const; constexpr point reflect(point p) const; + constexpr float& operator[](axis a); + constexpr float operator[](axis a) const; + float x; float y; }; @@ -86,6 +99,16 @@ namespace cycfi::artist { return {x + (x - p.x), y + (y - p.y)}; } + + constexpr float& point::operator[](axis a) + { + return a==axis::x ? x : y; + } + + constexpr float point::operator[](axis a) const + { + return a==axis::x ? x : y; + } } #endif diff --git a/lib/include/artist/rect.hpp b/lib/include/artist/rect.hpp index f8107102..8df5433c 100755 --- a/lib/include/artist/rect.hpp +++ b/lib/include/artist/rect.hpp @@ -242,6 +242,41 @@ namespace cycfi::artist r.right = 0.0; r.bottom = 0.0; } + + inline constexpr float axis_extent(rect const &r, axis a) + { + return a == axis::x ? r.width() : r.height(); + } + + inline constexpr float axis_min(rect const &r, axis a) + { + return a == axis::x ? r.left : r.top; + } + + inline constexpr float axis_max(rect const &r, axis a) + { + return a == axis::x ? r.right : r.bottom; + } + + inline constexpr float& axis_min(rect &r, axis a) + { + return a == axis::x ? r.left : r.top; + } + + inline constexpr float& axis_max(rect &r, axis a) + { + return a == axis::x ? r.right : r.bottom; + } + + inline constexpr auto make_rect(axis a, float this_axis_min, float other_axis_min, float this_axis_max, float other_axis_max) -> rect + { + if (a == axis::x) { + return rect(this_axis_min, other_axis_min, this_axis_max, other_axis_max); + } + else { + return rect(other_axis_min, this_axis_min, other_axis_max, this_axis_max); + } + } } #endif From 305f726697b1becac920f27ae2dab7fe86285fee Mon Sep 17 00:00:00 2001 From: bjn Date: Fri, 28 Jun 2024 21:05:37 +0100 Subject: [PATCH 3/3] Fixed issue with MSVC not understanding the 'not' keyword --- lib/include/artist/point.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/include/artist/point.hpp b/lib/include/artist/point.hpp index 9154ad6b..49cd138f 100755 --- a/lib/include/artist/point.hpp +++ b/lib/include/artist/point.hpp @@ -15,7 +15,7 @@ namespace cycfi::artist inline constexpr axis other(axis a) { - return axis(not bool(a)); + return axis(! bool(a)); } ////////////////////////////////////////////////////////////////////////////