1 """Conversion functions between RGB and other color systems.
2
3 This 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
8 All 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).
10 Inputs outside the valid range may cause exceptions or invalid outputs.
11
12 Supported color systems:
13 RGB: Red, Green, Blue components
14 YIQ: Luminance, Chrominance (used by composite video signals)
15 HLS: Hue, Luminance, Saturation
16 HSV: 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
29 ONE_THIRD = 1.0/3.0
30 ONE_SIXTH = 1.0/6.0
31 TWO_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
40 def 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
46 def 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
75 def rgb_to_hls(r, g, b):
76 maxc = max(r, g, b)
77 minc = min(r, g, b)
78 sumc = (maxc+minc)
79 rangec = (maxc-minc)
80 l = sumc/2.0
81 if minc == maxc:
82 return 0.0, l, 0.0
83 if l <= 0.5:
84 s = rangec / sumc
85 else:
86 s = rangec / (2.0-maxc-minc) # Not always 2.0-sumc: gh-106498.
87 rc = (maxc-r) / rangec
88 gc = (maxc-g) / rangec
89 bc = (maxc-b) / rangec
90 if r == maxc:
91 h = bc-gc
92 elif g == maxc:
93 h = 2.0+rc-bc
94 else:
95 h = 4.0+gc-rc
96 h = (h/6.0) % 1.0
97 return h, l, s
98
99 def hls_to_rgb(h, l, s):
100 if s == 0.0:
101 return l, l, l
102 if l <= 0.5:
103 m2 = l * (1.0+s)
104 else:
105 m2 = l+s-(l*s)
106 m1 = 2.0*l - m2
107 return (_v(m1, m2, h+ONE_THIRD), _v(m1, m2, h), _v(m1, m2, h-ONE_THIRD))
108
109 def _v(m1, m2, hue):
110 hue = hue % 1.0
111 if hue < ONE_SIXTH:
112 return m1 + (m2-m1)*hue*6.0
113 if hue < 0.5:
114 return m2
115 if hue < TWO_THIRD:
116 return m1 + (m2-m1)*(TWO_THIRD-hue)*6.0
117 return m1
118
119
120 # HSV: Hue, Saturation, Value
121 # H: position in the spectrum
122 # S: color saturation ("purity")
123 # V: color brightness
124
125 def rgb_to_hsv(r, g, b):
126 maxc = max(r, g, b)
127 minc = min(r, g, b)
128 rangec = (maxc-minc)
129 v = maxc
130 if minc == maxc:
131 return 0.0, 0.0, v
132 s = rangec / maxc
133 rc = (maxc-r) / rangec
134 gc = (maxc-g) / rangec
135 bc = (maxc-b) / rangec
136 if r == maxc:
137 h = bc-gc
138 elif g == maxc:
139 h = 2.0+rc-bc
140 else:
141 h = 4.0+gc-rc
142 h = (h/6.0) % 1.0
143 return h, s, v
144
145 def hsv_to_rgb(h, s, v):
146 if s == 0.0:
147 return v, v, v
148 i = int(h*6.0) # XXX assume int() truncates!
149 f = (h*6.0) - i
150 p = v*(1.0 - s)
151 q = v*(1.0 - s*f)
152 t = v*(1.0 - s*(1.0-f))
153 i = i%6
154 if i == 0:
155 return v, t, p
156 if i == 1:
157 return q, v, p
158 if i == 2:
159 return p, v, t
160 if i == 3:
161 return p, q, v
162 if i == 4:
163 return t, p, v
164 if i == 5:
165 return v, p, q
166 # Cannot get here