Skip to content

Commit 5209a13

Browse files
committed
gh-148330: colorsys: Add docstrings to module and all functions
1 parent d3b7b93 commit 5209a13

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

Lib/colorsys.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,21 @@
3838
# The ones in this library uses constants from the FCC version of NTSC.
3939

4040
def rgb_to_yiq(r, g, b):
41+
"""Convert RGB to YIQ.
42+
43+
r, g, b are in [0, 1].
44+
Return (y, i, q) where y is in [0, 1].
45+
"""
4146
y = 0.30*r + 0.59*g + 0.11*b
4247
i = 0.74*(r-y) - 0.27*(b-y)
4348
q = 0.48*(r-y) + 0.41*(b-y)
4449
return (y, i, q)
4550

4651
def yiq_to_rgb(y, i, q):
52+
"""Convert YIQ to RGB.
53+
54+
Return (r, g, b) in [0, 1].
55+
"""
4756
# r = y + (0.27*q + 0.41*i) / (0.74*0.41 + 0.27*0.48)
4857
# b = y + (0.74*q - 0.48*i) / (0.74*0.41 + 0.27*0.48)
4958
# g = y - (0.30*(r-y) + 0.11*(b-y)) / 0.59
@@ -73,6 +82,11 @@ def yiq_to_rgb(y, i, q):
7382
# S: color saturation
7483

7584
def rgb_to_hls(r, g, b):
85+
"""Convert RGB to HLS (Hue, Lightness, Saturation).
86+
87+
r, g, b are in [0, 1].
88+
Return (h, l, s) in [0, 1].
89+
"""
7690
maxc = max(r, g, b)
7791
minc = min(r, g, b)
7892
sumc = (maxc+minc)
@@ -97,6 +111,11 @@ def rgb_to_hls(r, g, b):
97111
return h, l, s
98112

99113
def hls_to_rgb(h, l, s):
114+
"""Convert HLS (Hue, Lightness, Saturation) to RGB.
115+
116+
h, l, s are in [0, 1].
117+
Return (r, g, b) in [0, 1].
118+
"""
100119
if s == 0.0:
101120
return l, l, l
102121
if l <= 0.5:
@@ -123,6 +142,11 @@ def _v(m1, m2, hue):
123142
# V: color brightness
124143

125144
def rgb_to_hsv(r, g, b):
145+
"""Convert RGB to HSV (Hue, Saturation, Value).
146+
147+
r, g, b are in [0, 1].
148+
Return (h, s, v) in [0, 1].
149+
"""
126150
maxc = max(r, g, b)
127151
minc = min(r, g, b)
128152
rangec = (maxc-minc)
@@ -143,6 +167,11 @@ def rgb_to_hsv(r, g, b):
143167
return h, s, v
144168

145169
def hsv_to_rgb(h, s, v):
170+
"""Convert HSV (Hue, Saturation, Value) to RGB.
171+
172+
h, s, v are in [0, 1].
173+
Return (r, g, b) in [0, 1].
174+
"""
146175
if s == 0.0:
147176
return v, v, v
148177
i = int(h*6.0) # XXX assume int() truncates!

0 commit comments

Comments
 (0)