Skip to content

Commit

Permalink
bpo-42452: Improve colorsys.rgb_to_hls code (GH-23306)
Browse files Browse the repository at this point in the history
Cache repeated sum and difference to make code slightly faster and easier to read.
  • Loading branch information
jjerphan committed Nov 28, 2020
1 parent 44ca05a commit f919531
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions Lib/colorsys.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit f919531

Please sign in to comment.