import math


def hsv_to_rgb(hue, saturation, value):
    """
    Convert HSV color (hue, saturation, value) to RGB (red, green, blue)
    @param hue:        0=Red, 1/6=Yellow, 2/6=Green, 3/6=Cyan, 4/6=Blue, etc.
    @param saturation: 0.0=Monochrome to 1.0=Fully saturated
    @param value:      0.0=Black to 1.0=Max brightness
    @returns: red, green, blue eacn in range 0 to 255
    """
    hue = hue * 6.0       # Hue circle = 0.0 to 6.0
    sxt = math.floor(hue) # Sextant index is next-lower integer of hue
    frac = hue - sxt      # Fraction-within-sextant is 0.0 to <1.0
    sxt = int(sxt) % 6    # mod6 the sextant so it's always 0 to 5

    if sxt == 0: # Red to <yellow
        red, green, blue = 1.0, frac, 0.0
    elif sxt == 1: # Yellow to <green
        red, green, blue = 1.0 - frac, 1.0, 0.0
    elif sxt == 2: # Green to <cyan
        red, green, blue = 0.0, 1.0, frac
    elif sxt == 3: # Cyan to <blue
        red, green, blue = 0.0, 1.0 - frac, 1.0
    elif sxt == 4: # Blue to <magenta
        red, green, blue = frac, 0.0, 1.0
    else: # Magenta to <red
        red, green, blue = 1.0, 0.0, 1.0 - frac

    invsat = 1.0 - saturation # Inverse-of-saturation

    red = int(((red * saturation) + invsat) * value * 255.0 + 0.5)
    green = int(((green * saturation) + invsat) * value * 255.0 + 0.5)
    blue = int(((blue * saturation) + invsat) * value * 255.0 + 0.5)

    return red, green, blue