Cosine Palette
Work out one value from 0 to 1, then turn it into colour as a last, separate step: a cosine palette, bands, or contour lines.
How it works¶
Inigo Quilez's cosine palette makes red, green and blue each from a cosine of the same value v:
a, b, c and d are three numbers each, one per channel. a is the middle brightness, b how far each channel swings from it, c how many times it cycles as v goes 0 to 1, and d where each channel starts in its cycle. With a = b = (0.5, 0.5, 0.5) and c = (1, 1, 1), shifting the three channels' d apart, (0, 0.33, 0.67), gives a rainbow; changing them changes the whole scheme without touching the pattern.
The helpers have palette(v, a, b, c, d) and four ready-made sets: rainbow, sea, acid and ember.
Keeping colour as a last step gives other choices too. floor(v × 5) / 4 allows only five shades (posterising). fract(v × 10) counts up ten times as v goes 0 to 1, so where it is close to 0.5 makes ten contour lines, like a map.
The C is for the raylib pixels starter. Every example uses the same plasma value.
Five bands¶
static RGB shade(float x, float y, float t)
{
float v = 0.5f + 0.5f * plasma(x, y, 150, 150);
return grey(floorf(v * 5) / 4);
}
function shade(x, y, t) {
let v = 0.5 + 0.5 * plasma(x, y, 150, 150);
return grey(floorf(v * 5) / 4);
}
Through the palette¶
static RGB shade(float x, float y, float t)
{
float v = 0.5f + 0.5f * plasma(x, y, 150, 150);
return rainbow(v);
}
function shade(x, y, t) {
let v = 0.5 + 0.5 * plasma(x, y, 150, 150);
return rainbow(v);
}
Contour lines¶
static RGB shade(float x, float y, float t)
{
float v = 0.5f + 0.5f * plasma(x, y, 150, 150);
bool edge = fabsf(fract(v * 10) - 0.5f) < 0.06f;
return edge ? grey(0.1f) : rainbow(v);
}
function shade(x, y, t) {
let v = 0.5 + 0.5 * plasma(x, y, 150, 150);
let edge = fabsf(fract(v * 10) - 0.5) < 0.06;
return edge ? grey(0.1) : rainbow(v);
}
Sea, acid and ember side by side¶
static RGB shade(float x, float y, float t)
{
float v = 0.5f + 0.5f * plasma(x, y, 150, 150);
return x < 100 ? sea(v) : x < 200 ? acid(v) : ember(v);
}
function shade(x, y, t) {
let v = 0.5 + 0.5 * plasma(x, y, 150, 150);
return x < 100 ? sea(v) : x < 200 ? acid(v) : ember(v);
}
A palette of your own¶
c = (2, 1, 0) cycles red twice and leaves blue still; change a number and see what moves.
static RGB shade(float x, float y, float t)
{
float v = 0.5f + 0.5f * plasma(x, y, 150, 150);
return palette(v, rgb(0.5f, 0.5f, 0.5f), rgb(0.5f, 0.5f, 0.5f), rgb(2, 1, 0), rgb(0.5f, 0.2f, 0.25f));
}
function shade(x, y, t) {
let v = 0.5 + 0.5 * plasma(x, y, 150, 150);
return palette(v, rgb(0.5, 0.5, 0.5), rgb(0.5, 0.5, 0.5), rgb(2, 1, 0), rgb(0.5, 0.2, 0.25));
}
Related¶
- Combining Two Patterns: one value first, colour last.
- Plasma Effect: the value used here.
- Creative Coding Helpers: the palette sets' numbers.