From d7e6a3ffc8453720cff2ceebb12346b0d54cf277 Mon Sep 17 00:00:00 2001 From: Matthew Date: Thu, 31 Jul 2025 12:49:35 +1000 Subject: [PATCH 1/5] Added tests for the hsb_color function from color.cpp to unit_test_color.cpp --- .../src/test/unit_tests/unit_test_color.cpp | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/coresdk/src/test/unit_tests/unit_test_color.cpp b/coresdk/src/test/unit_tests/unit_test_color.cpp index 41a849e1..777f84c0 100644 --- a/coresdk/src/test/unit_tests/unit_test_color.cpp +++ b/coresdk/src/test/unit_tests/unit_test_color.cpp @@ -69,3 +69,70 @@ TEST_CASE("can convert a color to a hex string", "[color_to_string][color_green] REQUIRE(alpha_of(result) == 200); } } + +TEST_CASE("hsb_color converts HSB to RGB correctly", "[hsb_color]") +{ + SECTION("black when brightness is 0") + { + color black = hsb_color(0.5, 1.0, 0.0); + REQUIRE(red_of(black) == 0); + REQUIRE(green_of(black) == 0); + REQUIRE(blue_of(black) == 0); + } + + SECTION("gray when saturation is 0") + { + color gray = hsb_color(0.3, 0.0, 0.5); + REQUIRE(red_of(gray) == 127); + REQUIRE(green_of(gray) == 127); + REQUIRE(blue_of(gray) == 127); + } + + SECTION("pure red from HSB") + { + color red = hsb_color(0.0, 1.0, 1.0); + REQUIRE(red_of(red) == 255); + REQUIRE(green_of(red) == 0); + REQUIRE(blue_of(red) == 0); + } + + SECTION("pure green from HSB") + { + color green = hsb_color(1.0 / 3.0, 1.0, 1.0); + REQUIRE(red_of(green) == 0); + REQUIRE(green_of(green) == 255); + REQUIRE(blue_of(green) == 0); + } + + SECTION("pure blue from HSB") + { + color blue = hsb_color(2.0 / 3.0, 1.0, 1.0); + REQUIRE(red_of(blue) == 0); + REQUIRE(green_of(blue) == 0); + REQUIRE(blue_of(blue) == 255); + } + + SECTION("pure yellow from HSB") + { + color yellow = hsb_color(1.0 / 6.0, 1.0, 1.0); + REQUIRE(red_of(yellow) == 255); + REQUIRE(green_of(yellow) == 255); + REQUIRE(blue_of(yellow) == 0); + } + + SECTION("pure cyan from HSB") + { + color cyan = hsb_color(0.5, 1.0, 1.0); + REQUIRE(red_of(cyan) == 0); + REQUIRE(green_of(cyan) == 255); + REQUIRE(blue_of(cyan) == 255); + } + + SECTION("pure magenta from HSB") + { + color magenta = hsb_color(5.0 / 6.0, 1.0, 1.0); + REQUIRE(red_of(magenta) == 255); + REQUIRE(green_of(magenta) == 0); + REQUIRE(blue_of(magenta) == 255); + } +} From bc3f37f6fdb61542e7304e10838ee82e4b217a44 Mon Sep 17 00:00:00 2001 From: Matthew Date: Thu, 7 Aug 2025 19:21:57 +1000 Subject: [PATCH 2/5] Modified the hsb_color function in color.cpp and modified the unit test file to include tests outside the valid range --- coresdk/src/coresdk/color.cpp | 27 ++++++++++++++++--- .../src/test/unit_tests/unit_test_color.cpp | 21 +++++++++++++++ 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/coresdk/src/coresdk/color.cpp b/coresdk/src/coresdk/color.cpp index 9cb79de1..a3a3255f 100644 --- a/coresdk/src/coresdk/color.cpp +++ b/coresdk/src/coresdk/color.cpp @@ -68,16 +68,37 @@ namespace splashkit_lib return rgba_color(red, green, blue, 1.0f); } - /// _returs a color from a combination of hue, saturation, and brightness. + /// _returns a color from a combination of hue, saturation, and brightness. /// - /// @param hue, saturation, brightness: _values between 0 and 1 - /// @returns _the matching color + /// @param hue, saturation, brightness: _values between 0.0 and 1.0 + /// @returns _the matching RGB color + /// + /// @note: Values outside the range of 0.0 to 1.0 will be clamped to that range. + /// If clamping occurs, rounds the number to the nearest valid value (either 0.0 or 1.0), + /// and the color may not be as expected and will be logged. /// color hsb_color(double hue, double saturation, double brightness) { double domain_offset; double red, green, blue; + double original_hue = hue; + double original_saturation = saturation; + double original_brightness = brightness; + + if (hue < 0.0) hue = 0.0; + if (hue > 1.0) hue = 1.0; + + if (saturation < 0.0) saturation = 0.0; + if (saturation > 1.0) saturation = 1.0; + + if (brightness < 0.0) brightness = 0.0; + if (brightness > 1.0) brightness = 1.0; + + if (hue != original_hue || saturation != original_saturation || brightness != original_brightness) { + LOG(WARNING) << "Error in hsb_color received out-of-bounds input. Values have been clamped."; + } + if (brightness == 0) return COLOR_BLACK; diff --git a/coresdk/src/test/unit_tests/unit_test_color.cpp b/coresdk/src/test/unit_tests/unit_test_color.cpp index 777f84c0..a9f0120e 100644 --- a/coresdk/src/test/unit_tests/unit_test_color.cpp +++ b/coresdk/src/test/unit_tests/unit_test_color.cpp @@ -78,6 +78,7 @@ TEST_CASE("hsb_color converts HSB to RGB correctly", "[hsb_color]") REQUIRE(red_of(black) == 0); REQUIRE(green_of(black) == 0); REQUIRE(blue_of(black) == 0); + REQUIRE(alpha_of(black) == 255); } SECTION("gray when saturation is 0") @@ -86,6 +87,7 @@ TEST_CASE("hsb_color converts HSB to RGB correctly", "[hsb_color]") REQUIRE(red_of(gray) == 127); REQUIRE(green_of(gray) == 127); REQUIRE(blue_of(gray) == 127); + REQUIRE(alpha_of(gray) == 255); } SECTION("pure red from HSB") @@ -135,4 +137,23 @@ TEST_CASE("hsb_color converts HSB to RGB correctly", "[hsb_color]") REQUIRE(green_of(magenta) == 0); REQUIRE(blue_of(magenta) == 255); } + + SECTION("Out-of-bounds values still return valid colors") + { + color low_hue = hsb_color(-0.5, 1.0, 1.0); + color high_hue = hsb_color(1.5, 1.0, 1.0); + color low_saturation = hsb_color(0.5, -0.5, 1.0); + color high_saturation = hsb_color(0.5, 1.5, 1.0); + color low_brightness = hsb_color(0.5, 1.0, -0.5); + color high_brightness = hsb_color(0.5, 1.0, 1.5); + + REQUIRE(red_of(low_hue) >= 0); + REQUIRE(red_of(high_hue) <= 255); + + REQUIRE(green_of(low_saturation) >= 0); + REQUIRE(green_of(high_saturation) <= 255); + + REQUIRE(blue_of(low_brightness) >= 0); + REQUIRE(blue_of(high_brightness) <= 255); + } } From 796a7022454d5dc935cbdd42850a319efeb8e494 Mon Sep 17 00:00:00 2001 From: Broccoli811 Date: Fri, 15 Aug 2025 19:34:44 +1000 Subject: [PATCH 3/5] modified the documentation to clarify the use of std::clamp in color.cpp from the algorithm library --- coresdk/src/coresdk/color.cpp | 24 ++++++++----------- .../cmake/Testing/Temporary/CTestCostData.txt | 1 + projects/cmake/Testing/Temporary/LastTest.log | 3 +++ 3 files changed, 14 insertions(+), 14 deletions(-) create mode 100644 projects/cmake/Testing/Temporary/CTestCostData.txt create mode 100644 projects/cmake/Testing/Temporary/LastTest.log diff --git a/coresdk/src/coresdk/color.cpp b/coresdk/src/coresdk/color.cpp index a3a3255f..51b6f20d 100644 --- a/coresdk/src/coresdk/color.cpp +++ b/coresdk/src/coresdk/color.cpp @@ -12,6 +12,7 @@ #include #include #include +#include #include "utility_functions.h" @@ -38,7 +39,7 @@ namespace splashkit_lib return rgba_color(red / 255.0f, green / 255.0f, blue / 255.0f, alpha / 255.0f); } - /// _gets a color given its color components. Each of the components has + /// gets a color given its color components. Each of the components has /// a value between 0 and 1 /// color rgba_color(double red, double green, double blue, double alpha) @@ -52,7 +53,7 @@ namespace splashkit_lib return result; } - /// _gets a color given its _r_g_b components. Each of the components has + /// gets a color given its _r_g_b components. Each of the components has /// a value between 0 and 1.0f. /// color rgb_color(int red, int green, int blue) @@ -60,7 +61,7 @@ namespace splashkit_lib return rgba_color(red / 255.0f, green / 255.0f, blue / 255.0f, 1.0f); } - /// _gets a color given its _r_g_b components. Each of the components has + /// gets a color given its _r_g_b components. Each of the components has /// a value between 0 and 1. /// color rgb_color(double red, double green, double blue) @@ -68,10 +69,10 @@ namespace splashkit_lib return rgba_color(red, green, blue, 1.0f); } - /// _returns a color from a combination of hue, saturation, and brightness. + /// returns a color from a combination of hue, saturation, and brightness. /// - /// @param hue, saturation, brightness: _values between 0.0 and 1.0 - /// @returns _the matching RGB color + /// @param hue, saturation, brightness: values between 0.0 and 1.0 + /// @returns the matching RGB color /// /// @note: Values outside the range of 0.0 to 1.0 will be clamped to that range. /// If clamping occurs, rounds the number to the nearest valid value (either 0.0 or 1.0), @@ -86,14 +87,9 @@ namespace splashkit_lib double original_saturation = saturation; double original_brightness = brightness; - if (hue < 0.0) hue = 0.0; - if (hue > 1.0) hue = 1.0; - - if (saturation < 0.0) saturation = 0.0; - if (saturation > 1.0) saturation = 1.0; - - if (brightness < 0.0) brightness = 0.0; - if (brightness > 1.0) brightness = 1.0; + hue = std::clamp(hue, 0.0, 1.0); + saturation = std::clamp(saturation, 0.0, 1.0); + brightness = std::clamp(brightness, 0.0, 1.0); if (hue != original_hue || saturation != original_saturation || brightness != original_brightness) { LOG(WARNING) << "Error in hsb_color received out-of-bounds input. Values have been clamped."; diff --git a/projects/cmake/Testing/Temporary/CTestCostData.txt b/projects/cmake/Testing/Temporary/CTestCostData.txt new file mode 100644 index 00000000..ed97d539 --- /dev/null +++ b/projects/cmake/Testing/Temporary/CTestCostData.txt @@ -0,0 +1 @@ +--- diff --git a/projects/cmake/Testing/Temporary/LastTest.log b/projects/cmake/Testing/Temporary/LastTest.log new file mode 100644 index 00000000..2eb211ee --- /dev/null +++ b/projects/cmake/Testing/Temporary/LastTest.log @@ -0,0 +1,3 @@ +Start testing: Aug 15 19:32 AUS Eastern Standard Time +---------------------------------------------------------- +End testing: Aug 15 19:32 AUS Eastern Standard Time From e1dbef6b7d1cb3c83c00e1b5a264fb0507e6b6f5 Mon Sep 17 00:00:00 2001 From: Broccoli811 Date: Tue, 19 Aug 2025 12:54:26 +1000 Subject: [PATCH 4/5] Remove temporary test data files: CTestCostData.txt and LastTest.log --- projects/cmake/Testing/Temporary/CTestCostData.txt | 1 - projects/cmake/Testing/Temporary/LastTest.log | 3 --- 2 files changed, 4 deletions(-) delete mode 100644 projects/cmake/Testing/Temporary/CTestCostData.txt delete mode 100644 projects/cmake/Testing/Temporary/LastTest.log diff --git a/projects/cmake/Testing/Temporary/CTestCostData.txt b/projects/cmake/Testing/Temporary/CTestCostData.txt deleted file mode 100644 index ed97d539..00000000 --- a/projects/cmake/Testing/Temporary/CTestCostData.txt +++ /dev/null @@ -1 +0,0 @@ ---- diff --git a/projects/cmake/Testing/Temporary/LastTest.log b/projects/cmake/Testing/Temporary/LastTest.log deleted file mode 100644 index 2eb211ee..00000000 --- a/projects/cmake/Testing/Temporary/LastTest.log +++ /dev/null @@ -1,3 +0,0 @@ -Start testing: Aug 15 19:32 AUS Eastern Standard Time ----------------------------------------------------------- -End testing: Aug 15 19:32 AUS Eastern Standard Time From 8d4ae5f4347a35cd79e00123a984b014599f6d8d Mon Sep 17 00:00:00 2001 From: Broccoli811 Date: Sun, 24 Aug 2025 18:02:00 +1000 Subject: [PATCH 5/5] Removed unnecessary comments and changed the log output message to be more consistent; add alpha checks in hsb_color unit tests --- coresdk/src/coresdk/color.cpp | 20 +------------------ .../src/test/unit_tests/unit_test_color.cpp | 6 ++++++ 2 files changed, 7 insertions(+), 19 deletions(-) diff --git a/coresdk/src/coresdk/color.cpp b/coresdk/src/coresdk/color.cpp index 51b6f20d..c3dd9348 100644 --- a/coresdk/src/coresdk/color.cpp +++ b/coresdk/src/coresdk/color.cpp @@ -39,9 +39,6 @@ namespace splashkit_lib return rgba_color(red / 255.0f, green / 255.0f, blue / 255.0f, alpha / 255.0f); } - /// gets a color given its color components. Each of the components has - /// a value between 0 and 1 - /// color rgba_color(double red, double green, double blue, double alpha) { color result; @@ -53,31 +50,16 @@ namespace splashkit_lib return result; } - /// gets a color given its _r_g_b components. Each of the components has - /// a value between 0 and 1.0f. - /// color rgb_color(int red, int green, int blue) { return rgba_color(red / 255.0f, green / 255.0f, blue / 255.0f, 1.0f); } - /// gets a color given its _r_g_b components. Each of the components has - /// a value between 0 and 1. - /// color rgb_color(double red, double green, double blue) { return rgba_color(red, green, blue, 1.0f); } - /// returns a color from a combination of hue, saturation, and brightness. - /// - /// @param hue, saturation, brightness: values between 0.0 and 1.0 - /// @returns the matching RGB color - /// - /// @note: Values outside the range of 0.0 to 1.0 will be clamped to that range. - /// If clamping occurs, rounds the number to the nearest valid value (either 0.0 or 1.0), - /// and the color may not be as expected and will be logged. - /// color hsb_color(double hue, double saturation, double brightness) { double domain_offset; @@ -92,7 +74,7 @@ namespace splashkit_lib brightness = std::clamp(brightness, 0.0, 1.0); if (hue != original_hue || saturation != original_saturation || brightness != original_brightness) { - LOG(WARNING) << "Error in hsb_color received out-of-bounds input. Values have been clamped."; + LOG(WARNING) << "Attempting to create a color from out-of-bounds HSB components. Values should be between 0 and 1."; } if (brightness == 0) diff --git a/coresdk/src/test/unit_tests/unit_test_color.cpp b/coresdk/src/test/unit_tests/unit_test_color.cpp index a9f0120e..61b44283 100644 --- a/coresdk/src/test/unit_tests/unit_test_color.cpp +++ b/coresdk/src/test/unit_tests/unit_test_color.cpp @@ -96,6 +96,7 @@ TEST_CASE("hsb_color converts HSB to RGB correctly", "[hsb_color]") REQUIRE(red_of(red) == 255); REQUIRE(green_of(red) == 0); REQUIRE(blue_of(red) == 0); + REQUIRE(alpha_of(red) == 255); } SECTION("pure green from HSB") @@ -104,6 +105,7 @@ TEST_CASE("hsb_color converts HSB to RGB correctly", "[hsb_color]") REQUIRE(red_of(green) == 0); REQUIRE(green_of(green) == 255); REQUIRE(blue_of(green) == 0); + REQUIRE(alpha_of(green) == 255); } SECTION("pure blue from HSB") @@ -112,6 +114,7 @@ TEST_CASE("hsb_color converts HSB to RGB correctly", "[hsb_color]") REQUIRE(red_of(blue) == 0); REQUIRE(green_of(blue) == 0); REQUIRE(blue_of(blue) == 255); + REQUIRE(alpha_of(blue) == 255); } SECTION("pure yellow from HSB") @@ -120,6 +123,7 @@ TEST_CASE("hsb_color converts HSB to RGB correctly", "[hsb_color]") REQUIRE(red_of(yellow) == 255); REQUIRE(green_of(yellow) == 255); REQUIRE(blue_of(yellow) == 0); + REQUIRE(alpha_of(yellow) == 255); } SECTION("pure cyan from HSB") @@ -128,6 +132,7 @@ TEST_CASE("hsb_color converts HSB to RGB correctly", "[hsb_color]") REQUIRE(red_of(cyan) == 0); REQUIRE(green_of(cyan) == 255); REQUIRE(blue_of(cyan) == 255); + REQUIRE(alpha_of(cyan) == 255); } SECTION("pure magenta from HSB") @@ -136,6 +141,7 @@ TEST_CASE("hsb_color converts HSB to RGB correctly", "[hsb_color]") REQUIRE(red_of(magenta) == 255); REQUIRE(green_of(magenta) == 0); REQUIRE(blue_of(magenta) == 255); + REQUIRE(alpha_of(magenta) == 255); } SECTION("Out-of-bounds values still return valid colors")