Skip to content Skip to sidebar Skip to footer

Named Colors In Tkinter

How can I get a list of all named colors in tkinter? I need to choose colors randomly and print their names to the user. I found a list of all colors here: Colour chart for Tkinter

Solution 1:

In case of linux (debian) there is a file /etc/X11/rgb.txt that has lines like

255 250 250             snow

and should be easy to parse. Your program could read color definitions from that file (or a copy of it) to a list, and then select a random color from that list.

Solution 2:

Is not the same but I wrote a function that may help:

import numpy as np
import matplotlib.colors as mcolors

defget_random_colors(n, palette):
    """
    :param n: numbers of desired colors
    :param palette: similar palette color to choose
    :return: random_color_list
    """
    colors = mcolors.CSS4_COLORS  
    # Sort colors by hue, saturation, value and name.
    by_hsv = sorted((tuple(mcolors.rgb_to_hsv(mcolors.to_rgb(color))), 
    name) for name, color in colors.items())
    names = [name for hsv, name in by_hsv]
    if palette == 'black_white':
        color_list = names[0:13]
        max_colors = color_list.__len__()
    elif palette == 'reds_yellow':
        color_list = names[14:63]
        max_colors = color_list.__len__()
    elif palette == 'greens':
        color_list = names[64:84]
        max_colors = color_list.__len__()
    elif palette == 'blues':
        color_list = names[85:124]
        max_colors = color_list.__len__()
    elif palette == 'purple_pink':
        color_list = names[124:-1]
        max_colors = color_list.__len__()
    else:
        color_list = names
        max_colors = color_list.__len__()
    # check n and max colorsif n > max_colors:
        print('max number of colors exceeded, please choose another palette')
        random_colors = Noneelse:
        random_index = np.random.choice(range(color_list.__len__()), n, replace=False)
        random_colors = []
    for ii in random_index:
        random_colors.append(color_list[ii])

return random_colors

Post a Comment for "Named Colors In Tkinter"