4

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 and Tix Using Python I would rather get the list from the library than to hardcode it in my program.

Community
  • 1
  • 1
kinnla
  • 411
  • 4
  • 13
  • Make a list of colors and use them the way you want to. Simple as this. – Parviz Karimli Jul 23 '16 at 09:02
  • 1
    "I would rather get the list from the library than to hardcode it in my program." -Make a file that contains all these colors somewhere in your computer, then call it. – Parviz Karimli Jul 23 '16 at 09:07
  • 1
    In case of linux/debian there is file */etc/X11/rgb.txt* that has lines like "255 250 250 snow". Your program could pick colors from that file (or a copy of it). – J.J. Hakala Jul 23 '16 at 10:49
  • You can post your comment as an answer and you will get +1 from me because that satisfies fully the laziness. @J.J.Hakala – Billal Begueradj Jul 23 '16 at 10:52
  • I ended up creating a file "racecolors.py" that can return the list of color names, as Parviz suggested. I understand now that tkinter is "only" an interface to a native GUI, so not all its features are accessible via python. – kinnla Jul 23 '16 at 19:35

2 Answers2

3

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.

J.J. Hakala
  • 6,136
  • 6
  • 27
  • 61
0

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

import numpy as np
import matplotlib.colors as mcolors

def get_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 colors
    if n > max_colors:
        print('max number of colors exceeded, please choose another palette')
        random_colors = None
    else:
        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