5

I don't like RGB, I use HEX instead. I'm new to Python and this is how my code is looking like; How do I use HEX. I'm sorry for english I'm not from USA.

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()
Orang O' Man
  • 67
  • 1
  • 1
  • 4

1 Answers1

11

pygame.Color supports hex arguments. So you can do this:

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

This automatically converts your color to RGBA values. So if you went to print the color, you would see:

(126, 192, 238, 255)
sam-pyt
  • 1,027
  • 15
  • 26
  • 2
    Also note that pygame comes with a dict of predefined colors, and you can use something like `pygame.Color('white')`, `pygame.Color('lightgrey')` etc etc – sloth Nov 12 '18 at 11:51
  • Is the performance impact of using hex arguments any significant? – Gabriel Araujo Aug 01 '22 at 20:25