blob: b93e3844067e4e55e0cc227b7f2f05e013724ab0 [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
Serhiy Storchaka9b855de2013-08-06 11:51:23 +030036#
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.
Guido van Rossum87b74731992-09-07 09:41:48 +000039
40def rgb_to_yiq(r, g, b):
41 y = 0.30*r + 0.59*g + 0.11*b
Serhiy Storchaka9b855de2013-08-06 11:51:23 +030042 i = 0.74*(r-y) - 0.27*(b-y)
43 q = 0.48*(r-y) + 0.41*(b-y)
Guido van Rossum87b74731992-09-07 09:41:48 +000044 return (y, i, q)
45
46def yiq_to_rgb(y, i, q):
Serhiy Storchaka9b855de2013-08-06 11:51:23 +030047 # 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
Benjamin Petersonc4bbc8d2009-01-30 03:39:35 +000055 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
Guido van Rossum87b74731992-09-07 09:41:48 +000067 return (r, g, b)
68
69
Fredrik Lundh0d89e352005-11-12 15:21:05 +000070# HLS: Hue, Luminance, Saturation
Guido van Rossum87b74731992-09-07 09:41:48 +000071# H: position in the spectrum
Fredrik Lundh0d89e352005-11-12 15:21:05 +000072# L: color lightness
73# S: color saturation
Guido van Rossum87b74731992-09-07 09:41:48 +000074
75def rgb_to_hls(r, g, b):
76 maxc = max(r, g, b)
77 minc = min(r, g, b)
78 # XXX Can optimize (maxc+minc) and (maxc-minc)
79 l = (minc+maxc)/2.0
Benjamin Petersonc4bbc8d2009-01-30 03:39:35 +000080 if minc == maxc:
81 return 0.0, l, 0.0
82 if l <= 0.5:
83 s = (maxc-minc) / (maxc+minc)
84 else:
85 s = (maxc-minc) / (2.0-maxc-minc)
Guido van Rossum87b74731992-09-07 09:41:48 +000086 rc = (maxc-r) / (maxc-minc)
87 gc = (maxc-g) / (maxc-minc)
88 bc = (maxc-b) / (maxc-minc)
Benjamin Petersonc4bbc8d2009-01-30 03:39:35 +000089 if r == maxc:
90 h = bc-gc
91 elif g == maxc:
92 h = 2.0+rc-bc
93 else:
94 h = 4.0+gc-rc
Guido van Rossum87b74731992-09-07 09:41:48 +000095 h = (h/6.0) % 1.0
96 return h, l, s
97
98def hls_to_rgb(h, l, s):
Benjamin Petersonc4bbc8d2009-01-30 03:39:35 +000099 if s == 0.0:
100 return l, l, l
101 if l <= 0.5:
102 m2 = l * (1.0+s)
103 else:
104 m2 = l+s-(l*s)
Guido van Rossum87b74731992-09-07 09:41:48 +0000105 m1 = 2.0*l - m2
106 return (_v(m1, m2, h+ONE_THIRD), _v(m1, m2, h), _v(m1, m2, h-ONE_THIRD))
107
108def _v(m1, m2, hue):
109 hue = hue % 1.0
Benjamin Petersonc4bbc8d2009-01-30 03:39:35 +0000110 if hue < ONE_SIXTH:
111 return m1 + (m2-m1)*hue*6.0
112 if hue < 0.5:
113 return m2
114 if hue < TWO_THIRD:
115 return m1 + (m2-m1)*(TWO_THIRD-hue)*6.0
Guido van Rossum87b74731992-09-07 09:41:48 +0000116 return m1
117
118
Fredrik Lundh0d89e352005-11-12 15:21:05 +0000119# HSV: Hue, Saturation, Value
Guido van Rossum87b74731992-09-07 09:41:48 +0000120# H: position in the spectrum
Fredrik Lundh0d89e352005-11-12 15:21:05 +0000121# S: color saturation ("purity")
122# V: color brightness
Guido van Rossum87b74731992-09-07 09:41:48 +0000123
124def rgb_to_hsv(r, g, b):
125 maxc = max(r, g, b)
126 minc = min(r, g, b)
127 v = maxc
Benjamin Petersonc4bbc8d2009-01-30 03:39:35 +0000128 if minc == maxc:
129 return 0.0, 0.0, v
Guido van Rossum87b74731992-09-07 09:41:48 +0000130 s = (maxc-minc) / maxc
131 rc = (maxc-r) / (maxc-minc)
132 gc = (maxc-g) / (maxc-minc)
133 bc = (maxc-b) / (maxc-minc)
Benjamin Petersonc4bbc8d2009-01-30 03:39:35 +0000134 if r == maxc:
135 h = bc-gc
136 elif g == maxc:
137 h = 2.0+rc-bc
138 else:
139 h = 4.0+gc-rc
Guido van Rossum87b74731992-09-07 09:41:48 +0000140 h = (h/6.0) % 1.0
141 return h, s, v
142
143def hsv_to_rgb(h, s, v):
Benjamin Petersonc4bbc8d2009-01-30 03:39:35 +0000144 if s == 0.0:
145 return v, v, v
Guido van Rossum87b74731992-09-07 09:41:48 +0000146 i = int(h*6.0) # XXX assume int() truncates!
147 f = (h*6.0) - i
148 p = v*(1.0 - s)
149 q = v*(1.0 - s*f)
150 t = v*(1.0 - s*(1.0-f))
Thomas Wouters89f507f2006-12-13 04:49:30 +0000151 i = i%6
Benjamin Petersonc4bbc8d2009-01-30 03:39:35 +0000152 if i == 0:
153 return v, t, p
154 if i == 1:
155 return q, v, p
156 if i == 2:
157 return p, v, t
158 if i == 3:
159 return p, q, v
160 if i == 4:
161 return t, p, v
162 if i == 5:
163 return v, p, q
Guido van Rossum87b74731992-09-07 09:41:48 +0000164 # Cannot get here