dipy/viz/colormap.py
def distinguishable_colormap(bg=(0, 0, 0), exclude=[], nb_colors=None):
    """ Generates colors that are maximally perceptually distinct.

    This function generates a set of colors which are distinguishable
    by reference to the "Lab" color space, which more closely matches
    human color perception than RGB. Given an initial large list of possible
    colors, it iteratively chooses the entry in the list that is farthest (in
    Lab space) from all previously-chosen entries. While this "greedy"
    algorithm does not yield a global maximum, it is simple and efficient.
    Moreover, the sequence of colors is consistent no matter how many you
    request, which facilitates the users' ability to learn the color order
    and avoids major changes in the appearance of plots when adding or
    removing lines.

    Parameters
    ----------
    bg : tuple (optional)
        Background RGB color, to make sure that your colors are also
        distinguishable from the background. Default: (0, 0, 0).
    exclude : list of tuples (optional)
        Additional RGB colors to be distinguishable from.
    nb_colors : int (optional)
        Number of colors desired. Default: generate as many colors as needed.

    Returns
    -------
    iterable of ndarray
        If `nb_colors` is provided, returns a list of RBG colors.
        Otherwise, yields the next RBG color maximally perceptually
        distinct from previous ones.

    Examples
    --------
    >>> from dipy.viz.colormap import distinguishable_colormap
    >>> # Generate 5 colors
    >>> [c for i, c in zip(range(5), distinguishable_colormap())]
    [array([ 0.,  1.,  0.]),
     array([ 1.,  0.,  1.]),
     array([ 1.        ,  0.75862069,  0.03448276]),
     array([ 0.        ,  1.        ,  0.89655172]),
     array([ 0.        ,  0.17241379,  1.        ])]


    Notes
    -----
    Code was initially in matlab and was rewritten in Python for dipy by
    the Dipy Team. Thank you Tim Holy for putting this online. Visit
    http://www.mathworks.com/matlabcentral/fileexchange/29702 for the
    original implementation (v1.2), 14 Dec 2010 (Updated 07 Feb 2011).

    """

    NB_DIVISIONS = 30  # This constant come from the original code.

    # Generate a sizable number of RGB triples. This represents our space of
    # possible choices. By starting in RGB space, we ensure that all of the
    # colors can be generated by the monitor.
    colors_to_exclude = np.array([bg] + exclude)
    x = np.linspace(0, 1, NB_DIVISIONS)  # Divisions along each axis in RGB space.
    R, G, B = np.meshgrid(x, x, x)
    rgb = np.c_[R.flatten(), G.flatten(), B.flatten()]

    lab = _rgb2lab(rgb)
    bglab = _rgb2lab(colors_to_exclude)

    def _generate_next_color():
        lastlab = bglab[0]
        mindist2 = np.ones(len(rgb)) * np.inf
        for bglab_i in bglab[1:]:
            dist2 = np.sum((lab-bglab_i)**2, axis=1)
            mindist2 = np.minimum(dist2, mindist2)  # Dist2 to closest previously-chosen color.

        while True:
            dX = lab - lastlab  # Displacement of last from all colors on list.
            dist2 = np.sum(dX**2, axis=1)  # Square distance.

            mindist2 = np.minimum(dist2, mindist2)  # Dist2 to closest previously-chosen color.
            idx = np.argmax(mindist2)  # Find the entry farthest from all previously-chosen colors.
            yield rgb[idx]

            lastlab = lab[idx]

    if nb_colors is not None:
        return [c for i, c in zip(range(nb_colors), _generate_next_color())]

    return _generate_next_color()
 Generates colors that are maximally perceptually distinct.

    This function generates a set of colors which are distinguishable
    by reference to the "Lab" color space, which more closely matches
    human color perception than RGB. Given an initial large list of possible
    colors, it iteratively chooses the entry in the list that is farthest (in
    Lab space) from all previously-chosen entries. While this "greedy"
    algorithm does not yield a global maximum, it is simple and efficient.
    Moreover, the sequence of colors is consistent no matter how many you
    request, which facilitates the users' ability to learn the color order
    and avoids major changes in the appearance of plots when adding or
    removing lines.

    Parameters
    ----------
    bg : tuple (optional)
        Background RGB color, to make sure that your colors are also
        distinguishable from the background. Default: (0, 0, 0).
    exclude : list of tuples (optional)
        Additional RGB colors to be distinguishable from.
    nb_colors : int (optional)
        Number of colors desired. Default: generate as many colors as needed.

    Returns
    -------
    iterable of ndarray
        If `nb_colors` is provided, returns a list of RBG colors.
        Otherwise, yields the next RBG color maximally perceptually
        distinct from previous ones.

    Examples
    --------
    >>> from dipy.viz.colormap import distinguishable_colormap
    >>> # Generate 5 colors
    >>> [c for i, c in zip(range(5), distinguishable_colormap())]
    [array([ 0.,  1.,  0.]),
     array([ 1.,  0.,  1.]),
     array([ 1.        ,  0.75862069,  0.03448276]),
     array([ 0.        ,  1.        ,  0.89655172]),
     array([ 0.        ,  0.17241379,  1.        ])]


    Notes
    -----
    Code was initially in matlab and was rewritten in Python for dipy by
    the Dipy Team. Thank you Tim Holy for putting this online. Visit
    http://www.mathworks.com/matlabcentral/fileexchange/29702 for the
    original implementation (v1.2), 14 Dec 2010 (Updated 07 Feb 2011).

    
Generates colors that are maximally perceptually distinct.
This function generates a set of colors which are distinguishable
by reference to the "Lab" color space, which more closely matches
human color perception than RGB. Given an initial large list of possible
colors, it iteratively chooses the entry in the list that is farthest (in
Lab space) from all previously-chosen entries. While this "greedy"
algorithm does not yield a global maximum, it is simple and efficient.
Moreover, the sequence of colors is consistent no matter how many you
request, which facilitates the users' ability to learn the color order
and avoids major changes in the appearance of plots when adding or
removing lines.

Parameters

bg : tuple (optional)
Background RGB color, to make sure that your colors are also
distinguishable from the background.

Returns

iterable of ndarray
If `nb_colors` is provided, returns a list of RBG colors.
Otherwise, yields the next RBG color maximally perceptually
distinct from previous ones.

Examples



Notes

Code was initially in matlab and was rewritten in Python for dipy by
the Dipy Team. Thank you Tim Holy for putting this online.
Generates colors that are maximally perceptually distinct . This function generates a set of colors which are distinguishable by reference to the " Lab " color space which more closely matches human color perception than RGB . Given an initial large list of possible colors it iteratively chooses the entry in the list that is farthest ( in Lab space ) from all previously - chosen entries . While this " greedy " algorithm does not yield a global maximum it is simple and efficient . Moreover the sequence of colors is consistent no matter how many you request which facilitates the users ' ability to learn the color order and avoids major changes in the appearance of plots when adding or removing lines . Parameters bg : tuple ( optional ) Background RGB color to make sure that your colors are also distinguishable from the background . Returns iterable of ndarray If ` nb_colors ` is provided returns a list of RBG colors . Otherwise yields the next RBG color maximally perceptually distinct from previous ones . Examples Notes Code was initially in matlab and was rewritten in Python for dipy by the Dipy Team . Thank you Tim Holy for putting this online .
def distinguishable_colormap(bg=(0, 0, 0), exclude=[], nb_colors=None):
    NB_DIVISIONS = 30  
    colors_to_exclude = np.array([bg] + exclude)
    x = np.linspace(0, 1, NB_DIVISIONS)  
    R, G, B = np.meshgrid(x, x, x)
    rgb = np.c_[R.flatten(), G.flatten(), B.flatten()]
    lab = _rgb2lab(rgb)
    bglab = _rgb2lab(colors_to_exclude)
    def _generate_next_color():
        lastlab = bglab[0]
        mindist2 = np.ones(len(rgb)) * np.inf
        for bglab_i in bglab[1:]:
            dist2 = np.sum((lab-bglab_i)**2, axis=1)
            mindist2 = np.minimum(dist2, mindist2)  
        while True:
            dX = lab - lastlab  
            dist2 = np.sum(dX**2, axis=1)  
            mindist2 = np.minimum(dist2, mindist2)  
            idx = np.argmax(mindist2)  
            yield rgb[idx]
            lastlab = lab[idx]
    if nb_colors is not None:
        return [c for i, c in zip(range(nb_colors), _generate_next_color())]
    return _generate_next_color()
def distinguishable_colormap ( bg = ( 0 , 0 , 0 ) , exclude = [ ] , nb_colors = None ) : NB_DIVISIONS = 30 colors_to_exclude = np . array ( [ bg ] + exclude ) x = np . linspace ( 0 , 1 , NB_DIVISIONS ) R , G , B = np . meshgrid ( x , x , x ) rgb = np . c_ [ R . flatten ( ) , G . flatten ( ) , B . flatten ( ) ] lab = _rgb2lab ( rgb ) bglab = _rgb2lab ( colors_to_exclude ) def _generate_next_color ( ) : lastlab = bglab [ 0 ] mindist2 = np . ones ( len ( rgb ) ) * np . inf for bglab_i in bglab [ 1 : ] : dist2 = np . sum ( ( lab - bglab_i ) ** 2 , axis = 1 ) mindist2 = np . minimum ( dist2 , mindist2 ) while True : dX = lab - lastlab dist2 = np . sum ( dX ** 2 , axis = 1 ) mindist2 = np . minimum ( dist2 , mindist2 ) idx = np . argmax ( mindist2 ) yield rgb [ idx ] lastlab = lab [ idx ] if nb_colors is not None : return [ c for i , c in zip ( range ( nb_colors ) , _generate_next_color ( ) ) ] return _generate_next_color ( )
Generates colors that are maximally perceptually distinct.
Generates colors that are maximally perceptually distinct .
""" Generates colors that are maximally perceptually distinct.

    This function generates a set of colors which are distinguishable
    by reference to the "Lab" color space, which more closely matches
    human color perception than RGB. Given an initial large list of possible
    colors, it iteratively chooses the entry in the list that is farthest (in
    Lab space) from all previously-chosen entries. While this "greedy"
    algorithm does not yield a global maximum, it is simple and efficient.
    Moreover, the sequence of colors is consistent no matter how many you
    request, which facilitates the users' ability to learn the color order
    and avoids major changes in the appearance of plots when adding or
    removing lines.

    Parameters
    ----------
    bg : tuple (optional)
        Background RGB color, to make sure that your colors are also
        distinguishable from the background. Default: (0, 0, 0).
    exclude : list of tuples (optional)
        Additional RGB colors to be distinguishable from.
    nb_colors : int (optional)
        Number of colors desired. Default: generate as many colors as needed.

    Returns
    -------
    iterable of ndarray
        If `nb_colors` is provided, returns a list of RBG colors.
        Otherwise, yields the next RBG color maximally perceptually
        distinct from previous ones.

    Examples
    --------
    >>> from dipy.viz.colormap import distinguishable_colormap
    >>> # Generate 5 colors
    >>> [c for i, c in zip(range(5), distinguishable_colormap())]
    [array([ 0.,  1.,  0.]),
     array([ 1.,  0.,  1.]),
     array([ 1.        ,  0.75862069,  0.03448276]),
     array([ 0.        ,  1.        ,  0.89655172]),
     array([ 0.        ,  0.17241379,  1.        ])]


    Notes
    -----
    Code was initially in matlab and was rewritten in Python for dipy by
    the Dipy Team. Thank you Tim Holy for putting this online. Visit
    http://www.mathworks.com/matlabcentral/fileexchange/29702 for the
    original implementation (v1.2), 14 Dec 2010 (Updated 07 Feb 2011).

    """ # This constant come from the original code. # Generate a sizable number of RGB triples. This represents our space of # possible choices. By starting in RGB space, we ensure that all of the # colors can be generated by the monitor. # Divisions along each axis in RGB space. # Dist2 to closest previously-chosen color. # Displacement of last from all colors on list. # Square distance. # Dist2 to closest previously-chosen color. # Find the entry farthest from all previously-chosen colors.
bg
exclude
nb_colors