import math
from PIL import ImageColor


def indexedcolor(i, num, npersat=15, lightness=60):
    """Returns an rgb color triplet for a given index, with a finite max 'num'.
    Thus if you need 10 colors and want to get color #5, you would call this with (5, 10).
    The colors are "repeatable".
    """
    nsats = int(math.ceil(num/float(npersat)))
    sat = 100 - int((i//npersat)*(100/nsats))
    l = lightness
    nhues = int(math.ceil(num/float(nsats)))
    hue = (i % nhues) * (360//nhues)
    #print >>sys.stderr, 'For i %d, num %d, got %d sats, %d hues -> %d, %d, %d' % (i, num, nsats, nhues, hue, sat, l)
    return ImageColor.getrgb('hsl(%d,%d%%,%d%%)' % (hue, sat, l))