def RGBtoHSB( nRed, nGreen, nBlue ):
    """RGB to HSB color space conversion routine.
    nRed, nGreen and nBlue are all numbers from 0 to 255.
    This routine returns three floating point numbers, nHue, nSaturation, nBrightness.
    nHue, nSaturation and nBrightness are all from 0.0 to 1.0.
    """
    nMin = min( nRed, nGreen, nBlue )
    nMax = max( nRed, nGreen, nBlue )

    if nMin == nMax:
        # Grayscale
        nHue = 0.0
        nSaturation = 0.0
        nBrightness = nMax
    else:
        if nRed == nMin:
            d = nGreen = nBlue
            h = 3.0
        elif nGreen == nMin:
            d = nBlue - nRed
            h = 5.0
        else:
            d = nRed - nGreen
            h = 1.0

        nHue = ( h - ( float( d ) / (nMax - nMin) ) ) / 6.0
        nSaturation = (nMax - nMin) / float( nMax )
        nBrightness = nMax / 255.0

    return nHue, nSaturation, nBrightness