Python Int Divide By Int Equals Int?

Is it normal for Python to do a int / int = int with imported scripts, but int / int = float when in the main script? I had everything in one script 90 / 360 = 0.25, but after I refactored and referenced via import, suddenly 90 / 360 = 0.

I changed RGBToHSV to convert the incoming ints to float() and it started working again, but it’s very weird it handles division differently in a single script vs an imported script.

# UPDATED: fix for integer division>
from __future__ import division 
# end fix
from System.Drawing import Color

def HSVToColor(h, s, v):
    
    if s == 0.0: rgb = (v, v, v)
    i = int(h*6.) # XXX assume int() truncates!
    f = (h*6.)-i; p,q,t = int(255*(v*(1.-s))), int(255*(v*(1.-s*f))), int(255*(v*(1.-s*(1.-f)))); v*=255; i%=6
    if i == 0: rgb = (v, t, p)
    if i == 1: rgb = (q, v, p)
    if i == 2: rgb = (p, v, t)
    if i == 3: rgb = (p, q, v)
    if i == 4: rgb = (t, p, v)
    if i == 5: rgb = (v, p, q)
    return Color.FromArgb(*rgb)
    
def RGBToHSV(r, g, b):
    r = float(r)
    g = float(g)
    b = float(b)
    maxc = max(r, g, b)
    minc = min(r, g, b)
    v = maxc
    if minc == maxc:
        return 0.0, 0.0, v / 255
    s = (maxc-minc) / maxc
    rc = (maxc-r) / (maxc-minc)
    gc = (maxc-g) / (maxc-minc)
    bc = (maxc-b) / (maxc-minc)
    if r == maxc:
        h = bc-gc
    elif g == maxc:
        h = 2.0+rc-bc
    else:
        h = 4.0+gc-rc
    h = (h/6.0) % 1.0
    return h, s, v / 255

Moved to Scripting category so it will be seen.

1 Like

Hi @EricM,

How about just doing something like this?

import System
import Eto

# HSB (hue, saturation, brightness), is also known as 
# HSV (hue, saturation, value).

# Converts a System.Drawing.Color struct to HSB color values.
def system_color_to_hsb(clr):
    eto_color = Eto.Drawing.Color.FromArgb(clr.ToArgb())
    eto_hsb = eto_color.ToHSB()
    return eto_hsb.H, eto_hsb.S, eto_hsb.B, eto_hsb.A

# Creates a System.Drawing.Color struct from HSB color values.
def system_color_from_hsb(hue, saturation, brightness, alpha = 1.0):
    eto_hsb = Eto.Drawing.ColorHSB(hue, saturation, brightness, alpha)
    eto_color = eto_hsb.ToColor()
    return System.Drawing.Color.FromArgb(eto_color.ToArgb())

def test_color_conversion():
    color = System.Drawing.Color.DarkSalmon
    print("ARGB: A={0}, R={1}, G={2}, B={3}".format(color.A, color.R, color.G, color.B))
    hsb = system_color_to_hsb(color)
    print("HSB: H={0}, S={1}, B={2}, A={3}".format(hsb[0], hsb[1], hsb[2], hsb[3]))
    color = system_color_from_hsb(hsb[0], hsb[1], hsb[2], hsb[3])
    print("ARGB: A={0}, R={1}, G={2}, B={3}".format(color.A, color.R, color.G, color.B))
    
if __name__ == "__main__":
    test_color_conversion()

– Dale

Srry John, thought I set that to scripting.

Dale, it’s more about why integer division returned an int instead of a float when called in an imported script vs. in main.

I figured there was a pre-built somewhere, but in searching I found the formula before I found that Eto method.

Hi @EricM ,
Check this topic for a reply why integer division occurs:

Yes probably best to use this solution

Thanks. Added from __future__ import division in the color utility script. Now it behaves correctly when imported by the parent scripts.

2 Likes