Color Mixing
15 / 30
Learn to use the mix function for color blending and understand the concept and application of linear interpolation.
To blend between two colors you don't need to do the math yourself — GLSL has mix.
What is mix()
mix(a, b, t) blends between a and b based on the factor t:
t = 0.0→ fullyat = 0.5→ halfway betweenaandbt = 1.0→ fullyb
t must be in the 0.0 to 1.0 range — exactly what UV coordinates give you.
Using uv.x for a horizontal gradient
Pass uv.x as t: left side shows color a (t = 0), right side shows color b (t = 1):
Try changing it
Swap in different colors:
| Combination | color1 | color2 |
|---|---|---|
| Orange → Purple | vec3(1.0, 0.5, 0.0) | vec3(0.5, 0.0, 1.0) |
| White → Black | vec3(1.0) | vec3(0.0) |
| Green → Yellow | vec3(0.0, 0.8, 0.3) | vec3(1.0, 0.9, 0.0) |
Exercise
The exercise code has mix with a fixed factor of 0.0, so the canvas is all red. Change the factor to uv.x so it fades from red on the left to blue on the right.
Answer Breakdown
Change the third argument from 0.0 to uv.x:
uv.x is 0 on the left (fully red), 1 on the right (fully blue), blending in between.
Try uv.y instead — the gradient flips to bottom-to-top.