ยปCore Development>Code coverage>Lib/colorsys.py

Python code coverage for Lib/colorsys.py

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