python / cpython

The Python programming language
https://www.python.org
Other
62.46k stars 29.98k forks source link

Catastrophic loss of precision in colorsys module #84845

Closed 3a66f4c3-06fa-4389-ab37-45426bd4fef7 closed 1 year ago

3a66f4c3-06fa-4389-ab37-45426bd4fef7 commented 4 years ago
BPO 40668
Nosy @rhettinger, @serhiy-storchaka, @Zac-HD

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields: ```python assignee = None closed_at = None created_at = labels = ['3.7', '3.8', 'type-bug', 'library', '3.9'] title = 'Catastrophic loss of precision in colorsys module' updated_at = user = 'https://github.com/Zac-HD' ``` bugs.python.org fields: ```python activity = actor = 'serhiy.storchaka' assignee = 'none' closed = False closed_date = None closer = None components = ['Library (Lib)'] creation = creator = 'Zac Hatfield-Dodds' dependencies = [] files = [] hgrepos = [] issue_num = 40668 keywords = [] message_count = 3.0 messages = ['369197', '369316', '369355'] nosy_count = 3.0 nosy_names = ['rhettinger', 'serhiy.storchaka', 'Zac Hatfield-Dodds'] pr_nums = [] priority = 'normal' resolution = None stage = None status = 'open' superseder = None type = 'behavior' url = 'https://bugs.python.org/issue40668' versions = ['Python 3.5', 'Python 3.6', 'Python 3.7', 'Python 3.8', 'Python 3.9'] ```

3a66f4c3-06fa-4389-ab37-45426bd4fef7 commented 4 years ago

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].

[1] https://pyfound.blogspot.com/2020/05/property-based-testing-for-python.html [2] https://docs.python.org/3/library/colorsys.html [3] https://github.com/Zac-HD/stdlib-property-tests/pull/13

rhettinger commented 4 years ago

See https://bugs.python.org/issue14323

serhiy-storchaka commented 4 years ago

It is correct. Colorspaces for RGB and 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.

terryjreedy commented 1 year ago

Serhiy's comment says that there is no bug and hence no issue.