PyXR

c:\python24\lib \ colorsys.py



0001 """Conversion functions between RGB and other color systems.
0002 
0003 This modules provides two functions for each color system ABC:
0004 
0005   rgb_to_abc(r, g, b) --> a, b, c
0006   abc_to_rgb(a, b, c) --> r, g, b
0007 
0008 All inputs and outputs are triples of floats in the range [0.0...1.0].
0009 Inputs outside this range may cause exceptions or invalid outputs.
0010 
0011 Supported color systems:
0012 RGB: Red, Green, Blue components
0013 YIQ: used by composite video signals
0014 HLS: Hue, Luminance, Saturation
0015 HSV: Hue, Saturation, Value
0016 """
0017 # References:
0018 # XXX Where's the literature?
0019 
0020 __all__ = ["rgb_to_yiq","yiq_to_rgb","rgb_to_hls","hls_to_rgb",
0021            "rgb_to_hsv","hsv_to_rgb"]
0022 
0023 # Some floating point constants
0024 
0025 ONE_THIRD = 1.0/3.0
0026 ONE_SIXTH = 1.0/6.0
0027 TWO_THIRD = 2.0/3.0
0028 
0029 
0030 # YIQ: used by composite video signals (linear combinations of RGB)
0031 # Y: perceived grey level (0.0 == black, 1.0 == white)
0032 # I, Q: color components
0033 
0034 def rgb_to_yiq(r, g, b):
0035     y = 0.30*r + 0.59*g + 0.11*b
0036     i = 0.60*r - 0.28*g - 0.32*b
0037     q = 0.21*r - 0.52*g + 0.31*b
0038     return (y, i, q)
0039 
0040 def yiq_to_rgb(y, i, q):
0041     r = y + 0.948262*i + 0.624013*q
0042     g = y - 0.276066*i - 0.639810*q
0043     b = y - 1.105450*i + 1.729860*q
0044     if r < 0.0: r = 0.0
0045     if g < 0.0: g = 0.0
0046     if b < 0.0: b = 0.0
0047     if r > 1.0: r = 1.0
0048     if g > 1.0: g = 1.0
0049     if b > 1.0: b = 1.0
0050     return (r, g, b)
0051 
0052 
0053 # HLS: Hue, Luminance, S???
0054 # H: position in the spectrum
0055 # L: ???
0056 # S: ???
0057 
0058 def rgb_to_hls(r, g, b):
0059     maxc = max(r, g, b)
0060     minc = min(r, g, b)
0061     # XXX Can optimize (maxc+minc) and (maxc-minc)
0062     l = (minc+maxc)/2.0
0063     if minc == maxc: return 0.0, l, 0.0
0064     if l <= 0.5: s = (maxc-minc) / (maxc+minc)
0065     else: s = (maxc-minc) / (2.0-maxc-minc)
0066     rc = (maxc-r) / (maxc-minc)
0067     gc = (maxc-g) / (maxc-minc)
0068     bc = (maxc-b) / (maxc-minc)
0069     if r == maxc: h = bc-gc
0070     elif g == maxc: h = 2.0+rc-bc
0071     else: h = 4.0+gc-rc
0072     h = (h/6.0) % 1.0
0073     return h, l, s
0074 
0075 def hls_to_rgb(h, l, s):
0076     if s == 0.0: return l, l, l
0077     if l <= 0.5: m2 = l * (1.0+s)
0078     else: m2 = l+s-(l*s)
0079     m1 = 2.0*l - m2
0080     return (_v(m1, m2, h+ONE_THIRD), _v(m1, m2, h), _v(m1, m2, h-ONE_THIRD))
0081 
0082 def _v(m1, m2, hue):
0083     hue = hue % 1.0
0084     if hue < ONE_SIXTH: return m1 + (m2-m1)*hue*6.0
0085     if hue < 0.5: return m2
0086     if hue < TWO_THIRD: return m1 + (m2-m1)*(TWO_THIRD-hue)*6.0
0087     return m1
0088 
0089 
0090 # HSV: Hue, Saturation, Value(?)
0091 # H: position in the spectrum
0092 # S: ???
0093 # V: ???
0094 
0095 def rgb_to_hsv(r, g, b):
0096     maxc = max(r, g, b)
0097     minc = min(r, g, b)
0098     v = maxc
0099     if minc == maxc: return 0.0, 0.0, v
0100     s = (maxc-minc) / maxc
0101     rc = (maxc-r) / (maxc-minc)
0102     gc = (maxc-g) / (maxc-minc)
0103     bc = (maxc-b) / (maxc-minc)
0104     if r == maxc: h = bc-gc
0105     elif g == maxc: h = 2.0+rc-bc
0106     else: h = 4.0+gc-rc
0107     h = (h/6.0) % 1.0
0108     return h, s, v
0109 
0110 def hsv_to_rgb(h, s, v):
0111     if s == 0.0: return v, v, v
0112     i = int(h*6.0) # XXX assume int() truncates!
0113     f = (h*6.0) - i
0114     p = v*(1.0 - s)
0115     q = v*(1.0 - s*f)
0116     t = v*(1.0 - s*(1.0-f))
0117     if i%6 == 0: return v, t, p
0118     if i == 1: return q, v, p
0119     if i == 2: return p, v, t
0120     if i == 3: return p, q, v
0121     if i == 4: return t, p, v
0122     if i == 5: return v, p, q
0123     # Cannot get here
0124 

Generated by PyXR 0.9.4
SourceForge.net Logo