Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions Lib/colorsys.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,21 @@
# The ones in this library uses constants from the FCC version of NTSC.

def rgb_to_yiq(r, g, b):
"""Convert RGB to YIQ.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is good, but just to clean it up a little more, just put the string next to the start of the doctoring on another line. For example:
Instead of

"""Convert RGB to YIQ.
"""

It becomes

"""
Convert RGB to YIQ.
"""

And this change would apply to the other lines that follow this pattern as well.


r, g, b are in [0, 1].
Return (y, i, q) where y is in [0, 1].
"""
y = 0.30*r + 0.59*g + 0.11*b
i = 0.74*(r-y) - 0.27*(b-y)
q = 0.48*(r-y) + 0.41*(b-y)
return (y, i, q)

def yiq_to_rgb(y, i, q):
"""Convert YIQ to RGB.

Return (r, g, b) in [0, 1].
"""
# r = y + (0.27*q + 0.41*i) / (0.74*0.41 + 0.27*0.48)
# b = y + (0.74*q - 0.48*i) / (0.74*0.41 + 0.27*0.48)
# g = y - (0.30*(r-y) + 0.11*(b-y)) / 0.59
Expand Down Expand Up @@ -73,6 +82,11 @@ def yiq_to_rgb(y, i, q):
# S: color saturation

def rgb_to_hls(r, g, b):
"""Convert RGB to HLS (Hue, Lightness, Saturation).

r, g, b are in [0, 1].
Return (h, l, s) in [0, 1].
"""
maxc = max(r, g, b)
minc = min(r, g, b)
sumc = (maxc+minc)
Expand All @@ -97,6 +111,11 @@ def rgb_to_hls(r, g, b):
return h, l, s

def hls_to_rgb(h, l, s):
"""Convert HLS (Hue, Lightness, Saturation) to RGB.

h, l, s are in [0, 1].
Return (r, g, b) in [0, 1].
"""
if s == 0.0:
return l, l, l
if l <= 0.5:
Expand All @@ -123,6 +142,11 @@ def _v(m1, m2, hue):
# V: color brightness

def rgb_to_hsv(r, g, b):
"""Convert RGB to HSV (Hue, Saturation, Value).

r, g, b are in [0, 1].
Return (h, s, v) in [0, 1].
"""
maxc = max(r, g, b)
minc = min(r, g, b)
rangec = (maxc-minc)
Expand All @@ -143,6 +167,11 @@ def rgb_to_hsv(r, g, b):
return h, s, v

def hsv_to_rgb(h, s, v):
"""Convert HSV (Hue, Saturation, Value) to RGB.

h, s, v are in [0, 1].
Return (r, g, b) in [0, 1].
"""
if s == 0.0:
return v, v, v
i = int(h*6.0) # XXX assume int() truncates!
Expand Down
Loading