def global_color_table(color_depth, palette):
    """
    Return a valid global color table.
    The global color table of a GIF image is a 1-d bytearray of the form
    [r1, g1, b1, r2, g2, b2, ...] with length equals to 2**n where n is
    the color depth of the image.

    ----------
    Parameters

    color_depth: color depth of the GIF.

    palette: a list of rgb colors of the format [r1, g1, b1, r2, g2, b2, ...].
        The number of colors must be greater than or equal to 2**n where n is
        the color depth. Redundant colors will be discarded.
    """
    try:
        palette = bytearray(palette)
    except:
        raise ValueError('Cannot convert palette to bytearray.')

    valid_length = 3 * (1 << color_depth)
    if len(palette) < valid_length:
        raise ValueError('Invalid palette length.')
    if len(palette) > valid_length:
        palette = palette[:valid_length]

    return palette