Colors

Every shape in a 3D scene starts out grey — and it’s up to you to bring it to life. In Python, colors are written as hex codes: a # sign followed by six digits that blend red, green, and blue light together.

In this lesson you’ll learn how hex codes work and use them to paint the sky, the ground, and every shape in your scene.

Open In Jupyter K-12

A Colorless World

Run the cell below. Notice anything missing? The sphere appears, but it’s a flat grey — and the sky is almost black. That’s what a scene looks like with no colors applied.

import scene3d

scene = scene3d.Scene()
scene.set_ground(length=10, width=10)

sphere = scene3d.Shapes.Sphere(diameter=1, segments=16)
sphere.set_position(0, 0.5, 0)
scene.add(sphere)

scene.run()

How Hex Colors Work

Every color is made from three ingredients: Red, Green, and Blue. A hex color code packs all three into one short string:

#e94560

The # marks the start. After that come three pairs of digits:

Pair Controls 00 means… ff means…
e9 Red no red full red
45 Green no green full green
60 Blue no blue full blue

The digits go from 00 (zero) to ff (which equals 255 in regular numbers — the maximum).

Here are some colors made from pure combinations:

Color Hex code Why?
Red #ff0000 Full red, no green, no blue
Green #00ff00 No red, full green, no blue
Blue #0000ff No red, no green, full blue
Yellow #ffff00 Full red + full green mixed
Cyan #00ffff Full green + full blue mixed
White #ffffff All three at maximum
Black #000000 All three at zero

Coloring the Sky and a Shape

Two function calls change the scene completely: - scene.set_sky('#color') — paints the background sky - sphere.set_color('#color') — paints the sphere

Run the cell below to see the difference from the grey world above.

import scene3d

scene = scene3d.Scene()
scene.set_sky('#87ceeb')
scene.set_ground(length=10, width=10)

sphere = scene3d.Shapes.Sphere(diameter=1, segments=16)
sphere.set_color('#e94560')
sphere.set_position(0, 0.5, 0)
scene.add(sphere)

{ “question_type”: “multiple_choice”, “question”: “What color does the hex code #ff0000 produce?”, “options”: [ { “key”: “a”, “text”: “Blue” }, { “key”: “b”, “text”: “Green” }, { “key”: “c”, “text”: “Red” }, { “key”: “d”, “text”: “White” } ], “answer”: “c”, “submitted_answer”: “” }

{ “question_type”: “true_false”, “question”: “The hex code #000000 produces white.”, “answer”: “False”, “submitted_answer”: “” }

Any Shape Can Be Colored

set_color works on every 3D shape — spheres, boxes, and cylinders all use the same function. The scene below places three different shapes side by side, each with its own color.

import scene3d

scene = scene3d.Scene()
scene.set_sky('#1a1a2e')
scene.set_ground(length=12, width=12)

sphere = scene3d.Shapes.Sphere(diameter=1, segments=16)
sphere.set_color('#e94560')
sphere.set_position(-3, 0.5, 0)
scene.add(sphere)

box = scene3d.Shapes.Box(width=1, height=1, depth=1)
box.set_color('#4488ff')
box.set_position(0, 0.5, 0)
scene.add(box)

cylinder = scene3d.Shapes.Cylinder(diameter=1, height=1.5, tessellation=16)
cylinder.set_color('#44cc88')
cylinder.set_position(3, 0.75, 0)
scene.add(cylinder)

{ “question_type”: “multiple_choice”, “question”: “Which hex code produces yellow?”, “options”: [ { “key”: “a”, “text”: “#ff00ff” }, { “key”: “b”, “text”: “#00ffff” }, { “key”: “c”, “text”: “#ffff00” }, { “key”: “d”, “text”: “#ff0000” } ], “answer”: “c”, “submitted_answer”: “” }

Design Your Own Palette

Use the dropdowns below to pick colors for the sky, the sphere, and the box. Change the values and run the cell to see your choices in action.

import scene3d

SKY_COLOR    = '#1a1a2e' #@param ['#1a1a2e', '#87ceeb', '#ff6b35', '#0f3460', '#2d5a27']
SPHERE_COLOR = '#e94560' #@param ['#e94560', '#4488ff', '#44cc88', '#f5a623', '#aa44ff']
BOX_COLOR    = '#f5a623' #@param ['#e94560', '#4488ff', '#44cc88', '#f5a623', '#aa44ff']

scene = scene3d.Scene()
scene.set_sky(SKY_COLOR)
scene.set_ground(length=10, width=10)

sphere = scene3d.Shapes.Sphere(diameter=1, segments=16)
sphere.set_color(SPHERE_COLOR)
sphere.set_position(-2, 0.5, 0)
scene.add(sphere)

box = scene3d.Shapes.Box(width=1, height=1, depth=1)
box.set_color(BOX_COLOR)
box.set_position(2, 0.5, 0)
scene.add(box)

Coloring the Ground

The sky gets its color from set_sky. But what about the ground? The built-in ground plane doesn’t have a color setting — so instead, we can place a wide, flat Box right at ground level and color that.

This turns out to be a useful trick: a Box with a small height and large width and depth becomes a colored floor for the whole scene.

import scene3d

scene = scene3d.Scene()
scene.set_sky('#0f3460')

floor = scene3d.Shapes.Box(width=12, height=0.1, depth=12)
floor.set_color('#2d5a27')
scene.add(floor)

sphere = scene3d.Shapes.Sphere(diameter=1, segments=16)
sphere.set_color('#e94560')
sphere.set_position(-2, 0.55, 0)
scene.add(sphere)

cylinder = scene3d.Shapes.Cylinder(diameter=1, height=1.5, tessellation=16)
cylinder.set_color('#f5a623')
cylinder.set_position(2, 0.8, 0)
scene.add(cylinder)

{ “question_type”: “true_false”, “question”: “In a hex color code, the value ff equals 255 in regular decimal numbers.”, “answer”: “True”, “submitted_answer”: “” }

{ “question_type”: “multiple_choice”, “question”: “What color does #00ffff produce?”, “options”: [ { “key”: “a”, “text”: “Purple” }, { “key”: “b”, “text”: “Yellow” }, { “key”: “c”, “text”: “Orange” }, { “key”: “d”, “text”: “Cyan (blue-green)” } ], “answer”: “d”, “submitted_answer”: “” }

{ “question_type”: “freeform”, “question”: “What is the name of the function used to set the sky color on a scene?”, “answer”: “set_sky”, “submitted_answer”: “” }

Look at the color mixing table in this lesson (red + green = yellow, green + blue = cyan, etc.).

Can you predict what red + blue mixed together would make? Write your prediction, then try it — set a sphere’s color to #ff00ff and see if you were right!