[3.12] gh-106498: Revert incorrect colorsys.rgb_to_hls change (GH-106627) (#106632)

gh-106498: Revert incorrect colorsys.rgb_to_hls change (GH-106627)

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.
(cherry picked from commit a2d54d4e8a)

Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu>
This commit is contained in:
Miss Islington (bot) 2023-07-11 08:47:15 -07:00 committed by GitHub
parent 6968f9e4d3
commit c594e25cd7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 1 deletions

View file

@ -83,7 +83,7 @@ def rgb_to_hls(r, g, b):
if l <= 0.5: if l <= 0.5:
s = rangec / sumc s = rangec / sumc
else: else:
s = rangec / (2.0-sumc) s = rangec / (2.0-maxc-minc) # Not always 2.0-sumc: gh-106498.
rc = (maxc-r) / rangec rc = (maxc-r) / rangec
gc = (maxc-g) / rangec gc = (maxc-g) / rangec
bc = (maxc-b) / rangec bc = (maxc-b) / rangec

View file

@ -69,6 +69,16 @@ class ColorsysTest(unittest.TestCase):
self.assertTripleEqual(hls, colorsys.rgb_to_hls(*rgb)) self.assertTripleEqual(hls, colorsys.rgb_to_hls(*rgb))
self.assertTripleEqual(rgb, colorsys.hls_to_rgb(*hls)) 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): def test_yiq_roundtrip(self):
for r in frange(0.0, 1.0, 0.2): for r in frange(0.0, 1.0, 0.2):
for g in frange(0.0, 1.0, 0.2): for g in frange(0.0, 1.0, 0.2):

View file

@ -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.