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