Skip to content Skip to sidebar Skip to footer

Is There A File With Every Color On Python?

I am working on several Pygame projects, although it is inconvenient to copy and paste a few colors that I made from a previous file into the one I am working on next. I was wonder

Solution 1:

You can see all the colors by looking at the dict pygame.color.THECOLORS. However that dict of all the names and color values is large (657 when I am writing this) and so it can be cumbersome to look through and find what you want.

I often find myself trying to find color names and find this snippet very handy:

import pygame
from pprint importpprintcolor_list= [(c, v) for c, v in pygame.color.THECOLORS.items() if'slategrey' in c]
pprint(color_list)

which outputs:

[('darkslategrey', (47, 79, 79, 255)),
 ('slategrey', (112, 128, 144, 255))
 ('lightslategrey', (119, 136, 153, 255))]

I do that in an interactive session to get all the names that include 'slategrey' and then in my actual code I can use the one I want like this:

slategrey = pygame.Color("slategrey")

and then later in the code reference it like this:

screen.fill(slategrey,  pygame.Rect(0, 0, 100, 100))

However, if you really want to see the list of all the colors in pygame.color.THECOLORS, you can look at them here in pygames colordict module where THECOLORS is defined.

Solution 2:

As Thomas Kläger already noted in a comment, there's a list of possible color strings in pygame.

Take a look here to see all those strings and their RGB value.

If you want, you can access this dict via pygame.color.THECOLORS, but you usually don't need to. You can just pass a string with the color name to pygame's Color class, like this:

screen.fill(pygame.Color('red'),  pygame.Rect(  0, 0, 100, 100))
screen.fill(pygame.Color('plum'), pygame.Rect(100, 0, 100, 100))
screen.fill(pygame.Color('pink'), pygame.Rect(100, 0, 100, 100))

Of course there's no list of every possible color, since there are 256^3 = 16.777.216 possible colors in pygame (4.294.967.296 if you include different alpha values).

Post a Comment for "Is There A File With Every Color On Python?"