Skip to content

Fix division by zero bug in colorsys.rgb_to_hls() for near-white colors#139215

Closed
Anuj092 wants to merge 1 commit intopython:mainfrom
Anuj092:fix-colorsys-division-by-zero
Closed

Fix division by zero bug in colorsys.rgb_to_hls() for near-white colors#139215
Anuj092 wants to merge 1 commit intopython:mainfrom
Anuj092:fix-colorsys-division-by-zero

Conversation

@Anuj092
Copy link
Copy Markdown

@Anuj092 Anuj092 commented Sep 21, 2025

Fixes gh-106498: Prevent ZeroDivisionError when converting RGB values very close to white (e.g., (0.9999999999999999, 1, 1)) to HLS.

Changes:

  • Add numerical stability check in rgb_to_hls() function
  • When denominator (2.0 - maxc - minc) is very close to zero, set s = 0.0
  • Improve HSV hue handling with explicit normalization
  • Add test case for hue wrapping edge cases

This fix prevents crashes when converting colors very close to pure white while maintaining backward compatibility and mathematical correctness.

Fixes gh-106498: Prevent ZeroDivisionError when converting RGB values
very close to white (e.g., (0.9999999999999999, 1, 1)) to HLS.

Changes:
- Add numerical stability check in rgb_to_hls() function
- When denominator (2.0 - maxc - minc) is very close to zero, set s = 0.0
- Improve HSV hue handling with explicit normalization
- Add test case for hue wrapping edge cases

This fix prevents crashes when converting colors very close to pure white
while maintaining backward compatibility and mathematical correctness.
@bedevere-app
Copy link
Copy Markdown

bedevere-app Bot commented Sep 21, 2025

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@ZeroIntensity
Copy link
Copy Markdown
Member

Hi @Anuj092!

We appreciate your enthusiasm, but using AI to generate pull requests is not helpful. LLMs can be useful for many things, but in this case, it's just noise on our issue tracker (see our AI policy). The underlying bug that this PR is fixing has already been fixed since Python 3.10.

If you're interested in contributing, you're free to use AI, but please be sure to use your own critical thinking before submitting a pull request. Thanks!

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.

colorsys.rgb_to_hls raises ZeroDivisionError on nearly-white inputs

2 participants