Back to index

python3.2  3.2.2
Functions | Variables
colorsys Namespace Reference

Functions

def rgb_to_yiq
def yiq_to_rgb
def rgb_to_hls
def hls_to_rgb
def _v
def rgb_to_hsv
def hsv_to_rgb

Variables

list __all__
float ONE_THIRD = 1.0
float ONE_SIXTH = 1.0
float TWO_THIRD = 2.0

Detailed Description

Conversion functions between RGB and other color systems.

This modules provides two functions for each color system ABC:

  rgb_to_abc(r, g, b) --> a, b, c
  abc_to_rgb(a, b, c) --> r, g, b

All inputs and outputs are triples of floats in the range [0.0...1.0]
(with the exception of I and Q, which covers a slightly larger range).
Inputs outside the valid range may cause exceptions or invalid outputs.

Supported color systems:
RGB: Red, Green, Blue components
YIQ: Luminance, Chrominance (used by composite video signals)
HLS: Hue, Luminance, Saturation
HSV: Hue, Saturation, Value

Function Documentation

def colorsys._v (   m1,
  m2,
  hue 
) [private]

Definition at line 100 of file colorsys.py.

00100 
00101 def _v(m1, m2, hue):
00102     hue = hue % 1.0
00103     if hue < ONE_SIXTH:
00104         return m1 + (m2-m1)*hue*6.0
00105     if hue < 0.5:
00106         return m2
00107     if hue < TWO_THIRD:
00108         return m1 + (m2-m1)*(TWO_THIRD-hue)*6.0
00109     return m1
00110 
00111 
00112 # HSV: Hue, Saturation, Value
00113 # H: position in the spectrum
00114 # S: color saturation ("purity")
00115 # V: color brightness

Here is the caller graph for this function:

def colorsys.hls_to_rgb (   h,
  l,
  s 
)

Definition at line 90 of file colorsys.py.

00090 
00091 def hls_to_rgb(h, l, s):
00092     if s == 0.0:
00093         return l, l, l
00094     if l <= 0.5:
00095         m2 = l * (1.0+s)
00096     else:
00097         m2 = l+s-(l*s)
00098     m1 = 2.0*l - m2
00099     return (_v(m1, m2, h+ONE_THIRD), _v(m1, m2, h), _v(m1, m2, h-ONE_THIRD))

Here is the call graph for this function:

Here is the caller graph for this function:

def colorsys.hsv_to_rgb (   h,
  s,
  v 
)

Definition at line 135 of file colorsys.py.

00135 
00136 def hsv_to_rgb(h, s, v):
00137     if s == 0.0:
00138         return v, v, v
00139     i = int(h*6.0) # XXX assume int() truncates!
00140     f = (h*6.0) - i
00141     p = v*(1.0 - s)
00142     q = v*(1.0 - s*f)
00143     t = v*(1.0 - s*(1.0-f))
00144     i = i%6
00145     if i == 0:
00146         return v, t, p
00147     if i == 1:
00148         return q, v, p
00149     if i == 2:
00150         return p, v, t
00151     if i == 3:
00152         return p, q, v
00153     if i == 4:
00154         return t, p, v
00155     if i == 5:
00156         return v, p, q
00157     # Cannot get here

Here is the caller graph for this function:

def colorsys.rgb_to_hls (   r,
  g,
  b 
)

Definition at line 67 of file colorsys.py.

00067 
00068 def rgb_to_hls(r, g, b):
00069     maxc = max(r, g, b)
00070     minc = min(r, g, b)
00071     # XXX Can optimize (maxc+minc) and (maxc-minc)
00072     l = (minc+maxc)/2.0
00073     if minc == maxc:
00074         return 0.0, l, 0.0
00075     if l <= 0.5:
00076         s = (maxc-minc) / (maxc+minc)
00077     else:
00078         s = (maxc-minc) / (2.0-maxc-minc)
00079     rc = (maxc-r) / (maxc-minc)
00080     gc = (maxc-g) / (maxc-minc)
00081     bc = (maxc-b) / (maxc-minc)
00082     if r == maxc:
00083         h = bc-gc
00084     elif g == maxc:
00085         h = 2.0+rc-bc
00086     else:
00087         h = 4.0+gc-rc
00088     h = (h/6.0) % 1.0
00089     return h, l, s

Here is the caller graph for this function:

def colorsys.rgb_to_hsv (   r,
  g,
  b 
)

Definition at line 116 of file colorsys.py.

00116 
00117 def rgb_to_hsv(r, g, b):
00118     maxc = max(r, g, b)
00119     minc = min(r, g, b)
00120     v = maxc
00121     if minc == maxc:
00122         return 0.0, 0.0, v
00123     s = (maxc-minc) / maxc
00124     rc = (maxc-r) / (maxc-minc)
00125     gc = (maxc-g) / (maxc-minc)
00126     bc = (maxc-b) / (maxc-minc)
00127     if r == maxc:
00128         h = bc-gc
00129     elif g == maxc:
00130         h = 2.0+rc-bc
00131     else:
00132         h = 4.0+gc-rc
00133     h = (h/6.0) % 1.0
00134     return h, s, v

Here is the caller graph for this function:

def colorsys.rgb_to_yiq (   r,
  g,
  b 
)

Definition at line 37 of file colorsys.py.

00037 
00038 def rgb_to_yiq(r, g, b):
00039     y = 0.30*r + 0.59*g + 0.11*b
00040     i = 0.60*r - 0.28*g - 0.32*b
00041     q = 0.21*r - 0.52*g + 0.31*b
00042     return (y, i, q)

def colorsys.yiq_to_rgb (   y,
  i,
  q 
)

Definition at line 43 of file colorsys.py.

00043 
00044 def yiq_to_rgb(y, i, q):
00045     r = y + 0.948262*i + 0.624013*q
00046     g = y - 0.276066*i - 0.639810*q
00047     b = y - 1.105450*i + 1.729860*q
00048     if r < 0.0:
00049         r = 0.0
00050     if g < 0.0:
00051         g = 0.0
00052     if b < 0.0:
00053         b = 0.0
00054     if r > 1.0:
00055         r = 1.0
00056     if g > 1.0:
00057         g = 1.0
00058     if b > 1.0:
00059         b = 1.0
00060     return (r, g, b)
00061 
00062 
00063 # HLS: Hue, Luminance, Saturation
00064 # H: position in the spectrum
00065 # L: color lightness
00066 # S: color saturation


Variable Documentation

Initial value:
00001 ["rgb_to_yiq","yiq_to_rgb","rgb_to_hls","hls_to_rgb",
00002            "rgb_to_hsv","hsv_to_rgb"]

Definition at line 24 of file colorsys.py.

float colorsys.ONE_SIXTH = 1.0

Definition at line 30 of file colorsys.py.

float colorsys.ONE_THIRD = 1.0

Definition at line 29 of file colorsys.py.

float colorsys.TWO_THIRD = 2.0

Definition at line 31 of file colorsys.py.