1"""Conversion functions between RGB and other color systems.
2
3This modules provides two functions for each color system ABC:
4
5  rgb_to_abc(r, g, b) --> a, b, c
6  abc_to_rgb(a, b, c) --> r, g, b
7
8All inputs and outputs are triples of floats in the range [0.0...1.0]
9(with the exception of I and Q, which covers a slightly larger range).
10Inputs outside the valid range may cause exceptions or invalid outputs.
11
12Supported color systems:
13RGB: Red, Green, Blue components
14YIQ: Luminance, Chrominance (used by composite video signals)
15HLS: Hue, Luminance, Saturation
16HSV: Hue, Saturation, Value
17"""
18
19# References:
20# http://en.wikipedia.org/wiki/YIQ
21# http://en.wikipedia.org/wiki/HLS_color_space
22# http://en.wikipedia.org/wiki/HSV_color_space
23
24__all__ = ["rgb_to_yiq","yiq_to_rgb","rgb_to_hls","hls_to_rgb",
25           "rgb_to_hsv","hsv_to_rgb"]
26
27# Some floating point constants
28
29ONE_THIRD = 1.0/3.0
30ONE_SIXTH = 1.0/6.0
31TWO_THIRD = 2.0/3.0
32
33# YIQ: used by composite video signals (linear combinations of RGB)
34# Y: perceived grey level (0.0 == black, 1.0 == white)
35# I, Q: color components
36#
37# There are a great many versions of the constants used in these formulae.
38# The ones in this library uses constants from the FCC version of NTSC.
39
40def rgb_to_yiq(r, g, b):
41    y = 0.30*r + 0.59*g + 0.11*b
42    i = 0.74*(r-y) - 0.27*(b-y)
43    q = 0.48*(r-y) + 0.41*(b-y)
44    return (y, i, q)
45
46def yiq_to_rgb(y, i, q):
47    # r = y + (0.27*q + 0.41*i) / (0.74*0.41 + 0.27*0.48)
48    # b = y + (0.74*q - 0.48*i) / (0.74*0.41 + 0.27*0.48)
49    # g = y - (0.30*(r-y) + 0.11*(b-y)) / 0.59
50
51    r = y + 0.9468822170900693*i + 0.6235565819861433*q
52    g = y - 0.27478764629897834*i - 0.6356910791873801*q
53    b = y - 1.1085450346420322*i + 1.7090069284064666*q
54
55    if r < 0.0:
56        r = 0.0
57    if g < 0.0:
58        g = 0.0
59    if b < 0.0:
60        b = 0.0
61    if r > 1.0:
62        r = 1.0
63    if g > 1.0:
64        g = 1.0
65    if b > 1.0:
66        b = 1.0
67    return (r, g, b)
68
69
70# HLS: Hue, Luminance, Saturation
71# H: position in the spectrum
72# L: color lightness
73# S: color saturation
74
75def rgb_to_hls(r, g, b):
76    maxc = max(r, g, b)
77    minc = min(r, g, b)
78    # XXX Can optimize (maxc+minc) and (maxc-minc)
79    l = (minc+maxc)/2.0
80    if minc == maxc:
81        return 0.0, l, 0.0
82    if l <= 0.5:
83        s = (maxc-minc) / (maxc+minc)
84    else:
85        s = (maxc-minc) / (2.0-maxc-minc)
86    rc = (maxc-r) / (maxc-minc)
87    gc = (maxc-g) / (maxc-minc)
88    bc = (maxc-b) / (maxc-minc)
89    if r == maxc:
90        h = bc-gc
91    elif g == maxc:
92        h = 2.0+rc-bc
93    else:
94        h = 4.0+gc-rc
95    h = (h/6.0) % 1.0
96    return h, l, s
97
98def hls_to_rgb(h, l, s):
99    if s == 0.0:
100        return l, l, l
101    if l <= 0.5:
102        m2 = l * (1.0+s)
103    else:
104        m2 = l+s-(l*s)
105    m1 = 2.0*l - m2
106    return (_v(m1, m2, h+ONE_THIRD), _v(m1, m2, h), _v(m1, m2, h-ONE_THIRD))
107
108def _v(m1, m2, hue):
109    hue = hue % 1.0
110    if hue < ONE_SIXTH:
111        return m1 + (m2-m1)*hue*6.0
112    if hue < 0.5:
113        return m2
114    if hue < TWO_THIRD:
115        return m1 + (m2-m1)*(TWO_THIRD-hue)*6.0
116    return m1
117
118
119# HSV: Hue, Saturation, Value
120# H: position in the spectrum
121# S: color saturation ("purity")
122# V: color brightness
123
124def rgb_to_hsv(r, g, b):
125    maxc = max(r, g, b)
126    minc = min(r, g, b)
127    v = maxc
128    if minc == maxc:
129        return 0.0, 0.0, v
130    s = (maxc-minc) / maxc
131    rc = (maxc-r) / (maxc-minc)
132    gc = (maxc-g) / (maxc-minc)
133    bc = (maxc-b) / (maxc-minc)
134    if r == maxc:
135        h = bc-gc
136    elif g == maxc:
137        h = 2.0+rc-bc
138    else:
139        h = 4.0+gc-rc
140    h = (h/6.0) % 1.0
141    return h, s, v
142
143def hsv_to_rgb(h, s, v):
144    if s == 0.0:
145        return v, v, v
146    i = int(h*6.0) # XXX assume int() truncates!
147    f = (h*6.0) - i
148    p = v*(1.0 - s)
149    q = v*(1.0 - s*f)
150    t = v*(1.0 - s*(1.0-f))
151    i = i%6
152    if i == 0:
153        return v, t, p
154    if i == 1:
155        return q, v, p
156    if i == 2:
157        return p, v, t
158    if i == 3:
159        return p, q, v
160    if i == 4:
161        return t, p, v
162    if i == 5:
163        return v, p, q
164    # Cannot get here
165