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.

classification
Title: rgb_to_hls in colorsys.py
Type: behavior Stage: resolved
Components: Versions: Python 3.5, Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: madhavendra.sharma, serhiy.storchaka
Priority: normal Keywords:

Created on 2017-02-23 05:55 by madhavendra.sharma, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (5)
msg288410 - (view) Author: Madhavendra Sharma (madhavendra.sharma) Date: 2017-02-23 05:55
Calculations in rgb_to_hls at line number 67 in python version 2.7.3 for "h" the hue component are not correct according reference mentioned in the same source file "en.wikipedia.org/wiki/HSV_color_space".


code snippet:

    if r == maxc:
        #h = (bc-gc) #current code
        h = (bc-gc)%6 # suggestion
    elif g == maxc:
        h = 2.0+rc-bc
    else:
        #h = 4.0+gc-rc #current code
        h = 4.0+rc-gc # suggestion

    #h = (h/6.0) % 1.0 not required 
    
    # h = 60 *h # component should be multiplied by 60 but it depends on user
msg288412 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-02-23 06:19
All coordinates in RGB and HSV spaces are floating point numbers between 0.0 and 1.0. In Wikipedia article h is a degree value between 0° and 360°.

Could you provide an example of rgb_to_hls() returning invalid result?
msg288416 - (view) Author: Madhavendra Sharma (madhavendra.sharma) Date: 2017-02-23 06:40
Please check examples in en.wikipedia.org/wiki/HSL_and_HSV

I picked one of them
for R = .750, B=0.250, G=0.750 
corresponding HLS value are H= 300 = (5.0 * 60) , L =  .5, S=.5

but colorsys.rgb_to_hls(0.75 ,  0.25,  0.75) gives following output
(0.8333333333333334, 0.5, 0.5)
h*60 = 0.8333333333333334 * 60 is not equal to 300

correct out put should be (5.0, 0.5, 0.5) or if h is multiplied by 60 then (300, 0.5, 0.5)

Thank you.
Madhavendra Sharma
msg288417 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-02-23 07:01
0.8333333333333334 * 360 is equivalent to 300.
msg288434 - (view) Author: Madhavendra Sharma (madhavendra.sharma) Date: 2017-02-23 09:52
OK, That's fine. 
Thanks for the clarification.

But most of the calculations I found for the conversion from RGB to HLS contains H = 60 * h' that's why I could not interpret it properly. Even on the same wiki page calculations specific to RGB to HLS were that way only.

Thank you.
History
Date User Action Args
2022-04-11 14:58:43adminsetgithub: 73815
2017-02-23 10:01:26madhavendra.sharmasetstatus: open -> closed
resolution: not a bug
stage: resolved
2017-02-23 09:52:36madhavendra.sharmasetmessages: + msg288434
2017-02-23 07:01:25serhiy.storchakasetmessages: + msg288417
2017-02-23 06:40:40madhavendra.sharmasetmessages: + msg288416
2017-02-23 06:19:15serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg288412
2017-02-23 05:56:40madhavendra.sharmasettitle: rgb_to_hls to colorsys.py -> rgb_to_hls in colorsys.py
2017-02-23 05:55:34madhavendra.sharmacreate