Search code examples
pythoncolorskivy

Kivy - is there a list of all color names?


In Kivy, the widgets' color property allows enter its value as a string of a color name, too, e.g. in .kv file:

   Label:
       color: "red"

Is there a list of all possible color names?


Solution

  • TL;DR

    from kivy.utils import colormap
    # import dict with all CSS 3 Colors 
    # like {'aliceblue':[0.9411764705882353, 0.9725490196078431, 1.0, 1.0]}
    
    'aliceblue' in colormap 
    # True
    

    For the plot, see the end of this answer.


    Dictionary of colors

    The kivy docs mention that colors referenced by name are retrieved from an object called colormap. This object resides in kivy.utils as a variable storing a dictionary comprehension that iterates over another dictionary named hex_colormap:

    colormap = {k: get_color_from_hex(v) for k, v in hex_colormap.items()}
    

    The ultimate source for these dictionaries is referenced only indirectly in the docs (link). A better reference would be: CSS 3 Colors (recommended by the W3C).

    At any rate, in order to retrieve all the valid color names, you can import either one of these objects:

    from kivy.utils import hex_colormap, colormap
    
    hex_colormap # name (key): hex (value)
    
    {'aliceblue': '#f0f8ff',
     'antiquewhite': '#faebd7',
     ...
     'yellow': '#ffff00',
     'yellowgreen': '#9acd32'}
    
    print('aliceblue' in hex_colormap)
    # True (all colors in CSS3)
    
    colormap # name (key): rgba (value)
    
    {'aliceblue': [0.9411764705882353, 0.9725490196078431, 
                   1.0, 1.0],
     'antiquewhite': [0.9803921568627451, 0.9215686274509803,
                      0.8431372549019608, 1.0],
     ...
     'yellow': [1.0, 1.0, 
                0.0, 1.0],
     'yellowgreen': [0.6039215686274509, 0.803921568627451,
                     0.19607843137254902, 1.0]}
     
    print('rebeccapurple' in hex_colormap)
    # False (only color added in CSS4)
    

    * on "rebeccapurple": Changes from Colors 3.

    Plot

    The matplotlib docs contain a nice function (plot_colortable) that you can copy/paste to plot a list of named colors. You can pass either one of the dictionaries to this function to get a nice sorted list of colors and their names.

    plot_colortable(colormap) # add `sort_colors=False` for unsorted plot
    plt.show()
    

    Result:

    plot CSS 3 Colors

    Of course, this plot (showing Kivy's 147 named colors) is just the same as the plot already shown in the docs for mcolors.CSS4_COLORS (containing CSS4's 148 colors), the only difference being that my plot is missing "rebeccapurple".