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

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

问题描述

我正在处理几个 Pygame 项目,尽管将​​我从前一个文件制作的一些颜色复制并粘贴到我正在处理的下一个文件中很不方便.我想知道是否有代码可以制作成我导入的文件或我可以下载的代码,这些代码具有 Python 中所有(或大部分)可在 Pygame 中使用的颜色.谢谢!

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.

推荐答案

您可以通过查看 dict pygame.color.THECOLORS 来查看所有颜色.然而,所有名称和颜色值的字典很大(我写这篇文章时为 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))

但是,如果你真的想在 pygame.color.THECOLORS 中看到所有颜色的列表,你可以在 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天全站免登陆