Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9b855de

Browse files
Issue #14323: Expanded the number of digits in the coefficients for the
RGB -- YIQ conversions so that they match the FCC NTSC versions.
1 parent 536f9fd commit 9b855de

4 files changed

Lines changed: 51 additions & 9 deletions

File tree

Doc/whatsnew/3.4.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,13 @@ platform-dependent. (Contributed by Christian Heimes in :issue:`11016`.)
251251

252252
The module supports new file types: door, event port and whiteout.
253253

254+
colorsys
255+
--------
256+
257+
The number of digits in the coefficients for the RGB --- YIQ conversions have
258+
been expanded so that they match the FCC NTSC versions. The change in
259+
results should be less than 1% and may better match results found elsewhere.
260+
254261

255262
Optimizations
256263
=============

Lib/colorsys.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,25 @@
3333
# YIQ: used by composite video signals (linear combinations of RGB)
3434
# Y: perceived grey level (0.0 == black, 1.0 == white)
3535
# 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.
3639

3740
def rgb_to_yiq(r, g, b):
3841
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
42+
i = 0.74*(r-y) - 0.27*(b-y)
43+
q = 0.48*(r-y) + 0.41*(b-y)
4144
return (y, i, q)
4245

4346
def 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+
# 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+
4755
if r < 0.0:
4856
r = 0.0
4957
if g < 0.0:

Lib/test/test_colorsys.py

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import unittest, test.support
1+
import unittest
22
import colorsys
33

44
def frange(start, stop, step):
@@ -69,8 +69,32 @@ def test_hls_values(self):
6969
self.assertTripleEqual(hls, colorsys.rgb_to_hls(*rgb))
7070
self.assertTripleEqual(rgb, colorsys.hls_to_rgb(*hls))
7171

72-
def test_main():
73-
test.support.run_unittest(ColorsysTest)
72+
def test_yiq_roundtrip(self):
73+
for r in frange(0.0, 1.0, 0.2):
74+
for g in frange(0.0, 1.0, 0.2):
75+
for b in frange(0.0, 1.0, 0.2):
76+
rgb = (r, g, b)
77+
self.assertTripleEqual(
78+
rgb,
79+
colorsys.yiq_to_rgb(*colorsys.rgb_to_yiq(*rgb))
80+
)
81+
82+
def test_yiq_values(self):
83+
values = [
84+
# rgb, yiq
85+
((0.0, 0.0, 0.0), (0.0, 0.0, 0.0)), # black
86+
((0.0, 0.0, 1.0), (0.11, -0.3217, 0.3121)), # blue
87+
((0.0, 1.0, 0.0), (0.59, -0.2773, -0.5251)), # green
88+
((0.0, 1.0, 1.0), (0.7, -0.599, -0.213)), # cyan
89+
((1.0, 0.0, 0.0), (0.3, 0.599, 0.213)), # red
90+
((1.0, 0.0, 1.0), (0.41, 0.2773, 0.5251)), # purple
91+
((1.0, 1.0, 0.0), (0.89, 0.3217, -0.3121)), # yellow
92+
((1.0, 1.0, 1.0), (1.0, 0.0, 0.0)), # white
93+
((0.5, 0.5, 0.5), (0.5, 0.0, 0.0)), # grey
94+
]
95+
for (rgb, yiq) in values:
96+
self.assertTripleEqual(yiq, colorsys.rgb_to_yiq(*rgb))
97+
self.assertTripleEqual(rgb, colorsys.yiq_to_rgb(*yiq))
7498

7599
if __name__ == "__main__":
76-
test_main()
100+
unittest.main()

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,9 @@ Core and Builtins
202202
Library
203203
-------
204204

205+
- Issue #14323: Expanded the number of digits in the coefficients for the
206+
RGB -- YIQ conversions so that they match the FCC NTSC versions.
207+
205208
- Issue #17998: Fix an internal error in regular expression engine.
206209

207210
- Issue #17557: Fix os.getgroups() to work with the modified behavior of

0 commit comments

Comments
 (0)