blob: 851417b5d5177b5d8ac894c82a9889b3b2bfaca1 [file] [log] [blame]
Fred Drake87209171999-02-25 14:24:22 +00001"""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
Fred Drakebff3ae11999-02-25 14:26:02 +00006 abc_to_rgb(a, b, c) --> r, g, b
Fred Drake87209171999-02-25 14:24:22 +00007
Fredrik Lundh0d89e352005-11-12 15:21:05 +00008All 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.
Fred Drake87209171999-02-25 14:24:22 +000011
12Supported color systems:
13RGB: Red, Green, Blue components
Fredrik Lundh0d89e352005-11-12 15:21:05 +000014YIQ: Luminance, Chrominance (used by composite video signals)
Fred Drakeb2176872000-02-14 21:30:52 +000015HLS: Hue, Luminance, Saturation
16HSV: Hue, Saturation, Value
Fred Drake87209171999-02-25 14:24:22 +000017"""
Fredrik Lundh0d89e352005-11-12 15:21:05 +000018
Guido van Rossum87b74731992-09-07 09:41:48 +000019# References:
Fredrik Lundh0d89e352005-11-12 15:21:05 +000020# 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 Rossum87b74731992-09-07 09:41:48 +000023
Skip Montanaroe99d5ea2001-01-20 19:54:20 +000024__all__ = ["rgb_to_yiq","yiq_to_rgb","rgb_to_hls","hls_to_rgb",
25 "rgb_to_hsv","hsv_to_rgb"]
Guido van Rossum87b74731992-09-07 09:41:48 +000026
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
Guido van Rossum87b74731992-09-07 09:41:48 +000033# 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
37def 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
43def 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
47 if r < 0.0: r = 0.0
48 if g < 0.0: g = 0.0
49 if b < 0.0: b = 0.0
50 if r > 1.0: r = 1.0
51 if g > 1.0: g = 1.0
52 if b > 1.0: b = 1.0
53 return (r, g, b)
54
55
Fredrik Lundh0d89e352005-11-12 15:21:05 +000056# HLS: Hue, Luminance, Saturation
Guido van Rossum87b74731992-09-07 09:41:48 +000057# H: position in the spectrum
Fredrik Lundh0d89e352005-11-12 15:21:05 +000058# L: color lightness
59# S: color saturation
Guido van Rossum87b74731992-09-07 09:41:48 +000060
61def rgb_to_hls(r, g, b):
62 maxc = max(r, g, b)
63 minc = min(r, g, b)
64 # XXX Can optimize (maxc+minc) and (maxc-minc)
65 l = (minc+maxc)/2.0
66 if minc == maxc: return 0.0, l, 0.0
67 if l <= 0.5: s = (maxc-minc) / (maxc+minc)
68 else: s = (maxc-minc) / (2.0-maxc-minc)
69 rc = (maxc-r) / (maxc-minc)
70 gc = (maxc-g) / (maxc-minc)
71 bc = (maxc-b) / (maxc-minc)
72 if r == maxc: h = bc-gc
73 elif g == maxc: h = 2.0+rc-bc
74 else: h = 4.0+gc-rc
75 h = (h/6.0) % 1.0
76 return h, l, s
77
78def hls_to_rgb(h, l, s):
79 if s == 0.0: return l, l, l
80 if l <= 0.5: m2 = l * (1.0+s)
81 else: m2 = l+s-(l*s)
82 m1 = 2.0*l - m2
83 return (_v(m1, m2, h+ONE_THIRD), _v(m1, m2, h), _v(m1, m2, h-ONE_THIRD))
84
85def _v(m1, m2, hue):
86 hue = hue % 1.0
87 if hue < ONE_SIXTH: return m1 + (m2-m1)*hue*6.0
88 if hue < 0.5: return m2
89 if hue < TWO_THIRD: return m1 + (m2-m1)*(TWO_THIRD-hue)*6.0
90 return m1
91
92
Fredrik Lundh0d89e352005-11-12 15:21:05 +000093# HSV: Hue, Saturation, Value
Guido van Rossum87b74731992-09-07 09:41:48 +000094# H: position in the spectrum
Fredrik Lundh0d89e352005-11-12 15:21:05 +000095# S: color saturation ("purity")
96# V: color brightness
Guido van Rossum87b74731992-09-07 09:41:48 +000097
98def rgb_to_hsv(r, g, b):
99 maxc = max(r, g, b)
100 minc = min(r, g, b)
101 v = maxc
102 if minc == maxc: return 0.0, 0.0, v
103 s = (maxc-minc) / maxc
104 rc = (maxc-r) / (maxc-minc)
105 gc = (maxc-g) / (maxc-minc)
106 bc = (maxc-b) / (maxc-minc)
107 if r == maxc: h = bc-gc
108 elif g == maxc: h = 2.0+rc-bc
109 else: h = 4.0+gc-rc
110 h = (h/6.0) % 1.0
111 return h, s, v
112
113def hsv_to_rgb(h, s, v):
114 if s == 0.0: return v, v, v
115 i = int(h*6.0) # XXX assume int() truncates!
116 f = (h*6.0) - i
117 p = v*(1.0 - s)
118 q = v*(1.0 - s*f)
119 t = v*(1.0 - s*(1.0-f))
Thomas Wouters89f507f2006-12-13 04:49:30 +0000120 i = i%6
121 if i == 0: return v, t, p
Guido van Rossum87b74731992-09-07 09:41:48 +0000122 if i == 1: return q, v, p
123 if i == 2: return p, v, t
124 if i == 3: return p, q, v
125 if i == 4: return t, p, v
126 if i == 5: return v, p, q
127 # Cannot get here