import math


def hsv2rgb(hsv):
    """Map an HSV color into an RGB color

    Parameters
    ----------
    hsv : numpy.array_like
        Array with three float values representing the Hue, Saturation,
        and Value components of the color. Each component should be a
        value between 0 and 1

    Returns
    -------
    rgb: numpy.array_like
        Array with three float values representing the corresponding
        Red, Green, and Blue components of the color. Each component is
        a value between 0 and 1

    Examples
    --------
    >>> import geomproc
    >>> rgb = geomproc.hsv2rgb([0.5, 0.8, 0.8])
    """
 
    # Output RGB color
    out = [0, 0, 0]

    # Perform mapping with this rather obscure algorithm
    h = hsv[0]*360.0
    if h >= 360.0:
        h = 0.0
    h /= 60.0
    i = math.floor(h)
    f = h - i
    p = hsv[2] * (1.0 - hsv[1])
    q = hsv[2] * (1.0 - (hsv[1] * f))
    t = hsv[2] * (1.0 - (hsv[1] * (1.0 - f)))

    if i == 0:
        out[0] = hsv[2]
        out[1] = t
        out[2] = p
    elif i == 1:
        out[0] = q
        out[1] = hsv[2]
        out[2] = p
    elif i == 2:
        out[0] = p
        out[1] = hsv[2]
        out[2] = t
    elif i == 3:
        out[0] = p
        out[1] = q
        out[2] = hsv[2]
    elif i == 4:
        out[0] = t
        out[1] = p
        out[2] = hsv[2]
    else:
        out[0] = hsv[2]
        out[1] = p
        out[2] = q

    return out