This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Catastrophic loss of precision in colorsys module
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.9, Python 3.8, Python 3.7, Python 3.6, Python 3.5
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Zac Hatfield-Dodds, rhettinger, serhiy.storchaka
Priority: normal Keywords:

Created on 2020-05-18 09:10 by Zac Hatfield-Dodds, last changed 2022-04-11 14:59 by admin.

Messages (3)
msg369197 - (view) Author: Zac Hatfield-Dodds (Zac Hatfield-Dodds) * Date: 2020-05-18 09:10
As part of the Mentored Sprints at PyCon US, Marielle wrote some property-based tests [1] for the colorsys module [2], which found two bugs.

Taking a YIQ color, converting to RGB, and back to YIQ can result in the Y coordinate varying by more 0.1 (where [0, 1] is the range of possible values).
For example: (0.0 1.0 2.2204460492503136e-16) -> RGB -> (0.0 1.1102230246251568e-16 1.0)

Taking an RGB color and converting though HSV-RBG-HSV can result in very different saturation values - up to having S1==0 and S2==1.
For example: (0.0 1.0 2.2204460492503136e-16) -> RGB -> (0.0 1.1102230246251568e-16 1.0)

You can reproduce additional examples and get error bounds from [3].

msg369316 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2020-05-19 02:22
msg369355 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2020-05-19 14:25
It is correct. Colorspaces for RGB an YIQ are different. Not all RGB colors can be represented in YIQ and vice versa. For YIQ color (0, 1, 0) you need RGB color (0.9468822170900693, -0.27478764629897834, -1.1085450346420322), but the G and B components are out of the range [0, 1]. So yiq_to_rgb() returns the closes RGB color to the original code, and it is (0.9468822170900693, 0, 0). It is the best that you can get.
Date User Action Args
2022-04-11 14:59:31adminsetgithub: 84845
2020-05-19 14:25:15serhiy.storchakasetmessages: + msg369355
2020-05-19 02:22:06rhettingersetnosy: + rhettinger, serhiy.storchaka
messages: + msg369316
2020-05-18 09:10:27Zac Hatfield-Doddscreate