cpython: 80e9cb6163b4 (original) (raw)
--- a/Doc/whatsnew/3.4.rst +++ b/Doc/whatsnew/3.4.rst @@ -251,6 +251,13 @@ platform-dependent. (Contributed by Chr The module supports new file types: door, event port and whiteout. +colorsys +-------- + +The number of digits in the coefficients for the RGB --- YIQ conversions have +been expanded so that they match the FCC NTSC versions. The change in +results should be less than 1% and may better match results found elsewhere. + Optimizations =============
--- a/Lib/colorsys.py +++ b/Lib/colorsys.py @@ -33,17 +33,25 @@ TWO_THIRD = 2.0/3.0
YIQ: used by composite video signals (linear combinations of RGB)
Y: perceived grey level (0.0 == black, 1.0 == white)
I, Q: color components
+# +# There are a great many versions of the constants used in these formulae. +# The ones in this library uses constants from the FCC version of NTSC. def rgb_to_yiq(r, g, b): y = 0.30r + 0.59g + 0.11*b
r = y + (0.27q + 0.41i) / (0.740.41 + 0.270.48)
b = y + (0.74q - 0.48i) / (0.740.41 + 0.270.48)
g = y - (0.30*(r-y) + 0.11*(b-y)) / 0.59
- r = y + 0.9468822170900693i + 0.6235565819861433q
- g = y - 0.27478764629897834i - 0.6356910791873801q
- b = y - 1.1085450346420322i + 1.7090069284064666q
+ if r < 0.0: r = 0.0 if g < 0.0:
--- a/Lib/test/test_colorsys.py +++ b/Lib/test/test_colorsys.py @@ -1,4 +1,4 @@ -import unittest, test.support +import unittest import colorsys def frange(start, stop, step): @@ -69,8 +69,32 @@ class ColorsysTest(unittest.TestCase): self.assertTripleEqual(hls, colorsys.rgb_to_hls(*rgb)) self.assertTripleEqual(rgb, colorsys.hls_to_rgb(*hls)) -def test_main():
- def test_yiq_roundtrip(self):
for r in frange(0.0, 1.0, 0.2):[](#l3.16)
for g in frange(0.0, 1.0, 0.2):[](#l3.17)
for b in frange(0.0, 1.0, 0.2):[](#l3.18)
rgb = (r, g, b)[](#l3.19)
self.assertTripleEqual([](#l3.20)
rgb,[](#l3.21)
colorsys.yiq_to_rgb(*colorsys.rgb_to_yiq(*rgb))[](#l3.22)
)[](#l3.23)
- def test_yiq_values(self):
values = [[](#l3.26)
# rgb, yiq[](#l3.27)
((0.0, 0.0, 0.0), (0.0, 0.0, 0.0)), # black[](#l3.28)
((0.0, 0.0, 1.0), (0.11, -0.3217, 0.3121)), # blue[](#l3.29)
((0.0, 1.0, 0.0), (0.59, -0.2773, -0.5251)), # green[](#l3.30)
((0.0, 1.0, 1.0), (0.7, -0.599, -0.213)), # cyan[](#l3.31)
((1.0, 0.0, 0.0), (0.3, 0.599, 0.213)), # red[](#l3.32)
((1.0, 0.0, 1.0), (0.41, 0.2773, 0.5251)), # purple[](#l3.33)
((1.0, 1.0, 0.0), (0.89, 0.3217, -0.3121)), # yellow[](#l3.34)
((1.0, 1.0, 1.0), (1.0, 0.0, 0.0)), # white[](#l3.35)
((0.5, 0.5, 0.5), (0.5, 0.0, 0.0)), # grey[](#l3.36)
][](#l3.37)
for (rgb, yiq) in values:[](#l3.38)
self.assertTripleEqual(yiq, colorsys.rgb_to_yiq(*rgb))[](#l3.39)
self.assertTripleEqual(rgb, colorsys.yiq_to_rgb(*yiq))[](#l3.40)
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -202,6 +202,9 @@ Core and Builtins Library ------- +- Issue #14323: Expanded the number of digits in the coefficients for the