8000 bpo-42452: Improve colorsys.rgb_to_hls code (GH-23306) · python/cpython@f919531 · GitHub
[go: up one dir, main page]

Skip to content

Commit f919531

Browse files
authored
bpo-42452: Improve colorsys.rgb_to_hls code (GH-23306)
Cache repeated sum and difference to make code slightly faster and easier to read.
1 parent 44ca05a commit f919531

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

Lib/colorsys.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,17 +75,18 @@ def yiq_to_rgb(y, i, q):
7575
def rgb_to_hls(r, g, b):
7676
maxc = max(r, g, b)
7777
minc = min(r, g, b)
78-
# XXX Can optimize (maxc+minc) and (maxc-minc)
79-
l = (minc+maxc)/2.0
78+
sumc = (maxc+minc)
79+
rangec = (maxc-minc)
80+
l = sumc/2.0
8081
if minc == maxc:
8182
return 0.0, l, 0.0
8283
if l <= 0.5:
83-
s = (maxc-minc) / (maxc+minc)
84+
s = rangec / sumc
8485
else:
85-
s = (maxc-minc) / (2.0-maxc-minc)
86-
rc = (maxc-r) / (maxc-minc)
87-
gc = (maxc-g) / (maxc-minc)
88-
bc = (maxc-b) / (maxc-minc)
86+
s = rangec / (2.0-sumc)
87+
rc = (maxc-r) / rangec
88+
gc = (maxc-g) / rangec
89+
bc = (maxc-b) / rangec
8990
if r == maxc:
9091
h = bc-gc
9192
elif g == maxc:

0 commit comments

Comments
 (0)
0