Learning LibraryCore Coding LibraryKids

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

1Start simple

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:

python
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
print("red is", red)
print("green is", green)
print("blue is", blue)
Run it — one channel up, two channels off:
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.

2A step further

The fun part is mixing. Turn up two channels at once, or all three, and brand-new colors appear:

python
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)
Run it — mixed channels make new colors:
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.

3In our world

Now put color on the canvas. We take the three pure colors and hand each one to a star with color=:

python
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)
Run it — three numbers make each pure color:
Three stars in a row on a dark canvas: a red one, a green one, and a blue one.

Each star's color is just its three numbers. Swap in (255, 255, 0) from the mixing warm-up and that star turns yellow!

The same idea, everywhere

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.

Older, or want more depth? Read the Teens version →