|
| 1 | +# Conversions between RGB and YIQ color spaces. |
1 | 2 | # |
2 | | -# Module color - do color conversions |
3 | | -# |
| 3 | +# R, G, B are amounts of Red, Green and Blue; |
| 4 | +# Y, I, Q are intensity (Y) and two difference signals (I, Q). |
| 5 | + |
| 6 | + |
| 7 | +# Convert a (R, G, B) triple to (Y, I, Q). |
| 8 | +# Scale is arbitrary (output uses the same scale as input). |
| 9 | + |
| 10 | +def rgb_to_yiq(r, g, b): |
| 11 | + y = 0.30*r + 0.59*g + 0.11*b |
| 12 | + i = 0.60*r - 0.28*g - 0.32*b |
| 13 | + q = 0.21*r - 0.52*g + 0.31*b |
| 14 | + return y, i, q |
| 15 | + |
| 16 | + |
| 17 | +# Convert a (Y, I, Q) triple to (R, G, B). |
| 18 | +# Input and output values are in the interval [0.0 ... 1.0]. |
4 | 19 |
|
5 | | -def rgb_to_yiq(r,g,b): |
6 | | - y = 0.30*r + 0.59*g + 0.11*b |
7 | | - i = 0.60*r - 0.28*g - 0.32*b |
8 | | - q = 0.21*r - 0.52*g + 0.31*b |
9 | | - return (y,i,q) |
10 | | -def yiq_to_rgb(y,i,q): |
11 | | - r = y + 0.948262*i + 0.624013*q |
12 | | - g = y - 0.276066*i - 0.639810*q |
13 | | - b = y - 1.105450*i + 1.729860*q |
14 | | - if r < 0.0: r = 0.0 |
15 | | - if g < 0.0: g = 0.0 |
16 | | - if b < 0.0: b = 0.0 |
17 | | - if r > 1.0: r = 1.0 |
18 | | - if g > 1.0: g = 1.0 |
19 | | - if b > 1.0: b = 1.0 |
20 | | - return (r,g,b) |
| 20 | +def yiq_to_rgb(y, i, q): |
| 21 | + r = y + 0.948262*i + 0.624013*q |
| 22 | + g = y - 0.276066*i - 0.639810*q |
| 23 | + b = y - 1.105450*i + 1.729860*q |
| 24 | + if r < 0.0: r = 0.0 |
| 25 | + if g < 0.0: g = 0.0 |
| 26 | + if b < 0.0: b = 0.0 |
| 27 | + if r > 1.0: r = 1.0 |
| 28 | + if g > 1.0: g = 1.0 |
| 29 | + if b > 1.0: b = 1.0 |
| 30 | + return r, g, b |
0 commit comments