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