[18455] | 1 | #!/usr/bin/env python |
---|
[18446] | 2 | # -*- coding: utf-8 -*- |
---|
| 3 | """Conversion functions between RGB and other color systems. |
---|
[18445] | 4 | |
---|
| 5 | This modules provides two functions for each color system ABC: |
---|
| 6 | |
---|
[18454] | 7 | rgb_to_abc(r, g, b) --> a, b, c |
---|
| 8 | abc_to_rgb(a, b, c) --> r, g, b |
---|
[18445] | 9 | |
---|
| 10 | All inputs and outputs are triples of floats in the range [0.0...1.0] |
---|
| 11 | (with the exception of I and Q, which covers a slightly larger range). |
---|
| 12 | Inputs outside the valid range may cause exceptions or invalid outputs. |
---|
| 13 | |
---|
| 14 | Supported color systems: |
---|
| 15 | RGB: Red, Green, Blue components |
---|
| 16 | YIQ: Luminance, Chrominance (used by composite video signals) |
---|
| 17 | HLS: Hue, Luminance, Saturation |
---|
| 18 | HSV: Hue, Saturation, Value |
---|
| 19 | """ |
---|
| 20 | |
---|
| 21 | # References: |
---|
| 22 | # http://en.wikipedia.org/wiki/YIQ |
---|
| 23 | # http://en.wikipedia.org/wiki/HLS_color_space |
---|
| 24 | # http://en.wikipedia.org/wiki/HSV_color_space |
---|
| 25 | |
---|
| 26 | __all__ = ["rgb_to_yiq","yiq_to_rgb","rgb_to_hls","hls_to_rgb", |
---|
[18454] | 27 | "rgb_to_hsv","hsv_to_rgb"] |
---|
[18445] | 28 | |
---|
| 29 | # Some floating point constants |
---|
| 30 | |
---|
| 31 | ONE_THIRD = 1.0/3.0 |
---|
| 32 | ONE_SIXTH = 1.0/6.0 |
---|
| 33 | TWO_THIRD = 2.0/3.0 |
---|
| 34 | |
---|
| 35 | # YIQ: used by composite video signals (linear combinations of RGB) |
---|
| 36 | # Y: perceived grey level (0.0 == black, 1.0 == white) |
---|
| 37 | # I, Q: color components |
---|
| 38 | |
---|
| 39 | def rgb_to_yiq(r, g, b): |
---|
[18448] | 40 | y = 0.30*r + 0.59*g + 0.11*b |
---|
| 41 | i = 0.60*r - 0.28*g - 0.32*b |
---|
| 42 | q = 0.21*r - 0.52*g + 0.31*b |
---|
| 43 | return (y, i, q) |
---|
[18445] | 44 | |
---|
| 45 | def yiq_to_rgb(y, i, q): |
---|
[18448] | 46 | r = y + 0.948262*i + 0.624013*q |
---|
| 47 | g = y - 0.276066*i - 0.639810*q |
---|
| 48 | b = y - 1.105450*i + 1.729860*q |
---|
| 49 | if r < 0.0: r = 0.0 |
---|
| 50 | if g < 0.0: g = 0.0 |
---|
| 51 | if b < 0.0: b = 0.0 |
---|
| 52 | if r > 1.0: r = 1.0 |
---|
| 53 | if g > 1.0: g = 1.0 |
---|
| 54 | if b > 1.0: b = 1.0 |
---|
| 55 | return (r, g, b) |
---|
[18445] | 56 | |
---|
| 57 | |
---|
| 58 | # HLS: Hue, Luminance, Saturation |
---|
| 59 | # H: position in the spectrum |
---|
| 60 | # L: color lightness |
---|
| 61 | # S: color saturation |
---|
| 62 | |
---|
| 63 | def rgb_to_hls(r, g, b): |
---|
[18448] | 64 | maxc = max(r, g, b) |
---|
| 65 | minc = min(r, g, b) |
---|
| 66 | # XXX Can optimize (maxc+minc) and (maxc-minc) |
---|
| 67 | l = (minc+maxc)/2.0 |
---|
| 68 | if minc == maxc: return 0.0, l, 0.0 |
---|
| 69 | if l <= 0.5: s = (maxc-minc) / (maxc+minc) |
---|
| 70 | else: s = (maxc-minc) / (2.0-maxc-minc) |
---|
| 71 | rc = (maxc-r) / (maxc-minc) |
---|
| 72 | gc = (maxc-g) / (maxc-minc) |
---|
| 73 | bc = (maxc-b) / (maxc-minc) |
---|
| 74 | if r == maxc: h = bc-gc |
---|
| 75 | elif g == maxc: h = 2.0+rc-bc |
---|
| 76 | else: h = 4.0+gc-rc |
---|
| 77 | h = (h/6.0) % 1.0 |
---|
| 78 | return h, l, s |
---|
[18445] | 79 | |
---|
| 80 | def hls_to_rgb(h, l, s): |
---|
[18448] | 81 | if s == 0.0: return l, l, l |
---|
| 82 | if l <= 0.5: m2 = l * (1.0+s) |
---|
| 83 | else: m2 = l+s-(l*s) |
---|
| 84 | m1 = 2.0*l - m2 |
---|
| 85 | return (_v(m1, m2, h+ONE_THIRD), _v(m1, m2, h), _v(m1, m2, h-ONE_THIRD)) |
---|
[18445] | 86 | |
---|
| 87 | def _v(m1, m2, hue): |
---|
[18448] | 88 | hue = hue % 1.0 |
---|
| 89 | if hue < ONE_SIXTH: return m1 + (m2-m1)*hue*6.0 |
---|
| 90 | if hue < 0.5: return m2 |
---|
| 91 | if hue < TWO_THIRD: return m1 + (m2-m1)*(TWO_THIRD-hue)*6.0 |
---|
| 92 | return m1 |
---|
[18445] | 93 | |
---|
| 94 | |
---|
| 95 | # HSV: Hue, Saturation, Value |
---|
| 96 | # H: position in the spectrum |
---|
| 97 | # S: color saturation ("purity") |
---|
| 98 | # V: color brightness |
---|
| 99 | |
---|
| 100 | def rgb_to_hsv(r, g, b): |
---|
[18448] | 101 | maxc = max(r, g, b) |
---|
| 102 | minc = min(r, g, b) |
---|
| 103 | v = maxc |
---|
| 104 | if minc == maxc: return 0.0, 0.0, v |
---|
| 105 | s = (maxc-minc) / maxc |
---|
| 106 | rc = (maxc-r) / (maxc-minc) |
---|
| 107 | gc = (maxc-g) / (maxc-minc) |
---|
| 108 | bc = (maxc-b) / (maxc-minc) |
---|
| 109 | if r == maxc: h = bc-gc |
---|
| 110 | elif g == maxc: h = 2.0+rc-bc |
---|
| 111 | else: h = 4.0+gc-rc |
---|
| 112 | h = (h/6.0) % 1.0 |
---|
| 113 | return h, s, v |
---|
[18445] | 114 | |
---|
| 115 | def hsv_to_rgb(h, s, v): |
---|
[18448] | 116 | if s == 0.0: return v, v, v |
---|
| 117 | i = int(h*6.0) # XXX assume int() truncates! |
---|
| 118 | f = (h*6.0) - i |
---|
| 119 | p = v*(1.0 - s) |
---|
| 120 | q = v*(1.0 - s*f) |
---|
| 121 | t = v*(1.0 - s*(1.0-f)) |
---|
| 122 | if i%6 == 0: return v, t, p |
---|
| 123 | if i == 1: return q, v, p |
---|
| 124 | if i == 2: return p, v, t |
---|
| 125 | if i == 3: return p, q, v |
---|
| 126 | if i == 4: return t, p, v |
---|
| 127 | if i == 5: return v, p, q |
---|
| 128 | # Cannot get here |
---|