Python上是否有每种颜色的文件? [英] Is there a file with every color on Python?

查看:90
本文介绍了Python上是否有每种颜色的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从事多个Pygame项目,尽管将​​我从上一个文件中制作的几种颜色复制并粘贴到下一个正在研究的颜色中很不方便。我想知道是否可以将要写入到文件中的代码或可以下载的代码包含在Pygame中可以使用的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 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.

推荐答案

您可以通过查看字典 pygame.color.THECOLORS 查看所有颜色。但是,所有名称和颜色值的dict很大(当我写这篇文章时为657),因此浏览并查找所需内容可能很麻烦。

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)

输出以下内容:

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

我在一个交互式会话中这样做,以获取所有包含'slategrey'的名称,然后在我的实际代码中可以这样使用我想要的名称:

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))

但是,如果您真的想查看所有colo的列表rs,您可以在pygames colordict模块,其中定义了THECOLORS。

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.

这篇关于Python上是否有每种颜色的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆