Colour and light

glint · Examples · Tutorials · Gallery

Making colour look good: mixing, cosine palettes, HSV, gamma, tone mapping, glows, grading, blend modes, dithering and the finishing touches.

A free interactive course of 10 steps in glint, a live shader editor in the browser: each step changes a running shader, and ticks itself when you've done its task.

1. Colour is three numbers

A colour on screen is three lights per pixel: red, green and blue, each from 0 (off) to 1 (full). All three full is white, all off is black, red + green is yellow, green + blue is cyan, red + blue is magenta. It's light, not paint: adding makes things brighter.

mix(a, b, t) blends two colours: t = 0 is all a, t = 1 all b, 0.5 halfway. Feed it a position and you get a gradient. Right now the screen is a plain grey ramp from left to right (vec3(uv.x): the same amount of all three lights).

Worked example: mix(vec3(1.0, 0.5, 0.2), vec3(0.3, 0.1, 0.6), 0.5) is (0.65, 0.3, 0.4), a dusky pink between the orange and the purple.

Your turn: Make a sunset, orange at the bottom to purple at the top: vec3 col = mix(vec3(1.0, 0.5, 0.2), vec3(0.3, 0.1, 0.6), uv.y);

2. Palettes from cosines

A whole palette in one line (Inigo Quilez's cosine palette): a + b * cos(6.2832 * (c * t + d)). As t goes from 0 to 1, each of red, green and blue swings up and down along its own cosine wave.

a is the middle brightness, b how far each channel swings, c how many times it cycles, and d shifts each channel's wave. When red, green and blue are out of step with each other you get a rainbow; in step, just bright and dark.

Worked example: with d = (0, 0.33, 0.67), at t = 0 red is at the top of its wave and green and blue a third and two thirds of the way round: a warm red. As t grows, the peaks pass through green then blue.

The strip shows t from 0 to 1. Tiny changes to the four vectors give endless palettes, and t can be anything: distance, angle, noise, time.

Your turn: Make it a rainbow: vec3 d = vec3(0.0, 0.33, 0.67);

3. Hue, saturation and value

RGB is how screens work, not how people think about colour. HSV describes it the way we talk: hue (which colour, going round the rainbow from 0 to 1 and back to red), saturation (how strong: 0 is grey, 1 vivid) and value (how bright).

hsv2rgb converts back to what the screen needs. It's ideal for anything that should cycle through colours: change the hue and the brightness stays the same, which is hard to do directly in RGB.

The wheel here uses the angle around the centre as the hue and the distance as the saturation.

Your turn: Spin the colours: float hue = angle + time * 0.1;

4. Gamma: why your gradients look wrong

A screen doesn't show 0.5 as half the light. It shows about a fifth of it: screen values are stored 'gamma encoded', bunched up in the darks where our eyes notice small differences. Light maths, though (adding lights, blending, shading) only works on real, linear amounts of light.

So do the maths in linear light and encode at the very end: pow(col, vec3(1.0 / 2.2)). Colours you type in are usually already encoded, so turn them linear first with pow(c, vec3(2.2)).

Both halves blend pure red into pure green. The top half mixes the encoded values directly: the middle goes a muddy brown. The bottom half will blend in linear light and then encode: a clean, bright yellow in the middle. That's the difference gamma makes.

Worked example: halfway, the top shows (0.5, 0.5, 0); the bottom computes (0.5, 0.5, 0) in linear light, which encodes to about (0.73, 0.73, 0): much brighter, as real light mixing would be.

Your turn: Encode the bottom half for the screen: col = pow(col, vec3(1.0 / 2.2)); inside the if.

5. Brighter than white: tone mapping

Add up a few lights and values go past 1. The screen just clips them: anything over 1 is the same flat white, so bright areas burn out into blobs and colours go wrong (a bright orange (2, 1, 0.4) clips to (1, 1, 0.4), pale yellow).

Tone mapping squeezes any brightness into 0..1 smoothly, the way a camera does. The simplest is Reinhard: col / (1.0 + col). Dark values barely change (0.1 becomes 0.09), 1 becomes 0.5, 4 becomes 0.8, and nothing ever reaches 1, so highlights keep their shape and their colour.

Usually you scale up first (an exposure) so the mid-tones aren't too dark after squeezing.

Your turn: Tone map instead of clipping: col = col / (1.0 + col);

6. Light that glows

Glow is light that spreads out from a source and fades with distance. A good glow is bright and tight in the middle with a long, soft tail: k / (distance + k), or squared for a faster fall.

Lights add up: each source adds its colour times its glow to whatever is already there. Overlapping red and blue make magenta where they meet, just as real coloured lights do.

Worked example with k = 0.02: on the light (distance 0) the glow is 1; at 0.02 away, 0.5; at 0.2 away, 0.09. A long tail that still lights the room.

Your turn: Add a third, green light: col += glow(p - vec2(0.0, -0.2), vec3(0.3, 1.0, 0.4));

7. Grading: contrast and saturation

Grading is adjusting the finished picture, like a filter on a photo. Two adjustments do most of the work.

Saturation: work out the grey with the same brightness (luma: dot(col, vec3(0.299, 0.587, 0.114)), weighted because green looks brightest to us), then push away from it: mix(vec3(luma), col, s). s = 1 changes nothing, 0 is black and white, above 1 more vivid.

Contrast: push away from middle grey, (col - 0.5) * k + 0.5. Above 1, darks get darker and lights lighter.

The left half is untouched, the right half graded, so you can compare.

Your turn: Make the right half more vivid: float saturation = 1.6;

8. Blend modes

Image editors' layer modes are one-line formulas, and they're just as useful in shaders. With a as the bottom layer and b the top, each from 0 to 1:

multiply:  a * b
screen:    1 - (1 - a) * (1 - b)
add:       a + b
overlay:   multiply in the darks,
           screen in the lights

Multiply can only darken (white leaves things alone, black makes black): shadows, stains, ink. Screen can only brighten (black leaves things alone): light, glows, fog. Add brightens more harshly and can go past 1.

Here a blue stripe pattern is the top layer over an orange glow, multiplied. Switch it to screen and see the stripes become light instead of shade.

Your turn: Screen the layers instead: vec3 col = 1.0 - (1.0 - a) * (1.0 - b);

9. Banding and dithering

Screens have 256 levels per channel. In a slow, dark gradient, 256 levels aren't enough: you can see the steps as stripes (banding), especially across a big dark sky.

The fix is surprisingly simple: add a tiny bit of random noise, less than one level (1/255), before the picture is turned into those levels. Each pixel rounds up or down at random, the steps break up, and from a normal distance the eye averages them back into a smooth gradient. That's dithering.

The gradient here is deliberately slow: a dusky blue that changes by only about fifteen levels from the middle to the corners, so each level is a wide ring. Look for the rings (closer to the screen helps), then add the dither line and look again: they melt into a smooth fade.

Your turn: Dither it: add col += (hash(gl_FragCoord.xy) - 0.5) / 255.0; before the output.

10. The finishing touches

A few cheap steps at the very end make almost any shader look more polished. A vignette darkens the corners, pulling the eye to the middle: col *= 1.0 - 0.5 * dot(p, p) (p is 0 in the middle, larger towards the corners).

Then tone map, grade, encode for the screen and dither, in that order: tone mapping and grading work on linear light, the encode comes after them, and the dither goes last, right before the output.

This scene already does all of that except the vignette. Everything in this course, in one place.

Your turn: Add a vignette: col *= 1.0 - 0.5 * dot(p, p); before the tone mapping.

Other courses: Basics · Medium · Advanced · Maths for shaders · Raymarching, step by step · Matrices, from scratch · Sound you can see · Noise from scratch · Passes and feedback · Skies and reflections · Vertex shaders, step by step · Raymarching 2: glow, fog and light · Raymarching 3: worlds · Raymarching 4: modelling, materials and motion · Fractals, step by step · Vertex shaders 2: lines, particles and moving meshes · Fractals 2: further in · Vertex shaders 3: living geometry · Post-processing · Surfaces: from vertex to fragment · Blur and glow