Gradients
Blend colors across a surface by position; the first thing most people draw in a shader.
Cards
2 live examples to tweak, learn and copy.
How it works
A gradient turns a position into a color. Every one has the same two parts: a number Linear interpolation (mix) Blends from a to b by a factor t, along a straight line. GLSL calls it mix; HLSL and most node editors call it lerp. Vector A group of two to four numbers handled as one value (vec2, vec3, vec4). The same type holds a position, a direction, a color or a UV.t that runs from 0 to 1 across
the surface, and a mixBlends from a to b by a factor t, along a straight line. GLSL calls it mix; HLSL and most node editors call it lerp.mix(a, b, t) = a (1 − t) + b tt. Colors are vectorsA group of two to four numbers handled as one value (vec2, vec3, vec4). The same type holds a position, a direction, a color or a UV.mix blends red, green and blue at once.
The shape of the gradient lives entirely in UV coordinates A 2D position on a surface or the screen, usually from 0 to 1 on both axes. Shaders draw shapes and patterns as functions of it. Smoothstep A soft threshold. It is 0 below the first edge, 1 above the second, and follows an S-shaped curve with flat ends in between.t. The UVA 2D position on a surface or the screen, usually from 0 to 1 on both axes. Shaders draw shapes and patterns as functions of it.uv = gl_FragCoord.xy / resolutiont sets where the blend starts and ends, and passing it through a smoothstepA soft threshold. It is 0 below the first edge, 1 above the second, and follows an S-shaped curve with flat ends in between.t = clamp((x − e0) / (e1 − e0), 0, 1); s = t² (3 − 2t)
The other choice is which color space the mix happens in. The same two endpoints mixed as sRGBThe standard encoding of screen colors. Stored values follow a curve close to a 1/2.2 power of the light, so they must be decoded before lighting or blending. sRGB The standard encoding of screen colors. Stored values follow a curve close to a 1/2.2 power of the light, so they must be decoded before lighting or blending. OKLab A color space where equal distances look like equal differences: L is lightness, a runs green to red and b blue to yellow. Ramps mixed in it stay even.linear = ((c + 0.055) / 1.055)^2.4 for c > 0.04045
The Linear sky card builds the basic gradient step by step. The RGB vs OKLab ramp card stacks the three color spaces one above the other, with a switch that shows only how bright each ramp looks.
Further reading
- HSL and HSV (opens in a new tab)Reference
- A perceptual color space for image processing (opens in a new tab)Article
- mix (opens in a new tab)Reference
- clamp (opens in a new tab)Reference
- Linear interpolation (opens in a new tab)Reference
- step (opens in a new tab)Reference
- smoothstep (opens in a new tab)Reference
- Shaping functions (opens in a new tab)Book
- gl_FragCoord (opens in a new tab)Reference
- Uniforms (opens in a new tab)Book
- UV mapping (opens in a new tab)Reference
- Euclidean vector (opens in a new tab)Reference
- length (opens in a new tab)Reference
- normalize (opens in a new tab)Reference
- Colors (opens in a new tab)Book
- CanvasItem shaders (opens in a new tab)Docs
- CSS Color Module Level 4 (opens in a new tab)Reference
- GPU Gems 3, chapter 24: The Importance of Being Linear (opens in a new tab)Book
- sRGB (opens in a new tab)Reference