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 | |
| 8 | All inputs and outputs are triples of floats in the range [0.0...1.0]. |
| 9 | Inputs outside this range may cause exceptions or invalid outputs. |
| 10 | |
| 11 | Supported color systems: |
| 12 | RGB: Red, Green, Blue components |
| 13 | YIQ: used by composite video signals |
Fred Drake | b217687 | 2000-02-14 21:30:52 +0000 | [diff] [blame] | 14 | HLS: Hue, Luminance, Saturation |
| 15 | HSV: Hue, Saturation, Value |
Fred Drake | 8720917 | 1999-02-25 14:24:22 +0000 | [diff] [blame] | 16 | """ |
Guido van Rossum | 87b7473 | 1992-09-07 09:41:48 +0000 | [diff] [blame] | 17 | # References: |
| 18 | # XXX Where's the literature? |
| 19 | |
Skip Montanaro | e99d5ea | 2001-01-20 19:54:20 +0000 | [diff] [blame^] | 20 | __all__ = ["rgb_to_yiq","yiq_to_rgb","rgb_to_hls","hls_to_rgb", |
| 21 | "rgb_to_hsv","hsv_to_rgb"] |
Guido van Rossum | 87b7473 | 1992-09-07 09:41:48 +0000 | [diff] [blame] | 22 | |
| 23 | # Some floating point constants |
| 24 | |
| 25 | ONE_THIRD = 1.0/3.0 |
| 26 | ONE_SIXTH = 1.0/6.0 |
| 27 | TWO_THIRD = 2.0/3.0 |
| 28 | |
| 29 | |
| 30 | # YIQ: used by composite video signals (linear combinations of RGB) |
| 31 | # Y: perceived grey level (0.0 == black, 1.0 == white) |
| 32 | # I, Q: color components |
| 33 | |
| 34 | def rgb_to_yiq(r, g, b): |
| 35 | y = 0.30*r + 0.59*g + 0.11*b |
| 36 | i = 0.60*r - 0.28*g - 0.32*b |
| 37 | q = 0.21*r - 0.52*g + 0.31*b |
| 38 | return (y, i, q) |
| 39 | |
| 40 | def yiq_to_rgb(y, i, q): |
| 41 | r = y + 0.948262*i + 0.624013*q |
| 42 | g = y - 0.276066*i - 0.639810*q |
| 43 | b = y - 1.105450*i + 1.729860*q |
| 44 | if r < 0.0: r = 0.0 |
| 45 | if g < 0.0: g = 0.0 |
| 46 | if b < 0.0: b = 0.0 |
| 47 | if r > 1.0: r = 1.0 |
| 48 | if g > 1.0: g = 1.0 |
| 49 | if b > 1.0: b = 1.0 |
| 50 | return (r, g, b) |
| 51 | |
| 52 | |
| 53 | # HLS: Hue, Luminance, S??? |
| 54 | # H: position in the spectrum |
| 55 | # L: ??? |
| 56 | # S: ??? |
| 57 | |
| 58 | def rgb_to_hls(r, g, b): |
| 59 | maxc = max(r, g, b) |
| 60 | minc = min(r, g, b) |
| 61 | # XXX Can optimize (maxc+minc) and (maxc-minc) |
| 62 | l = (minc+maxc)/2.0 |
| 63 | if minc == maxc: return 0.0, l, 0.0 |
| 64 | if l <= 0.5: s = (maxc-minc) / (maxc+minc) |
| 65 | else: s = (maxc-minc) / (2.0-maxc-minc) |
| 66 | rc = (maxc-r) / (maxc-minc) |
| 67 | gc = (maxc-g) / (maxc-minc) |
| 68 | bc = (maxc-b) / (maxc-minc) |
| 69 | if r == maxc: h = bc-gc |
| 70 | elif g == maxc: h = 2.0+rc-bc |
| 71 | else: h = 4.0+gc-rc |
| 72 | h = (h/6.0) % 1.0 |
| 73 | return h, l, s |
| 74 | |
| 75 | def hls_to_rgb(h, l, s): |
| 76 | if s == 0.0: return l, l, l |
| 77 | if l <= 0.5: m2 = l * (1.0+s) |
| 78 | else: m2 = l+s-(l*s) |
| 79 | m1 = 2.0*l - m2 |
| 80 | return (_v(m1, m2, h+ONE_THIRD), _v(m1, m2, h), _v(m1, m2, h-ONE_THIRD)) |
| 81 | |
| 82 | def _v(m1, m2, hue): |
| 83 | hue = hue % 1.0 |
| 84 | if hue < ONE_SIXTH: return m1 + (m2-m1)*hue*6.0 |
| 85 | if hue < 0.5: return m2 |
| 86 | if hue < TWO_THIRD: return m1 + (m2-m1)*(TWO_THIRD-hue)*6.0 |
| 87 | return m1 |
| 88 | |
| 89 | |
| 90 | # HSV: Hue, Saturation, Value(?) |
| 91 | # H: position in the spectrum |
| 92 | # S: ??? |
| 93 | # V: ??? |
| 94 | |
| 95 | def rgb_to_hsv(r, g, b): |
| 96 | maxc = max(r, g, b) |
| 97 | minc = min(r, g, b) |
| 98 | v = maxc |
| 99 | if minc == maxc: return 0.0, 0.0, v |
| 100 | s = (maxc-minc) / maxc |
| 101 | rc = (maxc-r) / (maxc-minc) |
| 102 | gc = (maxc-g) / (maxc-minc) |
| 103 | bc = (maxc-b) / (maxc-minc) |
| 104 | if r == maxc: h = bc-gc |
| 105 | elif g == maxc: h = 2.0+rc-bc |
| 106 | else: h = 4.0+gc-rc |
| 107 | h = (h/6.0) % 1.0 |
| 108 | return h, s, v |
| 109 | |
| 110 | def hsv_to_rgb(h, s, v): |
| 111 | if s == 0.0: return v, v, v |
| 112 | i = int(h*6.0) # XXX assume int() truncates! |
| 113 | f = (h*6.0) - i |
| 114 | p = v*(1.0 - s) |
| 115 | q = v*(1.0 - s*f) |
| 116 | t = v*(1.0 - s*(1.0-f)) |
| 117 | if i%6 == 0: return v, t, p |
| 118 | if i == 1: return q, v, p |
| 119 | if i == 2: return p, v, t |
| 120 | if i == 3: return p, q, v |
| 121 | if i == 4: return t, p, v |
| 122 | if i == 5: return v, p, q |
| 123 | # Cannot get here |