Title: Display Pygame's named colors in Python
This example displays color swatches for Pygame's 665 named colors. It performs three main tasks: drawing text, drawing color swatches, and running the event loop.
Drawing Text
To draw the colors' names, the program uses the draw_text function described in the post Draw aligned text with Python and Pygame. See that post for an explanation of that function.
Color Swatches
The program uses the follow Swatch class to draw a single color swatch.
class Swatch:
def __init__(self, name, color_components, rect, font):
self.name = name
self.color = pygame.Color(color_components)
self.rect = rect
self.font = font
if self.color.r + self.color.g + self.color.b >= 255:
self.text_color = pygame.Color('black') # Black for light colors.
else:
self.text_color = pygame.Color('white') # White for dark colors.
def draw(self, surface):
'''Draw the swatch.'''
pygame.draw.rect(surface, self.color, self.rect)
draw_text(surface, self.name, self.rect.center, 'c', self.font,
self.text_color)
The class's constructor saves the color's name and color components (the red, green, and blue components). It also saves the rectangle where it should later draw the swatch and the font it should use to display the color's name.
After saving the basic values, the code calculates the sum of the color components and uses that value to set the swatch's text_color property. It makes the text black for light colors and white for dark colors.
The draw method fills the swatch's rectangle with its color. It then calls draw_text to draw the color's name centered in the rectangle.
The main program uses the following make_swatch function to create the color swatches.
def make_swatches(width, height):
'''Make and return a list of color swatches.'''
# See how many colors there are.
num_colors = len(pygame.color.THECOLORS)
# Figure out how many rows and columns we need.
row_hgt = 20
num_rows = height // row_hgt
num_cols = math.ceil(num_colors / num_rows)
col_wid = width / num_cols
# Make a font.
font = pygame.font.SysFont('Calibri', 12)
# Make the swatches.
swatches = []
x = 0
y = 0
for key, value in pygame.color.THECOLORS.items():
rect = pygame.Rect(x, y, col_wid, row_hgt)
swatches.append(Swatch(key, value, rect, font))
y += row_hgt
if y + row_hgt > height:
y = 0
x += col_wid
return swatches
The key to this function, and to finding Pygame's named colors, is the pygame.color.THECOLORS dictionary. The function first gets the number of colors and then uses that, together with the window's dimensions, to figure out how many rows and columns it will use to draw the color swatches. It creates a font and then loops through the dictionary entries.
For each entry, the code creates a swatch and adds it to the swatches list. It then updates the X and Y coordinates where the next swatch should go.
After it finishes building the swatches, the function returns the swatches list.
Event Loop
The following code shows the main program, which includes the Pygame event loop.
# Initialize Pygame.
pygame.init()
# Set up the window.
width = 1500
height = 700
surface = pygame.display.set_mode((width, height))
pygame.display.set_caption('Pygame Colors')
# Make the swatches.
swatches = make_swatches(width, height)
running = True
while running:
for event in pygame.event.get():
# If the user clicks the close button, quit.
if event.type == pygame.QUIT:
running = False
# Set background color.
surface.fill((255, 255, 255))
# Draw the color swatches.
for swatch in swatches:
swatch.draw(surface)
# Update the display.
pygame.display.update()
# Quit Pygame
pygame.display.quit()
pygame.quit()
This code initializes Pygame and sets up the window. It calls make_swatches to make the swatches list and then enters its event loop.
The loop first checks for QUIT messages and, it if sees one, sets running to False to end the loop.
The code then fills the window's background with white and loops through the swatches making them draw themselves. Finally, the code calls update to make the display show what has been drawn.
After the event loop ends, the program shuts down the display and the closes Pygame.
Conclusion
This program displays labeled samples of Pygame's named colors. The 12 point font is about the smallest one that looks good, but there are still several color names that are too long to fit on their swatches. You can still mostly figure them out, though with the possible exception of the worst offender: lightgoldenrodyellow.
Download the example to experiment with it and to see additional details.
|