FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-106498: Fix an extreme case in `colorsys.rgb_to_hls` by gaogaotiantian · Pull Request #106530 · python/cpython · GitHub

Repository navigation

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
8 changes: 7 additions & 1 deletion Lib/colorsys.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,13 @@ def rgb_to_hls(r, g, b):
sumc = (maxc+minc)
rangec = (maxc-minc)
l = sumc/2.0
if minc == maxc:
# gh-106498
# Due to a degree of float precision error, it's possible that maxc != minc
# but maxc + minc == 2.0. Which will make (2.0 - sumc) zero, causing the
# ZeroDivisionError.
# e.g. colorsys.rgb_to_hls(1, 1, 0.9999999999999999)
# In this case, it's an extreme near white color so we can just return white
if minc == maxc or sumc == 2.0:
return 0.0, l, 0.0
if l <= 0.5:
s = rangec / sumc
Expand Down
6 changes: 6 additions & 0 deletions Lib/test/test_colorsys.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ def test_hls_values(self):
self.assertTripleEqual(hls, colorsys.rgb_to_hls(*rgb))
self.assertTripleEqual(rgb, colorsys.hls_to_rgb(*hls))

def test_hls_corner_case(self):
try:
colorsys.rgb_to_hls(1, 1, 0.9999999999999999)
except Exception:
self.fail("rgb_to_hls(1, 1, 0.9999999999999999) raised an exception!")

def test_yiq_roundtrip(self):
for r in frange(0.0, 1.0, 0.2):
for g in frange(0.0, 1.0, 0.2):
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed :func:`colorsys.rgb_to_hls` under an extreme input.

Back | FazBrowse Home | New Git URL