Game Development Reference
In-Depth Information
Colors in Pygame
11. # set up the colors
12. BLACK = (0, 0, 0)
13. WHITE = (255, 255, 255)
14. RED = (255, 0, 0)
15. GREEN = (0, 255, 0)
16. BLUE = (0, 0, 255)
Table 17-1: Colors and their RGB
values.
There are three primary colors of light: red, green
and blue. By combining different amounts of these
three colors you can form any other color. In Python,
we represent colors with tuples of three integers. The
first value in the tuple is how much red is in the
color. A value of 0 means there is no red in this
color, and a value of 255 means there is a maximum
amount of red in the color. The second value is for
green and the third value is for blue.
Color
RGB Values
Aqua
(0, 255, 255)
Black
(0, 0, 0)
Blue
(0, 0, 255)
Cornflower Blue
(100, 149, 237)
Fuchsia
(255, 0, 255)
Gray
(128, 128, 128)
For example, we will create the tuple (0, 0, 0)
and store it in a variable named BLACK . With no
amount of red, green, or blue, the resulting color is
completely black. The color black is the absence of
any color.
Green
(0, 128, 0)
Lime
(0, 255, 0)
Maroon
(128, 0, 0)
Navy Blue
(0, 0, 128)
Olive
(128, 128, 0)
On line 13, we use the tuple (255, 255, 255)
for a maximum amount of red, green, and blue to
result in white. The color white is the full
combination of red, green, and blue. We store this
tuple in the WHITE variable. (255, 0, 0)
represents the maximum amount of red but no
amount of green and blue, so the resulting color is
red. Similarly, (0, 255, 0) is green and (0, 0,
255) is blue.
Purple
(128, 0, 128)
Red
(255, 0, 0)
Silver
(192, 192, 192)
Teal
(0, 128, 128)
White
(255, 255, 255)
Yellow
(255, 255, 0)
These variable names are in all capitals because they are constant variables. It's just
easier to type BLACK in our code than (0, 0, 0) every time we want to specify the
color black, so we set up these color variables at the start of our program.
If you want to make a color lighter, try adding an equal amount from all three values.
For example, the RGB value for gray is (128, 128, 128) . You can get the RGB
value for a lighter gray by adding 20 to each value to get (148, 148, 148) . You can
get the RGB value for a darker gray by subtracting 20 from each value to get (108,
108, 108) . And you can get the RGB value for a slightly redder gray by adding 20 to
only the red value to get (148, 128, 128) . Table 17-1 has some common colors and
Search WWH ::




Custom Search