コード例 #1
0
def calc_delta(rgb1=(0, 0, 0), rgb2=(0, 0, 0), algo=delta_e_cie2000):
    lab1 = convert_color(AdobeRGBColor(*rgb1, is_upscaled=True), LabColor)
    lab2 = convert_color(AdobeRGBColor(*rgb2, is_upscaled=True), LabColor)
    if algo == delta_e_cie2000:
        return algo(lab1, lab2, Kl=0.63)
    else:
        return algo(lab1, lab2)
コード例 #2
0
def colorAdjust(layout, ctype=0, origin='rgb(0,0,0)'):
    result = []
    for item in layout:
        (word, count), font_size, (x, y), (width,
                                           height), orientation, color = item
        [r, g, b] = origin.replace('rgb(', '').replace(')', '').split(',')
        rgbObj = AdobeRGBColor(r, g, b)
        l, c, h = convert_color(rgbObj, LCHabColor).get_value_tuple()
        if ctype == 0:  # base
            l = random.randint(4300, 4700)
            c = random.randint(2300, 2500)
            h = random.randint(0, 2500)
        if ctype == 1:  # inner
            l = random.randint(3000, 3500)
            c += random.randint(-30, 0)
            h += random.randint(-60, 60)
        if ctype == 2:  # outer
            l = random.randint(3000, 5000)
            c += random.randint(-10, 30)
            h += random.randint(-30, 30)
        nr, ng, nb = convert_color(LCHabColor(l, c, h),
                                   AdobeRGBColor).get_value_tuple()
        color = f'rgb({int(nr)},{int(ng)},{int(nb)})'
        result.append(((word, count), font_size, (x, y), (width, height),
                       orientation, color))
    return result
コード例 #3
0
def son_colores_parecidos(color1, color2):
    '''Usa la librería colormath para determinar si un par de colores es parecido y devuelve verdadero en ese caso'''
    from colormath.color_objects import AdobeRGBColor, LabColor, XYZColor
    from colormath.color_conversions import convert_color
    from colormath.color_diff import delta_e_cie1976

    try:
        color1 = AdobeRGBColor.new_from_rgb_hex(color1)
        color2 = AdobeRGBColor.new_from_rgb_hex(color2)
        delta_e = delta_e_cie1976(convert_color(color1, LabColor),
                                  convert_color(color2, LabColor))
        print('delta color: ', delta_e)
        #print(abs(delta_e)<50)
        return abs(delta_e) < 50

    except ValueError:
        print('Elige un color')
        return False
コード例 #4
0
    def test_adobe_conversion_to_xyz_d65(self):
        """
        Adobe RGB's native illuminant is D65, like sRGB's. However, sRGB uses
        different conversion math that uses gamma, so test the alternate logic
        route for non-sRGB RGB colors.
        """

        adobe = AdobeRGBColor(0.482, 0.784, 0.196)
        xyz = convert_color(adobe, XYZColor)
        self.assertColorMatch(xyz, XYZColor(0.230, 0.429, 0.074))
コード例 #5
0
ファイル: run_fcn_voc.py プロジェクト: Lizonghang/learn-gluon
def delta_e_cie(pixels):
    rgbs = [AdobeRGBColor(*pix, is_upscaled=True) for pix in pixels]
    labs = [convert_color(rgb, LabColor) for rgb in rgbs]
    print(rgbs)
    print(labs)
    for i in range(len(files) - 1):
        for j in range(i + 1, len(files)):
            delta_e = delta_e_cie2000(labs[i], labs[j])
            print(
                "color difference between %9s and %9s is: %.1f" %
                (classes[random_idxes[i]], classes[random_idxes[j]], delta_e))
コード例 #6
0
    def test_adobe_conversion_to_xyz_d50(self):
        """
        Adobe RGB's native illuminant is D65, so an adaptation matrix is
        involved here. However, the math for sRGB and all other RGB types is
        different, so test all of the other types with an adaptation matrix
        here.
        """

        adobe = AdobeRGBColor(0.482, 0.784, 0.196)
        xyz = convert_color(adobe, XYZColor, target_illuminant='D50')
        self.assertColorMatch(xyz, XYZColor(0.247, 0.431, 0.060))
コード例 #7
0
def convert_hex_to_xyz(hex_color: str) -> (float, float, float):
    adobe_rgb_color = AdobeRGBColor.new_from_rgb_hex(hex_color)

    # clampedRgbColors = AdobeRGBColor(hexx.clamped_rgb_r, hexx.clamped_rgb_g, hexx.clamped_rgb_b)

    # xyz = convert_color(clampedRgbColors, XYZColor, through_rgb_type=AdobeRGBColor)

    xyz_color = convert_color(adobe_rgb_color,
                              XYZColor,
                              through_rgb_type=AdobeRGBColor,
                              target_illuminant='d65')

    return xyz_color.get_value_tuple()
コード例 #8
0
ファイル: colors.py プロジェクト: mohammeddahmani/mindboggle
def group_colors(colormap, colormap_name, description='', adjacency_matrix=[],
                 IDs=[], names=[], groups=[],
                 save_text_files=True, plot_colors=True,
                 plot_graphs=True, out_dir='.', verbose=True):
    """
    This greedy algoritm reorders a colormap so that labels assigned to
    the same group have more similar colors, but within a group (usually
    of adjacent labels), the colors are reordered so that adjacent labels
    have dissimilar colors:

    1. Convert colormap to Lab color space
       which better represents human perception.
    2. Load a binary (or weighted) adjacency matrix, where each row or column
       represents a label, and each value signifies whether (or the degree
       to which) a given pair of labels are adjacent.
       If a string (file) is provided instead of a numpy ndarray:
            column 0 = label "ID" number
            column 1 = label "name"
            column 2 = "group" number (each label is assigned to a group)
            columns 3... = label adjacency matrix
    3. Sort labels by decreasing number of adjacent labels (adjacency sum).
    4. Order label groups by decreasing maximum adjacency sum.
    5. Create a similarity matrix for pairs of colors.
    6. Sort colors by decreasing perceptual difference from all other colors.
    7. For each label group:
        7.1. Select unpicked colors for group that are similar to the first
             unpicked color (unpicked colors were sorted above by decreasing
             perceptual difference from all other colors).
        7.2. Reorder subgraph colors according to label adjacency sum
             (decreasing number of adjacent labels).
    8. Assign new colors.

    For plotting graphs and colormap:

    1. Convert the matrix to a graph, where each node represents a label
       and each edge represents the adjacency value between connected nodes.
    2. Break up the graph into subgraphs, where each subgraph contains labels
       assigned the same group number (which usually means they are adjacent).
    3. Plot the colormap and colored sub/graphs.

    NOTE: Requires pydotplus

    Parameters
    ----------
    colormap : string or numpy ndarray of ndarrays of 3 floats between 0 and 1
        csv file containing rgb colormap, or colormap array
    colormap_name : string
        name of colormap
    description : string
        description of colormap
    adjacency_matrix : string or NxN numpy ndarray (N = number of labels)
        csv file containing label adjacency matrix or matrix itself
    IDs : list of integers
        label ID numbers
    names : list of strings
        label names
    groups : list of integers
        label group numbers (one per label)
    save_text_files : Boolean
        save colormap as csv and json files?
    plot_colors : Boolean
        plot colormap as horizontal bar chart?
    plot_graphs : Boolean
        plot colormap as graphs?
    out_dir : string
        output directory path
    verbose : Boolean
        print to stdout?

    Returns
    -------
    colors : numpy ndarray of ndarrays of 3 floats between 0 and 1
        rgb colormap

    Examples
    --------
    >>> # Get colormap:
    >>> from mindboggle.mio.colors import distinguishable_colors
    >>> colormap = distinguishable_colors(ncolors=31,
    ...     backgrounds=[[0,0,0],[1,1,1]],
    ...     save_csv=False, plot_colormap=False, verbose=False)
    >>> # Get adjacency matrix:
    >>> from mindboggle.mio.colors import label_adjacency_matrix
    >>> from mindboggle.mio.fetch_data import prep_tests
    >>> urls, fetch_data = prep_tests()
    >>> label_file = fetch_data(urls['left_manual_labels'], '', '.vtk')
    >>> IDs, adjacency_matrix, output_table = label_adjacency_matrix(label_file,
    ...     ignore_values=[-1, 0], add_value=0, save_table=False,
    ...     output_format='', verbose=False)
    >>> adjacency_matrix = adjacency_matrix.values
    >>> adjacency_matrix = adjacency_matrix[:, 1::]
    >>> # Reorganize colormap:
    >>> from mindboggle.mio.colors import group_colors
    >>> from mindboggle.mio.labels import DKTprotocol
    >>> dkt = DKTprotocol()
    >>> colormap_name = "DKT31colormap"
    >>> description = "Colormap for DKT31 human brain cortical labels"
    >>> save_text_files = True
    >>> plot_colors = False
    >>> plot_graphs = False
    >>> out_dir = '.'
    >>> verbose = False
    >>> #IDs = dkt.DKT31_numbers
    >>> names = dkt.DKT31_names #dkt.left_cerebrum_cortex_DKT31_names
    >>> groups = dkt.DKT31_groups
    >>> colors = group_colors(colormap, colormap_name, description,
    ...     adjacency_matrix, IDs, names, groups,
    ...     save_text_files, plot_colors, plot_graphs, out_dir, verbose)
    >>> colors[0]
    [0.7586206896551724, 0.20689655172413793, 0.0]
    >>> colors[1]
    [0.48275862068965514, 0.4482758620689655, 0.48275862068965514]
    >>> colors[2]
    [0.3448275862068966, 0.3103448275862069, 0.034482758620689655]
    >>> colors[-1]
    [0.7931034482758621, 0.9655172413793103, 0.7931034482758621]

    No groups / subgraphs:

    >>> groups = []
    >>> colors = group_colors(colormap, colormap_name, description,
    ...     adjacency_matrix, IDs, names, groups,
    ...     save_text_files, plot_colors, plot_graphs, out_dir, verbose)
    >>> colors[0]
    [0.5172413793103449, 0.8275862068965517, 1.0]
    >>> colors[1]
    [0.13793103448275862, 0.0, 0.24137931034482757]
    >>> colors[2]
    [0.3793103448275862, 0.27586206896551724, 0.48275862068965514]
    >>> colors[-1]
    [0.6206896551724138, 0.48275862068965514, 0.3448275862068966]

    """
    import os
    import pandas as pd
    import numpy as np
    import matplotlib.pyplot as plt
    import networkx as nx
    from colormath.color_diff import delta_e_cie2000
    from colormath.color_objects import LabColor, AdobeRGBColor
    from colormath.color_conversions import convert_color
    import itertools

    from mindboggle.mio.colors import write_json_colormap, write_xml_colormap

    # ------------------------------------------------------------------------
    # Set parameters for graph layout and output files:
    # ------------------------------------------------------------------------
    if plot_graphs:
        graph_node_size = 1000
        graph_edge_width = 2
        graph_font_size = 10
        subgraph_node_size = 3000
        subgraph_edge_width = 5
        subgraph_font_size = 18
        axis_buffer = 10
        graph_image_file = os.path.join(out_dir, "label_graph.png")
        subgraph_image_file_pre = os.path.join(out_dir, "label_subgraph")
        subgraph_image_file_post = ".png"

    if plot_colors:
        colormap_image_file = os.path.join(out_dir, 'label_colormap.png')

    if save_text_files:
        colormap_csv_file = os.path.join(out_dir, 'label_colormap.csv')
        colormap_json_file = os.path.join(out_dir, 'label_colormap.json')
        colormap_xml_file = os.path.join(out_dir, 'label_colormap.xml')

    run_permutations = False

    # ------------------------------------------------------------------------
    # Load colormap:
    # ------------------------------------------------------------------------
    if verbose:
        print("Load colormap and convert to CIELAB color space.")
    if isinstance(colormap, np.ndarray):
        colors = colormap
    elif isinstance(colormap, str):
        colors = pd.read_csv(colormap, sep=',', header=None)
        colors = colors.values
    else:
        raise IOError("Please use correct format for colormap.")
    nlabels = np.shape(colors)[0]
    new_colors = np.copy(colors)

    if not IDs:
        IDs = range(nlabels)
    if not names:
        names = [str(x) for x in range(nlabels)]
    if not groups:
        groups = [1 for x in range(nlabels)]

    # ------------------------------------------------------------------------
    # Convert to Lab color space which better represents human perception:
    # ------------------------------------------------------------------------
    # https://python-colormath.readthedocs.io/en/latest/illuminants.html
    lab_colors = []
    for rgb in colors:
        lab_colors.append(convert_color(AdobeRGBColor(rgb[0], rgb[1], rgb[2]),
                                        LabColor))

    # ------------------------------------------------------------------------
    # Load label adjacency matrix:
    # ------------------------------------------------------------------------
    if np.size(adjacency_matrix):
        if verbose:
            print("Load label adjacency matrix.")
        if isinstance(adjacency_matrix, np.ndarray):
            adjacency_values = adjacency_matrix
        # If a string (file) is provided instead of a numpy ndarray:
        #    column 0 = label "ID" number
        #    column 1 = label "name"
        #    column 2 = "group" number (each label is assigned to a group)
        #    columns 3... = label adjacency matrix
        elif isinstance(adjacency_matrix, str):
            matrix = pd.read_csv(adjacency_matrix, sep=',', header=None)
            matrix = matrix.values
            IDs = matrix.ID
            names = matrix.name
            groups = matrix.group
            adjacency_values = matrix[[str(x) for x in IDs]].values
        else:
            raise IOError("Please use correct format for adjacency matrix.")
        if np.shape(adjacency_values)[0] != nlabels:
            raise IOError("The colormap and label adjacency matrix don't "
                          "have the same number of labels.")

        # Normalize adjacency values:
        adjacency_values = adjacency_values / np.max(adjacency_values)
    else:
        plot_graphs = False

    # ------------------------------------------------------------------------
    # Sort labels by decreasing number of adjacent labels (adjacency sum):
    # ------------------------------------------------------------------------
    if np.size(adjacency_matrix):
        adjacency_sums = np.sum(adjacency_values, axis = 1)  # sum rows
        isort_labels = np.argsort(adjacency_sums)[::-1]
    else:
        isort_labels = range(nlabels)

    # ------------------------------------------------------------------------
    # Order label groups by decreasing maximum adjacency sum:
    # ------------------------------------------------------------------------
    label_groups = np.unique(groups)
    if np.size(adjacency_matrix):
        max_adjacency_sums = []
        for label_group in label_groups:
            igroup = [i for i,x in enumerate(groups) if x == label_group]
            max_adjacency_sums.append(max(adjacency_sums[igroup]))
        label_groups = label_groups[np.argsort(max_adjacency_sums)[::-1]]

    # ------------------------------------------------------------------------
    # Convert adjacency matrix to graph for plotting:
    # ------------------------------------------------------------------------
    if plot_graphs:
        adjacency_graph = nx.from_numpy_matrix(adjacency_values)
        for inode in range(nlabels):
            adjacency_graph.node[inode]['ID'] = IDs[inode]
            adjacency_graph.node[inode]['label'] = names[inode]
            adjacency_graph.node[inode]['group'] = groups[inode]

    # ------------------------------------------------------------------------
    # Create a similarity matrix for pairs of colors:
    # ------------------------------------------------------------------------
    if verbose:
        print("Create a similarity matrix for pairs of colors.")
    dx_matrix = np.zeros((nlabels, nlabels))
    for icolor1 in range(nlabels):
        for icolor2 in range(nlabels):
            dx_matrix[icolor1,icolor2] = delta_e_cie2000(lab_colors[icolor1],
                                                         lab_colors[icolor2])
    # ------------------------------------------------------------------------
    # Sort colors by decreasing perceptual difference from all other colors:
    # ------------------------------------------------------------------------
    icolors_to_pick = list(np.argsort(np.sum(dx_matrix, axis = 1))[::-1])

    # ------------------------------------------------------------------------
    # Loop through label groups:
    # ------------------------------------------------------------------------
    for label_group in label_groups:
        if verbose:
            print("Labels in group {0}...".format(label_group))
        igroup = [i for i,x in enumerate(groups) if x == label_group]
        N = len(igroup)

        # --------------------------------------------------------------------
        # Select unpicked colors for group that are similar to the first
        # unpicked color (unpicked colors were sorted above by decreasing
        # perceptual difference from all other colors):
        # --------------------------------------------------------------------
        isimilar = np.argsort(dx_matrix[icolors_to_pick[0],
                                        icolors_to_pick])[0:N]
        icolors_to_pick_copy = icolors_to_pick.copy()
        group_colors = [list(colors[icolors_to_pick[i]]) for i in isimilar]
        if run_permutations:
            group_lab_colors = [lab_colors[icolors_to_pick[i]]
                                for i in isimilar]
        for iremove in isimilar:
            icolors_to_pick.remove(icolors_to_pick_copy[iremove])

        # --------------------------------------------------------------------
        # Reorder group colors according to label adjacency sum
        # (decreasing number of adjacent labels):
        # --------------------------------------------------------------------
        isort_group_labels = np.argsort(isort_labels[igroup])
        group_colors = [group_colors[i] for i in isort_group_labels]

        # --------------------------------------------------------------------
        # Compute differences between every pair of colors within group:
        # --------------------------------------------------------------------
        weights = False
        if run_permutations:
            permutation_max = np.zeros(N)
            NxN_matrix = np.zeros((N, N))
            # ----------------------------------------------------------------
            # Extract group adjacency submatrix:
            # ----------------------------------------------------------------
            neighbor_matrix = adjacency_values[igroup, :][:, igroup]
            if not weights:
                neighbor_matrix = (neighbor_matrix > 0).astype(np.uint8)
            # ----------------------------------------------------------------
            # Permute colors and color pair differences:
            # ----------------------------------------------------------------
            DEmax = 0
            permutations = [np.array(s) for s
                            in itertools.permutations(range(0, N), N)]
            if verbose:
                print(" ".join([str(N),'labels,',
                                str(len(permutations)),
                                'permutations:']))
            for permutation in permutations:
                delta_matrix = NxN_matrix.copy()
                for i1 in range(N):
                  for i2 in range(N):
                    if (i2 > i1) and (neighbor_matrix[i1, i2] > 0):
                      delta_matrix[i1,i2] = delta_e_cie2000(group_lab_colors[i1],
                                                            group_lab_colors[i2])
                if weights:
                    DE = np.sum((delta_matrix * neighbor_matrix))
                else:
                    DE = np.sum(delta_matrix)
                # ------------------------------------------------------------
                # Store color permutation with maximum adjacency cost:
                # ------------------------------------------------------------
                if DE > DEmax:
                    DEmax = DE
                    permutation_max = permutation
                # ------------------------------------------------------------
                # Reorder group colors by the maximum adjacency cost:
                # ------------------------------------------------------------
                group_colors = [group_colors[x] for x in permutation_max]
                new_colors[isimilar] = group_colors

        # --------------------------------------------------------------------
        # Assign new colors:
        # --------------------------------------------------------------------
        else:
            new_colors[isimilar] = group_colors

        # --------------------------------------------------------------------
        # Draw a figure of the colored subgraph:
        # --------------------------------------------------------------------
        if plot_graphs:
            plt.figure(label_group)
            subgraph = adjacency_graph.subgraph(igroup)

            # Layout:
            pos = nx.nx_pydot.graphviz_layout(subgraph,
                                              prog="neato")
            nx.draw(subgraph, pos, node_size=subgraph_node_size,
                    width=subgraph_edge_width, alpha=0.5,
                    with_labels=False)

            # Labels:
            labels={}
            for iN in range(N):
                labels[subgraph.nodes()[iN]] = \
                    subgraph.node[subgraph.nodes()[iN]]['label']
            nx.draw_networkx_labels(subgraph, pos, labels,
                                    font_size=subgraph_font_size,
                                    font_color='black')
            # Nodes:
            nodelist = list(subgraph.node.keys())
            for iN in range(N):
                nx.draw_networkx_nodes(subgraph, pos,
                    node_size=subgraph_node_size,
                    nodelist=[nodelist[iN]],
                    node_color=group_colors[iN])

            # Figure:
            ax = plt.gca().axis()
            plt.gca().axis([ax[0]-axis_buffer, ax[1]+axis_buffer,
                            ax[2]-axis_buffer, ax[3]+axis_buffer])
            plt.savefig(subgraph_image_file_pre + str(int(label_group)) +
                        subgraph_image_file_post)
            #plt.show()

    # ------------------------------------------------------------------------
    # Plot the entire graph (without colors):
    # ------------------------------------------------------------------------
    if plot_graphs:
        plt.figure(nlabels)

        # Graph:
        pos = nx.nx_pydot.graphviz_layout(adjacency_graph, prog="neato")
        nx.draw(adjacency_graph, pos,
                node_color='yellow',
                node_size=graph_node_size,
                width=graph_edge_width,
                with_labels=False)

        # Labels:
        labels={}
        for ilabel in range(nlabels):
            labels[ilabel] = adjacency_graph.node[ilabel]['label']
        nx.draw_networkx_labels(adjacency_graph, pos, labels,
                                font_size=graph_font_size,
                                font_color='black')
        # # Nodes:
        # nodelist = list(adjacency_graph.node.keys())
        # for icolor, new_color in enumerate(new_colors):
        #     nx.draw_networkx_nodes(subgraph, pos,
        #                            node_size=graph_node_size,
        #                            nodelist=[nodelist[icolor]],
        #                            node_color=new_color)

        plt.savefig(graph_image_file)
        plt.show()

    # ------------------------------------------------------------------------
    # Plot the subgraphs (colors):
    # ------------------------------------------------------------------------
    if plot_graphs:
        for label_group in label_groups:
            plt.figure(label_group)
            plt.show()

    # ------------------------------------------------------------------------
    # Plot the colormap as a horizontal bar chart:
    # ------------------------------------------------------------------------
    if plot_colors:
        plt.figure(nlabels, figsize=(5, 10))
        for ilabel in range(nlabels):
            ax = plt.subplot(nlabels, 1, ilabel + 1)
            plt.axis("off")
            rgb = new_colors[ilabel]
            plt.barh(0, 50, 1, 0, color=rgb)
        plt.savefig(colormap_image_file)
        plt.show()

    # ------------------------------------------------------------------------
    # Save new colormap as text files:
    # ------------------------------------------------------------------------
    if save_text_files:

        # ------------------------------------------------------------------------
        # Save new colormap as a csv file:
        # ------------------------------------------------------------------------
        np.savetxt(colormap_csv_file, new_colors, fmt='%.18e', delimiter=',',
                   newline='\n', header='')

        # ------------------------------------------------------------------------
        # Save new colormap as a json file:
        # ------------------------------------------------------------------------
        write_json_colormap(colormap=new_colors, label_numbers=IDs,
                            label_names=names,
                            colormap_file=colormap_json_file,
                            colormap_name=colormap_name,
                            description=description)

        # ------------------------------------------------------------------------
        # Save new colormap as an xml file:
        # ------------------------------------------------------------------------
        write_xml_colormap(colormap=new_colors, label_numbers=IDs,
                           colormap_file=colormap_xml_file,
                           colormap_name=colormap_name)

    # ------------------------------------------------------------------------
    # Return new colors:
    # ------------------------------------------------------------------------
    colors = new_colors.tolist()

    return colors
コード例 #9
0
ファイル: colors.py プロジェクト: mohammeddahmani/mindboggle
def distinguishable_colors(ncolors, backgrounds=[[0,0,0],[1,1,1]],
                           save_csv=True, plot_colormap=True, verbose=True):
    """
    Create a colormap of perceptually distinguishable colors.

    This program is a Python program based on Tim Holy's 2010-2011
    BSD-licensed Matlab program "distinguishable_colors.m"
    (https://www.mathworks.com/matlabcentral/fileexchange/
     29702-generate-maximally-perceptually-distinct-colors):

    "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
    RGB 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
    ----------
    ncolors : integer
        number of colors for the colormap
    backgrounds : list of list(s) of 3 elements between 0 and 1
        rgb background colors to initialize and distinguish from
    save_csv : Boolean
        save colormap as csv file?
    plot_colormap : Boolean
        plot colormap as horizontal bar chart?
    verbose : Boolean
        print to stdout?

    Returns
    -------
    colors : numpy ndarray of ndarrays of 3 floats between 0 and 1
        rgb colormap

    Examples
    --------
    >>> from mindboggle.mio.colors import distinguishable_colors
    >>> ncolors = 31
    >>> backgrounds = [[0,0,0],[1,1,1]]
    >>> save_csv = False
    >>> plot_colormap = False
    >>> verbose = False
    >>> colors = distinguishable_colors(ncolors, backgrounds,
    ...     save_csv, plot_colormap, verbose)
    >>> colors[0]
    array([ 0.62068966,  0.06896552,  1.        ])
    >>> colors[1]
    array([ 0.       ,  0.5862069,  0.       ])
    >>> colors[2]
    array([ 0.75862069,  0.20689655,  0.        ])

    """
    import numpy as np
    import matplotlib.pyplot as plt
    from colormath.color_objects import LabColor, AdobeRGBColor
    from colormath.color_conversions import convert_color
    from colormath.color_diff import delta_e_cie2000

    filename = "colormap_of_{0}_distinguishable_colors".format(ncolors)

    # ------------------------------------------------------------------------
    # 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:
    # ------------------------------------------------------------------------
    n_grid = 30  # number of grid divisions along each axis in RGB space
    x = np.linspace(0, 1, num=n_grid, endpoint=True)
    R, G, B = np.meshgrid(x, x, x)
    ncolors_total = np.size(R)
    RGB = np.vstack([np.ravel(R), np.ravel(G), np.ravel(B)])
    RGB = [[RGB[0][icolor], RGB[1][icolor], RGB[2][icolor]]
           for icolor in range(ncolors_total)]
    if ncolors > ncolors_total:
        raise IOError("You can't readily distinguish that many colors")

    # ------------------------------------------------------------------------
    # Convert to Lab color space which better represents human perception:
    # ------------------------------------------------------------------------
    # https://python-colormath.readthedocs.io/en/latest/illuminants.html
    lab_colors = []
    for rgb in RGB:
        lab = convert_color(AdobeRGBColor(rgb[0],
                                          rgb[1],
                                          rgb[2]), LabColor)
        lab_colors.append(lab)

    bg_lab_colors = []
    for bg_rgb in backgrounds:
        bg_lab = convert_color(AdobeRGBColor(bg_rgb[0],
                                             bg_rgb[1],
                                             bg_rgb[2]), LabColor)
        bg_lab_colors.append(bg_lab)

    # ------------------------------------------------------------------------
    # If the user specified multiple background colors, compute differences
    # between the candidate colors and the background colors:
    # ------------------------------------------------------------------------
    min_dx = np.inf * np.ones(ncolors_total)
    if backgrounds:
        for bg_lab_color in bg_lab_colors:
            # Store difference from closest previously-chosen color:
            for icolor_total, lab_color in enumerate(lab_colors):
                dx = delta_e_cie2000(lab_color, bg_lab_color)
                min_dx[icolor_total] = min(dx, min_dx[icolor_total])

    # ------------------------------------------------------------------------
    # Iteratively pick the color that maximizes the difference
    # with the nearest already-picked color:
    # ------------------------------------------------------------------------
    # Initialize by making the "previous" color equal to the last background:
    last_lab_color = bg_lab_colors[-1]
    colors = np.zeros((ncolors, 3))
    for icolor in range(ncolors):

        # Find the difference of the last color from all colors on the list:
        for icolor_total, lab_color in enumerate(lab_colors):
            dx = delta_e_cie2000(lab_color, last_lab_color)
            min_dx[icolor_total] = min(dx, min_dx[icolor_total])

        # Find the entry farthest from all previously chosen colors:
        imax_dx = np.argmax(min_dx)

        # Store distant color:
        colors[icolor] = RGB[imax_dx]

        # Prepare for next iteration:
        last_lab_color = lab_colors[imax_dx]

    # ------------------------------------------------------------------------
    # Plot the colormap as a horizontal bar chart:
    # ------------------------------------------------------------------------
    if plot_colormap:
        if verbose:
            print("RGB values:")
        plt.figure(ncolors, figsize=(5, 10))
        for icolor in range(ncolors):
            ax = plt.subplot(ncolors, 1, icolor + 1)
            plt.axis("off")
            rgb = colors[icolor]
            #rgb = [[rgb.rgb_r, rgb.rgb_g, rgb.rgb_b]]
            if verbose:
                print(rgb)
            plt.barh(0, 50, 1, 0, color=rgb)
            plt.savefig(filename + ".png")
        if verbose:
            print("Colormap image saved to {0}".format(filename + ".png"))

    # ------------------------------------------------------------------------
    # Save the colormap as a csv file:
    # ------------------------------------------------------------------------
    if save_csv:
        np.savetxt(filename + ".csv", colors, fmt='%.18e', delimiter=',',
                   newline='\n', header='')
        if verbose:
            print("Colormap saved to {0}".format(filename + ".csv"))

    return colors
コード例 #10
0
 def test_conversion_to_adobe_rgb(self):
     self.color = XYZColor(0.2553, 0.1125, 0.0011)
     rgb = convert_color(self.color, AdobeRGBColor)
     # This ends up getting clamped.
     self.assertColorMatch(rgb, AdobeRGBColor(0.6828, 0.0, 0.0))