Python generate colors

16 Python code examples are found related to " generate colors". You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
Example 1
Source File: visualization_utils.py    From MOTSFusion with MIT License 6 votes vote down vote up
def generate_colors():
    """
    Generate random colors.
    To get visually distinct colors, generate them in HSV space then
    convert to RGB.
    """
    import colorsys
    N = 30
    brightness = 0.7
    hsv = [(i / N, 1, brightness) for i in range(N)]
    colors = list(map(lambda c: colorsys.hsv_to_rgb(*c), hsv))
    perm = [15, 13, 25, 12, 19, 8, 22, 24, 29, 17, 28, 20, 2, 27, 11, 26, 21, 4, 3, 18, 9, 5, 14, 1, 16, 0, 23, 7,
            6, 10]
    colors = [colors[idx] for idx in perm]
    del colors[::2]
    return colors 
Example 2
Source File: utils.py    From InceptionTime with GNU General Public License v3.0 6 votes vote down vote up
def generate_array_of_colors(n):
    # https://www.quora.com/How-do-I-generate-n-visually-distinct-RGB-colours-in-Python
    ret = []
    r = int(random.random() * 256)
    g = int(random.random() * 256)
    b = int(random.random() * 256)
    alpha = 1.0
    step = 256 / n
    for i in range(n):
        r += step
        g += step
        b += step
        r = int(r) % 256
        g = int(g) % 256
        b = int(b) % 256
        ret.append((r / 255, g / 255, b / 255, alpha))
    return ret 
Example 3
Source File: visualize_mots.py    From mots_tools with MIT License 6 votes vote down vote up
def generate_colors():
  """
  Generate random colors.
  To get visually distinct colors, generate them in HSV space then
  convert to RGB.
  """
  N = 30
  brightness = 0.7
  hsv = [(i / N, 1, brightness) for i in range(N)]
  colors = list(map(lambda c: colorsys.hsv_to_rgb(*c), hsv))
  perm = [15, 13, 25, 12, 19, 8, 22, 24, 29, 17, 28, 20, 2, 27, 11, 26, 21, 4, 3, 18, 9, 5, 14, 1, 16, 0, 23, 7, 6, 10]
  colors = [colors[idx] for idx in perm]
  return colors


# from https://github.com/matterport/Mask_RCNN/blob/master/mrcnn/visualize.py 
Example 4
Source File: cml.py    From codimension with GNU General Public License v3.0 5 votes vote down vote up
def generateColors(background, foreground, border):
        """Generates the colors part"""
        parts = []
        if background is not None:
            parts.append('bg=' + cssLikeColor(background))
        if foreground is not None:
            parts.append('fg=' + cssLikeColor(foreground))
        if border is not None:
            parts.append('border=' + cssLikeColor(border))
        return ' '.join(parts) 
Example 5
Source File: general.py    From devicehive-video-analysis with Apache License 2.0 5 votes vote down vote up
def generate_colors(n, max_value=255):
    colors = []
    h = 0.1
    s = 0.5
    v = 0.95
    for i in range(n):
        h = 1 / (h + GOLDEN_RATIO)
        colors.append([c*max_value for c in colorsys.hsv_to_rgb(h, s, v)])

    return colors 
Example 6
Source File: texture_utils.py    From wwrando with MIT License 5 votes vote down vote up
def generate_new_palettes_from_colors(colors, palette_format):
  encoded_colors = []
  for color in colors:
    encoded_color = encode_color(color, palette_format)
    encoded_colors.append(encoded_color)
  
  return encoded_colors 
Example 7
Source File: ssd_mobilenet_utils.py    From object-detection with MIT License 5 votes vote down vote up
def generate_colors(class_names):
    hsv_tuples = [(x / len(class_names), 1., 1.) for x in range(len(class_names))]
    colors = list(map(lambda x: colorsys.hsv_to_rgb(*x), hsv_tuples))
    colors = list(map(lambda x: (int(x[0] * 255), int(x[1] * 255), int(x[2] * 255)), colors))
    random.seed(10101)  # Fixed seed for consistent colors across runs.
    random.shuffle(colors)  # Shuffle colors to decorrelate adjacent classes.
    random.seed(None)  # Reset seed to default.
    return colors 
Example 8
Source File: ansi.py    From instagram-terminal-news-feed with MIT License 5 votes vote down vote up
def generate_ANSI_to_set_fg_bg_colors(cur_fg_color, cur_bg_color, new_fg_color, new_bg_color):

    # This code assumes that ESC[49m and ESC[39m work for resetting bg and fg
    # This may not work on all terminals in which case we would have to use ESC[0m
    # to reset both at once, and then put back fg or bg that we actually want

    # We don't change colors that are already the way we want them - saves lots of file size

    color_array = []        # use array mechanism to avoid multiple escape sequences if we need to change fg AND bg

    if new_bg_color != cur_bg_color:
        if new_bg_color is None:
            color_array.append('49')        # reset to default
        else:
            color_array += getANSIbgarray_for_ANSIcolor(new_bg_color)

    if new_fg_color != cur_fg_color:
        if new_fg_color is None:
            color_array.append('39')        # reset to default
        else:
            color_array += getANSIfgarray_for_ANSIcolor(new_fg_color)

    if len(color_array) > 0:
        return "\x1b[" + ";".join(color_array) + "m"
    else:
        return "" 
Example 9
Source File: style.py    From agent-trainer with MIT License 5 votes vote down vote up
def generate_tableau20_colors():
    tableau20_rgb_components = [(31, 119, 180), (174, 199, 232), (255, 127, 14), (255, 187, 120),
                                (44, 160, 44), (152, 223, 138), (214, 39, 40), (255, 152, 150),
                                (148, 103, 189), (197, 176, 213), (140, 86, 75), (196, 156, 148),
                                (227, 119, 194), (247, 182, 210), (127, 127, 127), (199, 199, 199),
                                (188, 189, 34), (219, 219, 141), (23, 190, 207), (158, 218, 229)]
    return [(rgb[0] / 255., rgb[1] / 255., rgb[2] / 255.) for rgb in tableau20_rgb_components] 
Example 10
Source File: title.py    From brand with Creative Commons Zero v1.0 Universal 5 votes vote down vote up
def generate_title_by_colors(color_name, bg_color, font_color):
    dwg = svgwrite.Drawing('generate/cover/' + color_name + '.svg', size=(u'950', u'500'))

    shapes = dwg.add(dwg.g(id='shapes', fill='none'))

    shapes.add(dwg.rect((0, 0), (950, 500), fill='#' + bg_color))
    shapes.add(dwg.text('JavaScript', insert=(475, 190), fill='#' + font_color, font_size=120,
                        style="text-anchor: middle; dominant-baseline: hanging;",
                        font_family='Helvetica'))

    dwg.save() 
Example 11
Source File: RecurrentDetectionForwarder.py    From TrackR-CNN with MIT License 5 votes vote down vote up
def generate_colors():
  """
  Generate random colors.
  To get visually distinct colors, generate them in HSV space then
  convert to RGB.
  """
  N = 30
  brightness = 0.7
  hsv = [(i / N, 1, brightness) for i in range(N)]
  colors = list(map(lambda c: colorsys.hsv_to_rgb(*c), hsv))
  perm = [15, 13, 25, 12, 19, 8, 22, 24, 29, 17, 28, 20, 2, 27, 11, 26, 21, 4, 3, 18, 9, 5, 14, 1, 16, 0, 23, 7, 6, 10]
  colors = [colors[idx] for idx in perm]
  return colors 
Example 12
Source File: visualize.py    From SketchyScene with MIT License 5 votes vote down vote up
def generate_colors(N, bright=True, fix_color=False):
    """
    Generate random colors.
    To get visually distinct colors, generate them in HSV space then
    convert to RGB.
    """
    if fix_color:
        fixed_colors = \
            [(1.0, 0.0, 0.9473684210526319), (0.4210526315789469, 0.0, 1.0), (0.0, 1.0, 0.8421052631578947),
             (1.0, 0.0, 0.0), (0.10526315789473717, 0.0, 1.0), (1.0, 0.9473684210526315, 0.0),
             (0.42105263157894735, 1.0, 0.0), (1.0, 0.0, 0.6315789473684212), (0.0, 0.5263157894736841, 1.0),
             (1.0, 0.631578947368421, 0.0), (0.0, 0.2105263157894739, 1.0), (0.10526315789473695, 1.0, 0.0),
             (1.0, 0.0, 0.3157894736842106), (1.0, 0.3157894736842105, 0.0), (0.0, 1.0, 0.5263157894736841),
             (0.7368421052631575, 0.0, 1.0), (0.736842105263158, 1.0, 0.0), (0.0, 0.8421052631578947, 1.0),
             (0.0, 1.0, 0.21052631578947345),
             (0.33333333333333326, 1.0, 0.0), (1.0, 0.0, 0.3333333333333339), (1.0, 0.33333333333333326, 0.0),
             (1.0, 1.0, 0.0), (0.0, 1.0, 1.0), (0.0, 1.0, 0.0), (1.0, 0.0, 0.0), (1.0, 0.0, 1.0),
             (0.33333333333333304, 0.0, 1.0), (0.0, 1.0, 0.3333333333333335), (0.0, 0.0, 1.0),
             (0.666666666666667, 0.0, 1.0), (1.0, 0.0, 0.666666666666667), (0.0, 0.33333333333333304, 1.0),
             (1.0, 0.6666666666666666, 0.0), (0.6666666666666667, 1.0, 0.0), (0.0, 1.0, 0.6666666666666665),
             (0.0, 0.6666666666666665, 1.0),
             ]
        return fixed_colors
    else:
        brightness = 1.0 if bright else 0.7
        hsv = [(i / N, 1, brightness) for i in range(N)]
        colors = list(map(lambda c: colorsys.hsv_to_rgb(*c), hsv))
        random.shuffle(colors)
        return colors 
Example 13
Source File: read_mesh.py    From 3D-R2N2 with MIT License 4 votes vote down vote up
def generate_morph_colors(colorfiles, n_vertices, n_faces):
    morphColorData = []
    colorFaces = []
    materialColors = []

    for mfilepattern in colorfiles.split():

        matches = glob.glob(mfilepattern)
        matches.sort()
        for path in matches:
            normpath = os.path.normpath(path)
            name = os.path.basename(normpath)

            morphFaces, morphVertices, morphUvs, morphNormals, morphMaterials, morphMtllib = parse_obj(
                normpath)

            n_morph_vertices = len(morphVertices)
            n_morph_faces = len(morphFaces)

            if n_vertices != n_morph_vertices:

                print(
                    "WARNING: skipping morph color map [%s] with different number of vertices [%d] than the original model [%d]"
                    % (name, n_morph_vertices, n_vertices))

            elif n_faces != n_morph_faces:

                print(
                    "WARNING: skipping morph color map [%s] with different number of faces [%d] than the original model [%d]"
                    % (name, n_morph_faces, n_faces))

            else:

                morphMaterialColors = extract_material_colors(morphMaterials, morphMtllib, normpath)
                morphFaceColors = extract_face_colors(morphFaces, morphMaterialColors)
                morphColorData.append((get_name(name), morphFaceColors))

                # take first color map for baking into face colors

                if len(colorFaces) == 0:
                    colorFaces = morphFaces
                    materialColors = morphMaterialColors

                print("adding [%s] with %d face colors" % (name, len(morphFaceColors)))

    morphColors = ""
    if len(morphColorData):
        morphColors = "\n%s\n\t" % ",\n".join(
            generate_morph_color(name, colors) for name, colors in morphColorData)

    return morphColors, colorFaces, materialColors


# #####################################################
# Materials
# #####################################################