Skip to content

Commit 436be2b

Browse files
committed
Upgrade colorsys from Python 3.11
1 parent 89323da commit 436be2b

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

Lib/colorsys.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
"""Conversion functions between RGB and other color systems.
2+
23
This modules provides two functions for each color system ABC:
4+
35
rgb_to_abc(r, g, b) --> a, b, c
46
abc_to_rgb(a, b, c) --> r, g, b
7+
58
All inputs and outputs are triples of floats in the range [0.0...1.0]
69
(with the exception of I and Q, which covers a slightly larger range).
710
Inputs outside the valid range may cause exceptions or invalid outputs.
11+
812
Supported color systems:
913
RGB: Red, Green, Blue components
1014
YIQ: Luminance, Chrominance (used by composite video signals)
@@ -71,17 +75,18 @@ def yiq_to_rgb(y, i, q):
7175
def rgb_to_hls(r, g, b):
7276
maxc = max(r, g, b)
7377
minc = min(r, g, b)
74-
# XXX Can optimize (maxc+minc) and (maxc-minc)
75-
l = (minc+maxc)/2.0
78+
sumc = (maxc+minc)
79+
rangec = (maxc-minc)
80+
l = sumc/2.0
7681
if minc == maxc:
7782
return 0.0, l, 0.0
7883
if l <= 0.5:
79-
s = (maxc-minc) / (maxc+minc)
84+
s = rangec / sumc
8085
else:
81-
s = (maxc-minc) / (2.0-maxc-minc)
82-
rc = (maxc-r) / (maxc-minc)
83-
gc = (maxc-g) / (maxc-minc)
84-
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
8590
if r == maxc:
8691
h = bc-gc
8792
elif g == maxc:
@@ -120,13 +125,14 @@ def _v(m1, m2, hue):
120125
def rgb_to_hsv(r, g, b):
121126
maxc = max(r, g, b)
122127
minc = min(r, g, b)
128+
rangec = (maxc-minc)
123129
v = maxc
124130
if minc == maxc:
125131
return 0.0, 0.0, v
126-
s = (maxc-minc) / maxc
127-
rc = (maxc-r) / (maxc-minc)
128-
gc = (maxc-g) / (maxc-minc)
129-
bc = (maxc-b) / (maxc-minc)
132+
s = rangec / maxc
133+
rc = (maxc-r) / rangec
134+
gc = (maxc-g) / rangec
135+
bc = (maxc-b) / rangec
130136
if r == maxc:
131137
h = bc-gc
132138
elif g == maxc:

0 commit comments

Comments
 (0)