-
Notifications
You must be signed in to change notification settings - Fork 40
Add Unit tests for the hsb_color function #120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
| } | ||
| } | ||
|
|
||
| TEST_CASE("hsb_color converts HSB to RGB correctly", "[hsb_color]") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The docs specify that the input values of hsb_color must be between 0 and 1, so let's also check what happens with values outside of those bounds.
Unfortunately the docs don't specify what the behaviour should be, but from the code it looks like it will return the absolute value or the max for each component.
There's an opportunity here to also update the documentation with the expected behaviour. You could also go a step further and add handling for desired behaviour (for example, clamping values to the 0-1 range) and/or add logging when the function is called with invalid parameters. Feel free to post a new planner card related to this, even if you don't start on the task yourself. :)
| REQUIRE(red_of(black) == 0); | ||
| REQUIRE(green_of(black) == 0); | ||
| REQUIRE(blue_of(black) == 0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Check the alpha of the colour. HSB doesn't specify alpha so it should always be the max
| REQUIRE(red_of(black) == 0); | |
| REQUIRE(green_of(black) == 0); | |
| REQUIRE(blue_of(black) == 0); | |
| REQUIRE(red_of(black) == 0); | |
| REQUIRE(green_of(black) == 0); | |
| REQUIRE(blue_of(black) == 0); | |
| REQUIRE(alpha_of(black) == 255); |
coresdk/src/coresdk/color.cpp
Outdated
| } | ||
|
|
||
| /// _returs a color from a combination of hue, saturation, and brightness. | ||
| /// _returns a color from a combination of hue, saturation, and brightness. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure why some lines start with underscores in this file, so you should be good to tidy this comment up by removing them.
coresdk/src/coresdk/color.cpp
Outdated
| 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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can use std::clamp here for more concise code, since it's used in raspi_servo_driver.cpp
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Out of bounds tests look good to me!
dijidiji
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me and your tests run fine, just remove these temp files and I'll approve :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove these temporary files from the PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove these temporary files from the PR
dijidiji
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Approved 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @Broccoli811 ,
Nice job. Just a few changes before I approve this PR.
- Remove the comments above the function definitions in color.cpp. These are already included in color.h which is the proper convention. I know you weren't responsible for these comments but they shouldn't have been there in the first place.
- Change the warning log message in hsb_color to something clearer and more consistent with the rest of the Splashkit Library. I have added a comment with a suggestion.
- Add REQUIRE conditions for the alpha channel in all of your test sections. I saw you did it for the first two, but it would be good if you could do all of them.
Apart from that, everything looks pretty good and I especially like the changes you made to hsb_color to incorporate clamping using std::clamp. One thing you might consider looking into for the future is implementing clamping for all of the color functions because I noticed that not all of them use clamping and if they do, they use a different method. If all of the color functions used std::clamp, I think the code would be much more consistent but don't feel obligated to do this. This is something you could create a card for and potentially do as a future task.
Anyways, sorry to be overly pedantic with my requested changes. They shouldn't take to long to fix and when you do, I can approve this PR.
Sincerely,
SAm Stajnko.
coresdk/src/coresdk/color.cpp
Outdated
| /// gets a color given its color components. Each of the components has | ||
| /// a value between 0 and 1 | ||
| /// |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please delete this comment and all similar comments above the function definitions in color.cpp.
The declarations in color.h already have comments describing the function's purpose and therefore, the comments in color.cpp are redundant. If you look at other .cpp and .h files, you will see that this is the proper convention.
If there are any inline comments describing specific lines of code, you can leave these comments.
coresdk/src/coresdk/color.cpp
Outdated
| /// gets a color given its _r_g_b components. Each of the components has | ||
| /// a value between 0 and 1.0f. | ||
| /// |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please delete this comment and all similar comments above the function definitions in color.cpp.
The declarations in color.h already have comments describing the function's purpose and therefore, the comments in color.cpp are redundant. If you look at other .cpp and .h files, you will see that this is the proper convention.
If there are any inline comments describing specific lines of code, you can leave these comments.
coresdk/src/coresdk/color.cpp
Outdated
| /// gets a color given its _r_g_b components. Each of the components has | ||
| /// a value between 0 and 1. | ||
| /// |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please delete this comment and all similar comments above the function definitions in color.cpp.
The declarations in color.h already have comments describing the function's purpose and therefore, the comments in color.cpp are redundant. If you look at other .cpp and .h files, you will see that this is the proper convention.
If there are any inline comments describing specific lines of code, you can leave these comments.
coresdk/src/coresdk/color.cpp
Outdated
| /// 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. | ||
| /// |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please delete this comment and all similar comments above the function definitions in color.cpp.
The declarations in color.h already have comments describing the function's purpose and therefore, the comments in color.cpp are redundant. If you look at other .cpp and .h files, you will see that this is the proper convention.
If there are any inline comments describing specific lines of code, you can leave these comments.
coresdk/src/coresdk/color.cpp
Outdated
| 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."; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Warning message could be improved. Maybe something like the following would be better:
"Attempting to create a color from out-of-bounds HSB components. Values should be between 0 and 1."
I think this aligns better with other warning logs in the Splashkit library, making it more consistent.
| 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); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a REQUIRE condition for the alpha channel.
| 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); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a REQUIRE condition for the alpha channel.
| 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); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a REQUIRE condition for the alpha channel.
| 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); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a REQUIRE condition for the alpha channel.
| 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); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a REQUIRE condition for the alpha channel.
sam-stajnko
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have tested the changes made in this PR on my local machine and can confirm that they function as intended. The SKtests executable builds successfully and when ran, the unit tests for hsb_color return the expected outcome. I therefore have no issues approving this PR.
Nice work.
Regards,
Sam Stajnko.
|
I have discovered a critical mistake. I did not realize I was merging from my "main" branch instead of the intended branch, so I have copied over to a new branch with a proper name and have reset my main branch Here's the link to the new one: |
Description
The goal was to implement new test cases for a function called hsb_color from the color.cpp file to the unit_test_color.cpp file.
The Test case is named: "hsb_color converts HSB to RGB correctly". The associated test tag is "hsb_color"
Type of change
How Has This Been Tested?
I have tested this using the documentation as a guide. CMake operations were used to test if certain colors were a certain correct value.
(Converting HSB of red to RGB of red should HAVE no values for green and blue)
Black and Gray colors were also tested to see if the 'brightness' and 'saturation' variables were changed
Testing Checklist
Checklist