Learning LibraryCore Coding LibraryTeens

RGB Color in Python: build palettes and fades in code

A color is data: three integers, 0255, for red, green, and blue. Once color is numbers, you can compute it — fade it, shift it, generate a whole palette in a loop.

The big idea

An RGB color is a tuple (r, g, b) of 0–255 values, so any color is a point you can calculate.

See it in code

1The basics

A color is a tuple of three integers, 0255, for red, green, and blue. Because it's a tuple, you can pull out one channel by index — [0] is red:

python
red = (255, 0, 0)
teal = (0, 128, 128)
print("red:", red)
print("teal:", teal)
print("red channel of red:", red[0])
Run it — a color is just three numbers you can read:
red: (255, 0, 0)
teal: (0, 128, 128)
red channel of red: 255

teal mixes equal green and blue. Once color is numbers in a tuple, you can inspect a channel — and, next, compute one.

2A step further

Here's the key move: generate a color in a loop instead of hand-picking it. Each pass raises the red channel by a fixed step while green and blue stay put:

python
for i in range(8):
    red = 40 + i * 27
    print(i, "->", (red, 60, 200))
Run it — eight colors, computed one step apart:
0 -> (40, 60, 200)
1 -> (67, 60, 200)
2 -> (94, 60, 200)
3 -> (121, 60, 200)
4 -> (148, 60, 200)
5 -> (175, 60, 200)
6 -> (202, 60, 200)
7 -> (229, 60, 200)

The red channel climbs 40, 67, 94 … in even steps while green and blue hold at 60 and 200. Paint each of those colors onto a star and the steady climb becomes a smooth fade.

3In our world

Now render that computed fade. It's the exact 40 + i * 27 from the warm-up, feeding each color to a star across the row:

python
from art import Canvas, star

screen = Canvas.create()
Canvas.fill(screen)

for i in range(8):
    red = 40 + i * 27
    color = (red, 60, 200)
    star(screen, 60 + i * 55, 250, size=44, color=color)
Run it — a computed fade from deep blue toward magenta:
A row of eight stars whose color shifts smoothly as the red channel increases from left to right.

Same numbers you printed above, now as pixels: the color slides predictably because the math does. Since color is just math, you can interpolate between any two colors, brighten by scaling all three, or map a data value to a hue.

The same idea, everywhere

Treating color as numbers powers heatmaps (value → color), theme generators, image filters (tweak every pixel's RGB), and data visualization. The same idea extends to other color spaces like HSV, where 'hue' is a single number you can rotate for rainbow effects.

Try it yourself

Fade a different channel — step green instead of red — and compare. Then interpolate: for a blend factor t from 0 to 1, compute each channel as start + (end - start) * t to morph one color into another.

The common mistake

Letting a channel exceed 255 or go below 0, or using floats. Computed values can overrun the range — 40 + 9 * 27 would be 283. Clamp with min(255, value) and keep channels as int, since a color expects whole numbers.

What it unlocks

Computed color builds on operators and tuples, and drives generative art and palette design.

Want the simpler version? Read the Kids version →