LearnFirst steps
Lesson 5 of 30 · 55 min
Mixing colors
Design color with mix and palettes, and layer shapes over a background
In this lesson you will
- Blend numbers and colors with
mix(a, b, t)and build multi-color gradients - Layer shapes over a background with
col = mix(col, shapeColor, mask), controlling order and translucency - Know how multiply, screen and add differ as ways of combining colors
- Turn hues with
hsv2rgband design color schemes with the four parameters ofpalette - Get a rough idea of what gamma correction,
pow(col, vec3(1.0 / 2.2)), is for
01Blending with mix
You often want a color somewhere between two others. That is what mix(a, b, t) is for: it returns a when t is 0, b when t is 1, and something in between otherwise. Under the hood it computes a * (1.0 - t) + b * t.
It works on numbers and colors alike. mix(10.0, 20.0, 0.25) is 12.5. Mixing red (1, 0, 0) and blue (0, 0, 1) with t = 0.5 gives exactly half of each, (0.5, 0, 0.5): purple. The color1 * (1.0 - t) + color2 * t pattern you wrote in earlier exercises was mix all along.
1
2
3
4
5
6
uv.x to 0.5 and you get the single color halfway between. What about smoothstep(0.4, 0.6, uv.x) or uv.x * uv.x?What you feed into t decides how the colors blend. Use a shaping function from Shaping functions as t to sharpen the transition or push it to one side.
Watch out
Keep t between 0 and 1. mix doesn't clip t, so above 1 you overshoot past b, and below 0 you land on the far side of a. If t might stray, pass it through clamp(t, 0.0, 1.0).
02Designing gradients
For three or more colors, stack mixes. A sunset sky might be orange near the horizon, pink a little higher and deep navy at the top. The first mix goes from orange to pink; the second mixes that result toward navy. Each t comes from smoothstep turning a range of heights in uv.y into 0–1.
1
2
3
4
5
6
7
8
smoothstep(0.3, 0.9, uv.y) to smoothstep(0.6, 0.9, uv.y) and the pink band widens. What if low becomes vec3(1.0, 0.85, 0.4)?The two ranges (0.0–0.4 and 0.3–0.9) overlap slightly, so there is no visible seam above or below the pink. When choosing colors, vary the brightness too: a darker top and a brighter horizon give the sky depth.
03Layering shapes
The single most important use of mix is laying a shape over a background. It always looks like this:
1
col = mix(col, shapeColor, mask);
The mask is 1 inside the shape and 0 outside (the 1.0 - smoothstep(-px, px, d) from Circles and distance). Where the mask is 0, col stays as it was; where it is 1, col is replaced by shapeColor. On the edge the mask is around 0.5, so background and shape blend half and half, giving a smooth edge.
In Circles and distance we layered by adding colors, but adding lets the background bleed into the shape (yellow added to a blue background turns whitish). mix puts the shape's color down exactly as it is.
1
2
3
4
5
6
7
8
9
mix(col, vec3(1.0, 0.9, 0.6), sun) with col + vec3(1.0, 0.9, 0.6) * sun. How does the sun's color change? What about sun * 0.5 instead of sun?Shrink the mask, as with sun * 0.5, and the shape becomes translucent. The mask is the fraction of shape color to use, so it acts just like opacity: at 0.5 the shape and the background mix half and half.
04Layer order
Chain several col = mix(col, …) lines and each later line lands on top. It is like painting on paper: you work from the back to the front. The picture below goes sky → sun → far hill → near hill. The hills are huge circles centered far below the screen.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
far to far * 0.6 and the far hill turns hazyShadows and outlines work the same way: draw them first, as lower layers. A shadow is the shape itself, nudged a little, blurred with a wide smoothstep, and made translucent with a smaller mask. An outline is a slightly larger circle painted in a dark color before the shape.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
shadow * 0.8 instead of shadow * 0.35. Swap the "1. shadow" and "2. outline" lines: what changes?05Multiply, screen and add
mix replaces one color with another. There are also ways to combine the lower and upper colors with arithmetic — the same thing as blend modes in image editors. Call the lower color a and the upper color b.
- Multiply,
a * b… darkens. Multiplying by black (0) gives black; by white (1) changes nothing. Like stacking colored film or ink - Screen,
1.0 - (1.0 - a) * (1.0 - b)… brightens but never exceeds 1. Screening white gives white; screening black changes nothing. Like two projectors shining on the same spot - Add,
a + b… adds light directly. The brightest of the three, and anything over 1 blows out to white
Compare them with numbers. For a = 0.5 and b = 0.5, multiply gives 0.25, screen gives 1 - 0.5 × 0.5 = 0.75, and add gives 1.0. In the playground below, the same orange is laid three ways over a background that is dark at the bottom and light at the top.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
b to vec3(0.3, 0.6, 1.0). With white vec3(1.0) or black vec3(0.0) as b, which circles disappear?The multiply circle sinks to black over the dark bottom of the background and only shows orange near the light top. The screen circle is orange at the bottom and nearly white at the top. The add circle goes past 1 near the top and blows out to white. Multiply suits shadows and ink; screen and add suit light.
In Circles and distance you added light — red, green and blue — and saw overlaps turn yellow and white. Now do the opposite: multiply inks — cyan, magenta and yellow — on white paper. mix(vec3(1.0), inkColor, mask) is white outside the circle (multiplying by it changes nothing) and the ink color inside.
1
2
3
4
5
6
7
8
9
10
11
12
13
vec3(0.0, 1.0, 1.0)?06Turning hues with HSV
RGB is about amounts of red, green and blue light, which makes adjustments like "a bit bluer, same brightness" awkward. That is why people often describe color with hue (H), saturation (S) and value (V) instead — HSV.
- Hue H … which color. 0 to 1 goes once around the color wheel: 0 is red, about 0.17 yellow, 0.33 green, 0.5 cyan, 0.67 blue, 0.83 magenta, and 1 is red again
- Saturation S … vividness. 0 is washed out to gray, 1 is fully vivid
- Value V … brightness. 0 is black
GLSL has no HSV function, so paste the following hsv2rgb into your code. Pass it vec3(H, S, V) and it returns an RGB color. This course always uses it with exactly this name and body.
1
2
3
4
vec3 hsv2rgb(vec3 c) {
vec3 k = clamp(abs(mod(c.x * 6.0 + vec3(0.0, 4.0, 2.0), 6.0) - 3.0) - 1.0, 0.0, 1.0);
return c.z * mix(vec3(1.0), k, c.y);
}
It is built entirely from tools in Shaping functions. mod turns the hue into a repeating 0–6, and abs(… - 3.0) - 1.0 with clamp shapes it into a trapezoid wave between 0 and 1. Red, green and blue each get the wave shifted by a third of a turn, so as the hue advances the three channels take turns. The last line goes from white at zero saturation to k at full saturation (with mix), then multiplies by the value.
1
2
3
4
5
6
7
8
9
10
11
uv.x to uv.x + time * 0.1 and the color wheel scrolls. What if sat is 1.0 and 0.95 becomes uv.y?The great thing about HSV is that adding something to the hue changes the color while keeping saturation and brightness the same. Below, the hue turns a little with distance from the center, so the concentric stripes form a smooth rainbow.
1
2
3
4
5
6
7
8
9
10
11
12
13
0.65 to 0.3 or 1.0. With p.x * 0.3 instead of d * 0.8, how are the colors arranged now?07Cosine palettes
One more handy function builds whole color schemes from a few numbers: the cosine palette, popularized by Inigo Quilez. This course always uses it with this name and body.
1
2
3
4
5
#define TAU 6.28318531
vec3 palette(float t, vec3 a, vec3 b, vec3 c, vec3 d) {
return a + b * cos(TAU * (c * t + d));
}
t is a number from 0 to 1, and the color changes as t advances. Each of red, green and blue rides its own cosine wave up and down. The four vec3s set the shape of those waves (x for red, y for green, z for blue).
a… the middle height of each wave: overall brightnessb… how far each wave swings: bigger means stronger contrastc… how many times each wave repeats while t goes from 0 to 1d… where each wave starts (its phase). Offsetting r, g and b from each other makes the hue turn
With a = b = (0.5, 0.5, 0.5) and c = (1, 1, 1), each channel swings once between 0 and 1. With d = (0, 0, 0) all three move together, giving grays from white to black and back. Offset them by thirds, d = (0.0, 0.33, 0.67), and you get a rainbow. The playground below shows the palette on top and graphs the r, g and b waves underneath.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
d to vec3(0.0), then to vec3(0.0, 0.1, 0.2). With c at vec3(2.0), how many times do the waves repeat?Compare with the graph to see why each color appears where it does: where only red is high you get red, red and green high gives yellow, and all three high is nearly white. When you find a combination you like, note the numbers down. Here are some popular ones:
1
2
3
4
5
6
7
8
9
10
11
// in order: a, b, c, d
// rainbow
vec3(0.5), vec3(0.5), vec3(1.0), vec3(0.0, 0.33, 0.67)
// warm and cool
vec3(0.5), vec3(0.5), vec3(1.0), vec3(0.0, 0.1, 0.2)
// yellow to deep teal
vec3(0.5), vec3(0.5), vec3(1.0, 1.0, 0.5), vec3(0.8, 0.9, 0.3)
// pop
vec3(0.5), vec3(0.5), vec3(2.0, 1.0, 0.0), vec3(0.5, 0.2, 0.25)
// soft reds and greens
vec3(0.8, 0.5, 0.4), vec3(0.2, 0.4, 0.2), vec3(2.0, 1.0, 1.0), vec3(0.0, 0.25, 0.25)
Palettes aren't only for full-screen gradients; they are great for handing out colors to several shapes. Take five colors at t steps of 0.2 and you have five colors that sit well together. Take the background from the same palette, darkened, and the whole picture feels unified.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
pal, change vec3(0.0, 0.15, 0.3) to vec3(0.0, 0.33, 0.67). How do the five circles change? Set shift to 0.0 to stop the colors drifting08A little about gamma
Finally, a quick look at how color numbers relate to brightness on screen. A screen does not show the number 0.5 as half the light. It emits roughly 0.5 to the power of 2.2 — about 0.22 of the light. Our eyes are more sensitive to differences in the dark, so the 0–1 range is spent more finely on dark shades. This mismatch between numbers and light is called gamma.
Most of the time you can ignore it: colors you picked by eye are already in these screen numbers. It matters when you want to compute with amounts of light. Mix red (1, 0, 0) and green (0, 1, 0) half and half and you get (0.5, 0.5, 0), but on screen each channel emits only about 0.22 of its light, so the middle looks dark and muddy.
To mix as light, convert to light amounts with pow(color, vec3(2.2)) before mixing, then convert back to screen numbers with pow(color, vec3(1.0 / 2.2)) afterward. That final step is called gamma correction.
1
2
3
4
5
6
7
8
9
10
11
12
vec3(0.1, 0.2, 1.0) and yellow vec3(1.0, 0.9, 0.1) as a and bTip
Gamma correction matters most when you add up lots of light, as in glows and 3D lighting. This course covers it properly in Light and finishing, together with taming over-bright light, and the 3D lessons finish with pow(col, vec3(1.0 / 2.2)). Until then, colors picked by eye are fine as they are.
Watch out
As you saw in Shaping functions, pow must not see negative numbers. If a color might go negative, write pow(max(col, 0.0), vec3(1.0 / 2.2)).
09Exercises
From here on, aim for pictures that are a pleasure to look at. Layer order and color choice are what count.
A fried egg
Draw a fried egg on a blue plate. Make the egg white a slightly lumpy shape by joining three circles with min. Put the yolk on top, a small highlight on the yolk's upper left, and a soft, translucent shadow under the white toward the lower right.
1
2
3
4
5
6
7
8
9
10
Design a sunset palette
Tune palette's a, b, c and d to make a sunset palette: deep blue at t = 0, purple at t = 0.5 and orange at t = 1. Use the graph in the bottom half to watch the r, g and b waves as you adjust.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
A ringed planet
Draw a striped planet with a ring around it. The upper half of the ring passes behind the planet and the lower half in front; show this with layer order. Pick the planet's stripe colors with palette, and darken the lower right as if light were coming from the upper left.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
10Recap
mix(a, b, t)blends a and b by t; keep t in 0–1 and shape it with shaping functions- Layer shapes with
col = mix(col, shapeColor, mask): later lines go on top, and a smaller mask means translucent - Multiply darkens (shadow, ink), screen brightens without passing 1, and add piles up light (and blows out)
hsv2rgbbuilds color from hue, saturation and value; turning the hue keeps the vividness steadypalette(t, a, b, c, d)sets a scheme with fourvec3s: a brightness, b contrast, c repeats, d phase- Screen numbers aren't amounts of light (gamma); if you compute in light, finish with
pow(col, vec3(1.0 / 2.2))
Next time we use time to set shapes in motion. With back-and-forth swings, orbits, blinking and easing, you'll bring the pictures you've drawn so far to life.
Finished lessons get a mark in the course contents