Skip to main content
 首页 » 编程设计

python之如何在 python 中使用十六进制值而不是 RGB 值。 Python 2.7

2025年05月04日16unruledboy

我不喜欢 RGB,我改用 HEX。我是 Python 的新手,这就是我的代码的样子;我如何使用十六进制。对不起,我不是来自美国的英语。

import pygame 
 
black = (0,0,0) 
white = (255,255,255) 
blue = ("#7ec0ee") 
 
pygame.init() 
 
size = 1024,768 
screen = pygame.display.set_mode(size) 
pygame.display.set_caption("Code for Stack") 
 
done = False 
clock = pygame.time.Clock() 
 
while not done: 
    for event in pygame.event.get(): 
      if event.type == pygame.QUIT: 
        done = True 
 
      screen.fill(blue) 
 
      pygame.display.flip() 
      clock.tick(60) 
 
pygame.quit() 

请您参考如下方法:

pygame.Color 支持十六进制参数。所以你可以这样做:

blue = pygame.Color("#7ec0ee") 
screen.fill(blue) 

这会自动将您的颜色转换为 RGBA 值。所以如果你去打印颜色,你会看到:

(126, 192, 238, 255)