Colors in Pygame

The color system used in Pygame is a common one used in many computer languages and programs. It's called RGB. The R, G, and B stand for red, green, and blue.

You might have learned in science class that you can make any color by combining or mixing the three primary colors of light: red, green, and blue. That's the same way it works on computers. Each color gets a number from 0 to 255. If all the numbers are 0, there is none of any color, which is completely dark, so you get the color black. If they're all 255, you get the brightest of all three colors mixed together, which is white. If you have something like [255, 0, 0], that would be pure red with no green or blue. Pure green would be [0, 255, 0]. Pure blue would be [0, 0, 255]. If all three numbers are the same, like [150, 150, 150], you get some shade of grey. The lower the numbers, the darker the shade; the higher the numbers, the brighter the shade.

chapter 16 Graphics Colors are given as a list of three integers, each one ranging from 0 to 255.

Color names

Pygame has a list of named colors you can use if you don't want to use the [R, G, B] notation. There are over 600 color names defined. I won't list them all here, but if you want to see what they are, search your hard drive for a file called, and open it in a text editor.

If you want to use the color names, you have to add this line at the start of your program:

from pygame.color import THECOLORS

Then, when you want to use one of the named colors, you'll do it like this (in our circle example):, THECOLORS[MredM],[100,100], 30, 0)

11 iHHT'q nniNn riN in thfhi

W mI

Why 255? The range from 0 to 255 gives us 256 different values for each primary color (red, green, and blue). So, what's special about that number? Why not 200 or 300 or 500?

tTwo hundred and fifty-six is the number of different values you can make with 8 bits. That's all the possible combinations of eight 1s and 0s. Eight bits is also called a byte, and a byte is the smallest chunk of memory that has its own address. An address is the computer's way of finding particular pieces of memory.

It's like on your street. Your house or apartment has an address, but your room doesn't have its own address. A house is the smallest "addressable unit" on the street. A byte is the smallest "addressable unit" in your computer's memory.

They could have used more than 8 bits for each color, but the next amount that makes sense would be 16 bits (2 bytes), because it's not very convenient to use only part of a byte. And it turns out that, because of the way the human eye sees color, 8 bits is enough to make realistic-looking colors.

Because there are three values (red, green, blue), each with 8 bits, that's 24 bits in total, so this way of representing color is also known as "24-bit color." It uses 24 bits for each pixel, 8 for each primary color.

If you want to play around and experiment with how the red, green, and blue combine to make different colors, you can try out the program that was put in the \examples folder when you ran this book's installer. This will let you try any combination of red, green, and blue to see what color you get.

100 Bowling Tips

100 Bowling Tips

Playing bowling with your friends can help you decide if it is indeed the hobby that you want to invest your time on today. Aside from that, it can help you get a better feel of the sport. More importantly, when you play with your friends, it would become a more fun activity, which you can look forward to each week.

Get My Free Ebook


  • adriana
    How to use pygame colordict?
    8 years ago
  • Mhret
    How to use python?
    7 years ago

Post a comment