问题
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 wondering if there is code I can make into a file I import or code I can download that has all (or most) of the colors in python that work in Pygame. Thanks!
import pygame
pygame.init()
gameDisplay=pygame.display.set_mode((600,600))
pygame.display.set_caption("plz help")
white=(255,255,255)#r,g,b
black=(0,0,0)
red=(255,0,0)
green=(0,255,0)
blue=(0,0,255)
aquamarine2=(118,238,198)
It works out and provides variables with colors, but I would love to have more variety and an easier and cleaner way to access this. Online, I have only found ways to find specific colors, but nothing in bulk and in format to copy and paste.
回答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 import pprint
color_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.
回答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).
回答3:
I found another website https://www.webucator.com/blog/2015/03/python-color-constants-module that can be used for a lot more colors. On the bottom of the page they have a long list of color combinations that can be copied.
来源:https://stackoverflow.com/questions/58795570/is-there-a-file-with-every-color-on-python