RGB color: mix any color from three numbers
In code, a color is just three numbers: how much red, green, and blue to mix. Turn the numbers up and down, and you can make any color you like.
The big idea
An RGB color is three numbers — red, green, blue — each from 0 to 255.
See it in code
A color is three numbers in ( ): how much red, green, and blue. Turn one all the way up to 255 and the other two to 0, and you get a pure color:
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
print("red is", red)
print("green is", green)
print("blue is", blue)red is (255, 0, 0) green is (0, 255, 0) blue is (0, 0, 255)
255 means 'all the way up' and 0 means 'none'. Pure red is all red and nothing else.
The fun part is mixing. Turn up two channels at once, or all three, and brand-new colors appear:
yellow = (255, 255, 0)
white = (255, 255, 255)
purple = (128, 0, 128)
print("red + green =", yellow)
print("all three full =", white)
print("some red + some blue =", purple)red + green = (255, 255, 0) all three full = (255, 255, 255) some red + some blue = (128, 0, 128)
Red and green together make yellow. All three full makes white. Change the three numbers and you can make millions of colors.
Now put color on the canvas. We take the three pure colors and hand each one to a star with color=:
from art import Canvas, star
screen = Canvas.create()
Canvas.fill(screen)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
star(screen, 130, 250, size=90, color=red)
star(screen, 250, 250, size=90, color=green)
star(screen, 370, 250, size=90, color=blue)
Each star's color is just its three numbers. Swap in (255, 255, 0) from the mixing warm-up and that star turns yellow!
Every color on every screen is made this way. Your games, your art, your favorite apps — all their colors are red, green, and blue numbers mixed together. Learn the three numbers and you can make millions of colors.
Try it yourself
Change red to (255, 165, 0) for orange. Then try (128, 0, 128) for purple, and (255, 255, 255) — that's all three at full, which makes white.
The common mistake
Going past 255. Each number only goes up to 255, not higher. (300, 0, 0) is out of range — the biggest red is 255.
What it unlocks
Colors as numbers build on data types, and let you use every hue in the canvas and pygame drawing.