Fred Drake | 8720917 | 1999-02-25 14:24:22 +0000 | [diff] [blame] | 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 |
Fred Drake | bff3ae1 | 1999-02-25 14:26:02 +0000 | [diff] [blame] | 6 | abc_to_rgb(a, b, c) --> r, g, b |
Fred Drake | 8720917 | 1999-02-25 14:24:22 +0000 | [diff] [blame] | 7 | |
Fredrik Lundh | 0d89e35 | 2005-11-12 15:21:05 +0000 | [diff] [blame] | 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. |
Fred Drake | 8720917 | 1999-02-25 14:24:22 +0000 | [diff] [blame] | 11 | |
| 12 | Supported color systems: |
| 13 | RGB: Red, Green, Blue components |
Fredrik Lundh | 0d89e35 | 2005-11-12 15:21:05 +0000 | [diff] [blame] | 14 | YIQ: Luminance, Chrominance (used by composite video signals) |
Fred Drake | b217687 | 2000-02-14 21:30:52 +0000 | [diff] [blame] | 15 | HLS: Hue, Luminance, Saturation |
| 16 | HSV: Hue, Saturation, Value |
Fred Drake | 8720917 | 1999-02-25 14:24:22 +0000 | [diff] [blame] | 17 | """ |
Fredrik Lundh | 0d89e35 | 2005-11-12 15:21:05 +0000 | [diff] [blame] | 18 | |
Guido van Rossum | 87b7473 | 1992-09-07 09:41:48 +0000 | [diff] [blame] | 19 | # References: |
Fredrik Lundh | 0d89e35 | 2005-11-12 15:21:05 +0000 | [diff] [blame] | 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 |
Guido van Rossum | 87b7473 | 1992-09-07 09:41:48 +0000 | [diff] [blame] | 23 | |
Skip Montanaro | e99d5ea | 2001-01-20 19:54:20 +0000 | [diff] [blame] | 24 | __all__ = ["rgb_to_yiq","yiq_to_rgb","rgb_to_hls","hls_to_rgb", |
| 25 | "rgb_to_hsv","hsv_to_rgb"] |
Guido van Rossum | 87b7473 | 1992-09-07 09:41:48 +0000 | [diff] [blame] | 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 | |
Guido van Rossum | 87b7473 | 1992-09-07 09:41:48 +0000 | [diff] [blame] | 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 | def rgb_to_yiq(r, g, b): |
| 38 | y = 0.30*r + 0.59*g + 0.11*b |
| 39 | i = 0.60*r - 0.28*g - 0.32*b |
| 40 | q = 0.21*r - 0.52*g + 0.31*b |
| 41 | return (y, i, q) |
| 42 | |
| 43 | def yiq_to_rgb(y, i, q): |
| 44 | r = y + 0.948262*i + 0.624013*q |
| 45 | g = y - 0.276066*i - 0.639810*q |
| 46 | b = y - 1.105450*i + 1.729860*q |
Benjamin Peterson | c4bbc8d | 2009-01-30 03:39:35 +0000 | [diff] [blame^] | 47 | if r < 0.0: |
| 48 | r = 0.0 |
| 49 | if g < 0.0: |
| 50 | g = 0.0 |
| 51 | if b < 0.0: |
| 52 | b = 0.0 |
| 53 | if r > 1.0: |
| 54 | r = 1.0 |
| 55 | if g > 1.0: |
| 56 | g = 1.0 |
| 57 | if b > 1.0: |
| 58 | b = 1.0 |
Guido van Rossum | 87b7473 | 1992-09-07 09:41:48 +0000 | [diff] [blame] | 59 | return (r, g, b) |
| 60 | |
| 61 | |
Fredrik Lundh | 0d89e35 | 2005-11-12 15:21:05 +0000 | [diff] [blame] | 62 | # HLS: Hue, Luminance, Saturation |
Guido van Rossum | 87b7473 | 1992-09-07 09:41:48 +0000 | [diff] [blame] | 63 | # H: position in the spectrum |
Fredrik Lundh | 0d89e35 | 2005-11-12 15:21:05 +0000 | [diff] [blame] | 64 | # L: color lightness |
| 65 | # S: color saturation |
Guido van Rossum | 87b7473 | 1992-09-07 09:41:48 +0000 | [diff] [blame] | 66 | |
| 67 | def rgb_to_hls(r, g, b): |
| 68 | maxc = max(r, g, b) |
| 69 | minc = min(r, g, b) |
| 70 | # XXX Can optimize (maxc+minc) and (maxc-minc) |
| 71 | l = (minc+maxc)/2.0 |
Benjamin Peterson | c4bbc8d | 2009-01-30 03:39:35 +0000 | [diff] [blame^] | 72 | if minc == maxc: |
| 73 | return 0.0, l, 0.0 |
| 74 | if l <= 0.5: |
| 75 | s = (maxc-minc) / (maxc+minc) |
| 76 | else: |
| 77 | s = (maxc-minc) / (2.0-maxc-minc) |
Guido van Rossum | 87b7473 | 1992-09-07 09:41:48 +0000 | [diff] [blame] | 78 | rc = (maxc-r) / (maxc-minc) |
| 79 | gc = (maxc-g) / (maxc-minc) |
| 80 | bc = (maxc-b) / (maxc-minc) |
Benjamin Peterson | c4bbc8d | 2009-01-30 03:39:35 +0000 | [diff] [blame^] | 81 | if r == maxc: |
| 82 | h = bc-gc |
| 83 | elif g == maxc: |
| 84 | h = 2.0+rc-bc |
| 85 | else: |
| 86 | h = 4.0+gc-rc |
Guido van Rossum | 87b7473 | 1992-09-07 09:41:48 +0000 | [diff] [blame] | 87 | h = (h/6.0) % 1.0 |
| 88 | return h, l, s |
| 89 | |
| 90 | def hls_to_rgb(h, l, s): |
Benjamin Peterson | c4bbc8d | 2009-01-30 03:39:35 +0000 | [diff] [blame^] | 91 | if s == 0.0: |
| 92 | return l, l, l |
| 93 | if l <= 0.5: |
| 94 | m2 = l * (1.0+s) |
| 95 | else: |
| 96 | m2 = l+s-(l*s) |
Guido van Rossum | 87b7473 | 1992-09-07 09:41:48 +0000 | [diff] [blame] | 97 | m1 = 2.0*l - m2 |
| 98 | return (_v(m1, m2, h+ONE_THIRD), _v(m1, m2, h), _v(m1, m2, h-ONE_THIRD)) |
| 99 | |
| 100 | def _v(m1, m2, hue): |
| 101 | hue = hue % 1.0 |
Benjamin Peterson | c4bbc8d | 2009-01-30 03:39:35 +0000 | [diff] [blame^] | 102 | if hue < ONE_SIXTH: |
| 103 | return m1 + (m2-m1)*hue*6.0 |
| 104 | if hue < 0.5: |
| 105 | return m2 |
| 106 | if hue < TWO_THIRD: |
| 107 | return m1 + (m2-m1)*(TWO_THIRD-hue)*6.0 |
Guido van Rossum | 87b7473 | 1992-09-07 09:41:48 +0000 | [diff] [blame] | 108 | return m1 |
| 109 | |
| 110 | |
Fredrik Lundh | 0d89e35 | 2005-11-12 15:21:05 +0000 | [diff] [blame] | 111 | # HSV: Hue, Saturation, Value |
Guido van Rossum | 87b7473 | 1992-09-07 09:41:48 +0000 | [diff] [blame] | 112 | # H: position in the spectrum |
Fredrik Lundh | 0d89e35 | 2005-11-12 15:21:05 +0000 | [diff] [blame] | 113 | # S: color saturation ("purity") |
| 114 | # V: color brightness |
Guido van Rossum | 87b7473 | 1992-09-07 09:41:48 +0000 | [diff] [blame] | 115 | |
| 116 | def rgb_to_hsv(r, g, b): |
| 117 | maxc = max(r, g, b) |
| 118 | minc = min(r, g, b) |
| 119 | v = maxc |
Benjamin Peterson | c4bbc8d | 2009-01-30 03:39:35 +0000 | [diff] [blame^] | 120 | if minc == maxc: |
| 121 | return 0.0, 0.0, v |
Guido van Rossum | 87b7473 | 1992-09-07 09:41:48 +0000 | [diff] [blame] | 122 | s = (maxc-minc) / maxc |
| 123 | rc = (maxc-r) / (maxc-minc) |
| 124 | gc = (maxc-g) / (maxc-minc) |
| 125 | bc = (maxc-b) / (maxc-minc) |
Benjamin Peterson | c4bbc8d | 2009-01-30 03:39:35 +0000 | [diff] [blame^] | 126 | if r == maxc: |
| 127 | h = bc-gc |
| 128 | elif g == maxc: |
| 129 | h = 2.0+rc-bc |
| 130 | else: |
| 131 | h = 4.0+gc-rc |
Guido van Rossum | 87b7473 | 1992-09-07 09:41:48 +0000 | [diff] [blame] | 132 | h = (h/6.0) % 1.0 |
| 133 | return h, s, v |
| 134 | |
| 135 | def hsv_to_rgb(h, s, v): |
Benjamin Peterson | c4bbc8d | 2009-01-30 03:39:35 +0000 | [diff] [blame^] | 136 | if s == 0.0: |
| 137 | return v, v, v |
Guido van Rossum | 87b7473 | 1992-09-07 09:41:48 +0000 | [diff] [blame] | 138 | i = int(h*6.0) # XXX assume int() truncates! |
| 139 | f = (h*6.0) - i |
| 140 | p = v*(1.0 - s) |
| 141 | q = v*(1.0 - s*f) |
| 142 | t = v*(1.0 - s*(1.0-f)) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 143 | i = i%6 |
Benjamin Peterson | c4bbc8d | 2009-01-30 03:39:35 +0000 | [diff] [blame^] | 144 | if i == 0: |
| 145 | return v, t, p |
| 146 | if i == 1: |
| 147 | return q, v, p |
| 148 | if i == 2: |
| 149 | return p, v, t |
| 150 | if i == 3: |
| 151 | return p, q, v |
| 152 | if i == 4: |
| 153 | return t, p, v |
| 154 | if i == 5: |
| 155 | return v, p, q |
Guido van Rossum | 87b7473 | 1992-09-07 09:41:48 +0000 | [diff] [blame] | 156 | # Cannot get here |