Shaders · Chapter 4 of 11
Shaders: Colour
Written for version 2.7.0
A colour is three numbers. Make colours, take them apart, mix, ramp, adjust and blend them with the Color, Channels and Blend nodes.
The last chapter bent a single value into edges, bands, curves, waves and repeats, and showed it as a brightness from black to white. This chapter adds colour. The good news is that there is almost nothing new to learn: a colour is three of those values side by side, and every node you have met works on all three at once.
Edges, bands, curves, waves and repeats from one value.
A colour is three numbers
A screen makes every colour out of three lights: red, green and blue. So a colour in the graph is three numbers, one for each light, and each number is a brightness just like the values of the last chapter: 0 is off, 1 is fully on. The three numbers are called channels.
- All three at 0 is black. All three at 1 is white.
- Three equal numbers are always a grey. That is why a single value showed as black, grey and white in the last chapter: wired into Color, one value fills all three channels with the same number.
- Red and green make yellow, green and blue make cyan, red and blue make magenta. Light adds up, so mixing more of it gets brighter, not muddier as paint does.
The graph has no special type for colour. A colour travels as a Vector 3, a bundle of three numbers, and its ports have a yellow dot. The app does not care whether those three numbers mean red, green and blue or something else, which is why a node called Construct Vector3 makes colours and a node called Split Vector takes them apart.
Making a colour
There are two ways to make a colour. The Color node, in the Parameters folder, is the one you pick by eye: tap its colour well in the inspector and choose. Construct Vector3, in the Channels folder, is the one you build by number, and each of its three inputs can take a wire, so a channel can come from any value in the graph.
- Every Color node is also listed in the material panel under Graph Inputs, as the graph chapter explained, so its colour can be changed without opening the graph. Type a Label to name its row.
- Use Construct Vector3 when a channel should come from a value: a gradient in red, a wave in blue.
- The Color node has three outputs. RGB is the colour and is the one you will use nearly every time. Alpha is its opacity as a single value. RGBA is all four numbers together.
- RGBA is a Vector 4 and will not attach to a colour input, which wants a Vector 3. If a wire from a Color node refuses to land, you have probably dragged from RGBA: use RGB.
- Typed numbers look brighter than you might expect: 0.5 in all three inputs of Construct Vector3 is a light grey, for the reason given in the last chapter under "Half is not half as bright". The colour you pick in a Color node is the colour you see.
Taking a colour apart
Going the other way is just as useful. Split Vector, in the Channels folder, takes a colour in and hands out its numbers one at a time, on outputs X, Y and Z. Each is a plain value again, ready for any node from the last chapter.
- Split Vector has a fourth output, W. A colour has only three numbers, so ignore it. It is there for Vector 4.
- A single channel wired into Color shows as grey, because one value fills all three channels. White means a lot of that channel, black means none.
- Swizzle reorders channels without splitting them. Swizzle 3 gives three numbers out and has three menus, X, Y and Z, each choosing which incoming number goes there. Swizzle 1, 2 and 4 give one, two or four numbers out.
- Split followed by Construct Vector3 is the long way to do what Swizzle does, but it lets you change a channel on the way through: split a colour, put One Minus on X, and build it again.
Mixing two colours
Mix (Lerp) from the last chapter blended between two values. Wire two colours into A and B and it blends between colours, because it does the same sum on each of the three channels. T is still a single value: 0 gives A, 1 gives B.
- Anything that makes a value can drive T. A Gradient gives a fade, a Wave gives stripes of two colours, a Step gives two flat areas with a hard line between them.
- This is how every shape in the next chapter gets its colour: the shape is a value of 0 or 1, and it goes into T.
- The middle of a mix between two strong colours can look dull or grey. If that bothers you, use a Color Ramp with a third colour placed in the middle.
Multiply and add
Multiply and Add also work channel by channel: red with red, green with green, blue with blue. They do opposite things to brightness. Multiplying colours whose channels are between 0 and 1 can only darken, which suits shadows, tints and masks. Adding can only lighten, which suits glows and highlights.
| You do this | And get this |
|---|---|
| Multiply by white | No change. White is 1 in every channel |
| Multiply by a grey | A darker picture. Every channel is scaled down by the same amount |
| Multiply by a colour | A tint. Each channel is scaled by a different amount, like looking through coloured glass |
| Multiply by black | Black |
| Add black | No change. Black is 0 in every channel |
| Add a grey | A lighter, more washed out picture |
| Add a colour | A lighter picture pushed towards that colour, like shining a coloured light on it |
When Multiply is working on a colour, the single number you can type into B in the inspector fills only the first channel. Type 0.5, expecting a picture half as bright, and only red is halved while green and blue stay as they were, so the picture turns blue-green. The same goes for Add, Subtract and the other Math nodes. To change all three channels by the same amount, add a Float node from the Parameters folder, set its Value, and wire it into B. A wired value fills every channel.
Add two bright colours and a channel can end up above 1. The screen shows that as fully on, so bright areas flatten out and shift towards white or yellow. The next node still gets the real number. Follow with Saturate to hold every channel between 0 and 1.
Color Ramp: many colours from one value
Mix blends two colours. Color Ramp, in the Color folder, blends as many as eight. It takes one value in, called Fac, and looks it up along a row of colour stops. Each stop is a colour and a position from 0 to 1. Between two stops the colours blend smoothly.
- Select the node to see its stops in the inspector. Each row is a colour well and a Pos, the position of that stop.
- Add Stop adds one, up to eight. The red minus removes one. A ramp always keeps at least two.
- Move two stops close together for a sharp change of colour, or far apart for a slow one.
- Fac is held between 0 and 1. A value below 0 gets the first colour and a value above 1 gets the last.
- The second output, Alpha, is the opacity of the stops, blended the same way.
Color Ramp is the workhorse of colour in a graph. Every trick from the last chapter makes a value, and a ramp turns any value into colours of your choosing: fire from a gradient, marble from noise, a striped flag from a ramp with stops pushed together. When you are not sure how to colour something, build it in grey first and finish with a Color Ramp.
Hue, saturation and value
Red, green and blue are how a screen thinks. People find it easier to describe a colour in three other terms, and the graph can work in those too.
- Hue is which colour it is, as a position round the colour wheel from 0 to 1. Red is 0, green is a third of the way round, blue two thirds, and 1 is red again. It wraps round: a hue of 1.25 is the same colour as 0.25, so a value that keeps growing runs through the rainbow again and again.
- Saturation is how vivid it is. 0 is grey and 1 is the pure colour.
- Value is how bright it is. 0 is black whatever the other two say.
| Node | What it does |
|---|---|
| HSV to RGB | Reads a Vector 3 as hue, saturation and value and gives the colour |
| RGB to HSV | The other way. Follow it with Split Vector: X is the hue, Y the saturation, Z the value |
| Hue to RGB | One value in, a fully vivid, fully bright colour out. A rainbow in a single node |
Adjusting a finished colour
To change one of the three on a colour you already have, you could convert it with RGB to HSV, split it, change a number, pack it again and convert back. Hue/Saturation/Value, in the Color folder, does all of that in one node. Reach for it when a picture is right but its colours are not.
On this node the neutral setting for Hue is 0.5, not 0. Setting Hue to 0 or to 1 swaps every colour for its opposite on the wheel. Saturation and Value are neutral at 1.
Grey and vivid
The Saturation node blends each colour with its own brightness. Luminance works that brightness out and gives it as one value, with green counting for the most and blue for the least, the way the eye sees them. Use Luminance when you want to forget the colours of something and treat it as a value again: to cut it with Step, or to colour it afresh with a Color Ramp.
Brightness, contrast and gamma
Three small nodes in the Color folder do the everyday adjustments. They are shown here on a grey ramp so that the plot can show what each does to the numbers. On a colour they do the same to all three channels.
| Node | Settings | What it does |
|---|---|---|
| Brightness/Contrast | Brightness, Contrast, both 0 for no change | Brightness is added to every channel. Contrast pushes each channel away from the middle value of 0.5, or towards it when negative |
| Gamma | Gamma, 1 for no change | Above 1 lifts the middle and keeps black and white fixed. Below 1 darkens the middle |
| Invert Color | Fac, from 0 to 1 | Gives the opposite colour: each channel taken away from 1. Fac at 0 leaves the colour alone |
| RGB Curves | Three lists of points, headed R, G and B | Float Curve from the last chapter, once for each channel. Raise the middle of the blue list for a cooler picture, or lower the end of the red one to calm a hot highlight |
The Blend node
Blend, in the Blend & Compositing folder, combines two colours the way layers combine in a painting app. Base is the picture underneath, Blend is the layer on top, and the Mode menu picks the sum. Opacity fades the result back towards the base. Order matters in most modes: if the result looks wrong, swap the wires going into Base and Blend.
| What the mode does | Modes | In short |
|---|---|---|
| Covers | Normal | Shows the Blend layer over the base |
| Darkens | Multiply, Darken, Burn, Subtract | White in the Blend layer changes nothing in Multiply, Darken and Burn. Darken keeps whichever is darker |
| Lightens | Add, Screen, Lighten, Dodge, Divide | Black in the Blend layer changes nothing in Add, Screen, Lighten and Dodge. Lighten keeps whichever is lighter |
| Adds contrast | Overlay, Soft Light, Linear Light | A Blend layer darker than middle grey darkens and a lighter one lightens. Soft Light is the gentlest |
| Compares | Difference | Black where the two match, brighter the more they differ |
Multiply and Add here are the same sums as the Math nodes. Blend adds the Mode menu and Opacity, so you can try one mode after another without rewiring. Screen is a softer Add that never goes past white.
Now you try
- A sun. Gradient with Type set to Spherical, into a Color Ramp with three stops: pale yellow at 0, orange at about 0.4, deep blue at 0.6. Wire the ramp into Surface Output.
- Two-colour stripes. A Wave into T of a Mix (Lerp), with two Color nodes in A and B. Change the Wave Profile and Scale.
- A pastel rainbow. Gradient into Hue to RGB, then Saturation with Amount at 0.4.
- A duotone. Take the sun from the first exercise, follow its Color Ramp with Luminance, and feed that into a second Color Ramp with only two stops. Any picture can be recoloured this way.
- A darker picture, done properly. Put Multiply after any of the above, add a Float node set to 0.5 and wire it into B. Then pull the wire off, type 0.5 into B, and see the difference.
Draw bars, circles, rings and stars from nothing but position.
Tell us which node or picture confused you, what you built, and what colour the square showed instead of the one you expected.