def build_unique_palette(thread_palette, threadlist):
    """Turns a threadlist into a unique index list with the thread palette"""
    chart = [None] * len(thread_palette)
    for thread in set(threadlist):
        index = thread.find_nearest_color_index(thread_palette)
        if index is None:
            break
        thread_palette[index] = None
        chart[index] = thread
    palette = []
    for thread in threadlist:
        palette.append(thread.find_nearest_color_index(chart))
    return palette
