Skip to content

Raise ValueError for RGB values outside the [0, 1] range in rgb_to_hsv function #28948

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

Merged
merged 10 commits into from
Oct 10, 2024
Merged

Raise ValueError for RGB values outside the [0, 1] range in rgb_to_hsv function #28948

merged 10 commits into from
Oct 10, 2024

Conversation

PedroBittarBarao
Copy link
Contributor

This change improves the rgb_to_hsv function by adding a clear and explicit error message when the input RGB values are outside the valid range [0, 1]. Previously, the error raised by NumPy was not intuitive, making it difficult for users to understand the issue. This update ensures that users are notified of the value range requirements upfront, enhancing usability and debugging experience.
Fix #28941

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for opening your first PR into Matplotlib!

If you have not heard from us in a week or so, please leave a new comment below and that should bring it to our attention. Most of our reviewers are volunteers and sometimes things fall through the cracks.

You can also join us on gitter for real-time discussion.

For details on testing, writing docs, and our review process, please see the developer guide

We strive to be a welcoming and open project. Please follow our Code of Conduct.

@PedroBittarBarao PedroBittarBarao marked this pull request as draft October 7, 2024 17:34
@PedroBittarBarao PedroBittarBarao marked this pull request as ready for review October 7, 2024 18:28
# Check if input is in the expected range
if np.any(arr_max > 1) or np.any(arr < 0):
raise ValueError("Input RGB values must be in the range [0, 1]. "
f"Found values out of range in array: {arr}")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Displaying the whole array generally does not make sense. Either leave out the value completely or state the offending max or min value; for example "Input RGB values must be in the range [0, 1]. Found a maximum of 253" may be a useful hint. When going with the values, you want to do two separate if statements and issue tailored messages.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. I will change the code to address the issue with the error messages

Copy link
Member

@timhoffm timhoffm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approval was premature. Please fix the above issue.

@PedroBittarBarao
Copy link
Contributor Author

I believe I've now addressed all raised points. Please let me know if any further adjustments are required.

@oscargus oscargus added this to the v3.10.0 milestone Oct 10, 2024
@oscargus oscargus merged commit 0f70a22 into matplotlib:main Oct 10, 2024
39 of 43 checks passed
kyracho pushed a commit to kyracho/matplotlib that referenced this pull request Oct 10, 2024
…v function (matplotlib#28948)

* initial error correction

* Remove unintentional white-space

* Reorder closer to original

* Remove unwanted whitespace

* flake8 compliance

* Improved Error messages

* Update lib/matplotlib/colors.py

Co-authored-by: Tim Hoffmann <[email protected]>

* Update lib/matplotlib/colors.py

Co-authored-by: Tim Hoffmann <[email protected]>

* Update lib/matplotlib/colors.py

Co-authored-by: Tim Hoffmann <[email protected]>

* remove unnecessary arr_min calculation

---------

Co-authored-by: Tim Hoffmann <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: unexplicit error message when using matplotlib.colors.rgb_to_hsv() with wrong input
3 participants