diff options
author | Julien Jerphanion <git@jjerphan.xyz> | 2020-11-28 07:11:19 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-11-28 07:11:19 (GMT) |
commit | f9195318a863e237f41ed7665c767028cde1c9a3 (patch) | |
tree | f4576ae54276a274cb1cdd990fa007daa371b00e | |
parent | 44ca05afc89c9967f5dbc6c3ad89fc298c460e93 (diff) | |
download | cpython-f9195318a863e237f41ed7665c767028cde1c9a3.zip cpython-f9195318a863e237f41ed7665c767028cde1c9a3.tar.gz cpython-f9195318a863e237f41ed7665c767028cde1c9a3.tar.bz2 |
bpo-42452: Improve colorsys.rgb_to_hls code (GH-23306)
Cache repeated sum and difference to make code slightly faster and easier to read.
-rw-r--r-- | Lib/colorsys.py | 15 |
1 files changed, 8 insertions, 7 deletions
diff --git a/Lib/colorsys.py b/Lib/colorsys.py index b93e384..0f52512 100644 --- a/Lib/colorsys.py +++ b/Lib/colorsys.py @@ -75,17 +75,18 @@ def yiq_to_rgb(y, i, q): def rgb_to_hls(r, g, b): maxc = max(r, g, b) minc = min(r, g, b) - # XXX Can optimize (maxc+minc) and (maxc-minc) - l = (minc+maxc)/2.0 + sumc = (maxc+minc) + rangec = (maxc-minc) + l = sumc/2.0 if minc == maxc: return 0.0, l, 0.0 if l <= 0.5: - s = (maxc-minc) / (maxc+minc) + s = rangec / sumc else: - s = (maxc-minc) / (2.0-maxc-minc) - rc = (maxc-r) / (maxc-minc) - gc = (maxc-g) / (maxc-minc) - bc = (maxc-b) / (maxc-minc) + s = rangec / (2.0-sumc) + rc = (maxc-r) / rangec + gc = (maxc-g) / rangec + bc = (maxc-b) / rangec if r == maxc: h = bc-gc elif g == maxc: |