]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-106498: Revert incorrect colorsys.rgb_to_hls change (#106627)
authorTerry Jan Reedy <tjreedy@udel.edu>
Tue, 11 Jul 2023 15:07:20 +0000 (11:07 -0400)
committerGitHub <noreply@github.com>
Tue, 11 Jul 2023 15:07:20 +0000 (11:07 -0400)
gh-86618 assumed a-b-c = a-(b+c) = a-d where d = b+d.
For floats 2.0, 1.0, and 0.9999999999999999, this assumption
is false.  The net change of 1.1102230246251565e-16 to 0.0
results in division by 0.  Revert the replacement.  Add test.

Lib/colorsys.py
Lib/test/test_colorsys.py
Misc/NEWS.d/next/Library/2023-07-11-09-25-40.gh-issue-106530.VgXrMx.rst [new file with mode: 0644]

index 9bdc83e377260339bf0578ab85d00ff8f9fbcc85..bc897bd0f9929828a4a826879b7274b18a442b81 100644 (file)
@@ -83,7 +83,7 @@ def rgb_to_hls(r, g, b):
     if l <= 0.5:
         s = rangec / sumc
     else:
-        s = rangec / (2.0-sumc)
+        s = rangec / (2.0-maxc-minc)  # Not always 2.0-sumc: gh-106498.
     rc = (maxc-r) / rangec
     gc = (maxc-g) / rangec
     bc = (maxc-b) / rangec
index a24e3adcb4b842ea9ba246b8d75a296181a8a081..74d76294b0b4d467e9a4cdd44cb6d208d2d78235 100644 (file)
@@ -69,6 +69,16 @@ class ColorsysTest(unittest.TestCase):
             self.assertTripleEqual(hls, colorsys.rgb_to_hls(*rgb))
             self.assertTripleEqual(rgb, colorsys.hls_to_rgb(*hls))
 
+    def test_hls_nearwhite(self):  # gh-106498
+        values = (
+            # rgb, hls: these do not work in reverse
+            ((0.9999999999999999, 1, 1), (0.5, 1.0, 1.0)),
+            ((1, 0.9999999999999999, 0.9999999999999999), (0.0, 1.0, 1.0)),
+        )
+        for rgb, hls in values:
+            self.assertTripleEqual(hls, colorsys.rgb_to_hls(*rgb))
+            self.assertTripleEqual((1.0, 1.0, 1.0), colorsys.hls_to_rgb(*hls))
+
     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):
diff --git a/Misc/NEWS.d/next/Library/2023-07-11-09-25-40.gh-issue-106530.VgXrMx.rst b/Misc/NEWS.d/next/Library/2023-07-11-09-25-40.gh-issue-106530.VgXrMx.rst
new file mode 100644 (file)
index 0000000..09fc647
--- /dev/null
@@ -0,0 +1,2 @@
+Revert a change to :func:`colorsys.rgb_to_hls` that caused division by zero
+for certain almost-white inputs.  Patch by Terry Jan Reedy.