Different results in processing and interpreter

Hello again!
I encountered a rather strange thing in processing 3.5.4 :
the code

def rgbshv(x,y):
    if x==0:
        if max(col)==min(col):
            return 0
        if max(col)==col[0]:
            return (60*((col[1]-col[2])/(max(col)-min(col)))+360)%360
        if max(col)==col[1]:
            return (60*((col[2]-col[0])/(max(col)-min(col)))+120)%360
        if max(col)==col[2]:
            return (60*((col[0]-col[1])/(max(col)-min(col)))+240)%360
    if x!=0:
        return max(col)

yields different results in processing than a python interpreter and an online color converter and gimp’s color mixer.

Here are screenshots : https://imgur.com/a/cGoYn6O

processing 4 doesn’t run on my computer so i couldn’t try it in it.

The hue command also gives different results from the online converter and interpreter and gimp and this time also the code in processing.

I tried print(hue(color(229,28,36))) and i got 253.308456421, which is also quite different form the online converter’s 358.

Gimp’s color mixer gives me the same results as the online converter and the python interpreter,so the issue should reside in processing. Am i missing something?

How could i get the correct hue of the colors?

Thanks in advance.

Python 2 vs Python 3 division –

Processing’s Python mode uses Jython (Python 2.7). Python 2.7 will round down when you divide two integers, but not a floating-point and an integer, so:

print(5/2)   # 2
print(5./2)  # 2.5

You can convert one of the operands to a floating-point; for example, using a float() function:

return (60*((col[1]-col[2])/float(max(col)-min(col)))+360)%360

Regarding the hue issue –

Change the Processing color-mode to match that of the mixer you are referencing:

background(229, 28, 36)  # reddish color defined in RGB
# change color-mode to HSB
hue_range = 360  # 0-360 degrees
sat_range = 100  # 0-100 percent
bri_range = 100  # 0-100 percent
colorMode(HSB, hue_range, sat_range, bri_range) 
# sample hue of a pixel from background fill
print(hue(get(10, 10)))  # 357.61

GIMP’s colour mixer allows you to specify RGB and HSB (same as HSV) values, so you can confirm the results there.

1 Like