I wanted to play with the new palette support in v3.30, so I made my favorite palette, Viridis. I got the RGB values from a neat palette generator.
The idea is to generate 11 colors and have them evenly distributed between 0.0, 0.1, 0.3, ..., 0.9, 1.0
. This will work with any palette generator that can generate 11 colors. If you have n
colors then you can space them evenly with 1/(n-1)
spacing.
# put these lines near the top, outside of function declarations
var viridis = [ 0.0, 68/255, 1/255, 84/255, 0.1, 72/255, 36/255, 117/255, 0.2, 65/255, 68/255, 135/255, 0.3, 53/255, 95/255, 141/255, 0.4, 42/255, 120/255, 142/255, 0.5, 33/255, 145/255, 140/255, 0.6, 34/255, 168/255, 132/255, 0.7, 68/255, 191/255, 112/255, 0.8, 122/255, 209/255, 81/255, 0.9, 189/255, 223/255, 38/255, 1.0, 253/255, 231/255, 37/255,]
var inferno = [ 0.0, 0/255, 0/255, 4/255, 0.1, 22/255, 11/255, 57/255, 0.2, 66/255, 10/255, 104/255, 0.3, 106/255, 23/255, 110/255, 0.4, 147/255, 38/255, 103/255, 0.5, 188/255, 55/255, 84/255, 0.6, 221/255, 81/255, 58/255, 0.7, 243/255, 120/255, 25/255, 0.8, 252/255, 165/255, 10/255, 0.9, 246/255, 215/255, 70/255, 1.0, 252/255, 255/255, 164/255, ]
var magma = [ 0.0, 0/255, 0/255, 4/25, 0.1, 20/255, 14/255, 54/255, 0.2, 59/255, 15/255, 112/255, 0.3, 100/255, 26/255, 128/255, 0.4, 140/255, 41/255, 129/255, 0.5, 183/255, 55/255, 121/255, 0.6, 222/255, 73/255, 104/255, 0.7, 247/255, 112/255, 92/255, 0.8, 254/255, 159/255, 109/255, 0.9, 254/255, 207/255, 146/255, 1.0, 252/255, 253/255, 191/255, ]
var plasma = [ 0.0, 13/255, 8/255, 135/255, 0.1, 65/255, 4/255, 157/255, 0.2, 106/255, 0/255, 168/255, 0.3, 143/255, 13/255, 164/255, 0.4, 177/255, 42/255, 144/255, 0.5, 204/255, 71/255, 120/255, 0.6, 225/255, 100/255, 98/255, 0.7, 242/255, 132/255, 75/255, 0.8, 252/255, 166/255, 54/255, 0.9, 252/255, 206/255, 37/255, 1.0, 240/255, 249/255, 33/255, ]
setPalette(viridis)
// setPalette(inferno)
// setPalette(magma)
// setPalette(plasma)
// in your render code replace hsv() with paint()
paint(v) # a bit too bright for me
paint(v, v) # darken dark areas
For example, here’s Sinpulse 1D/2D/3D using the viridis palette:
var viridis = [ 0.0, 68/255, 1/255, 84/255, 0.1, 72/255, 36/255, 117/255, 0.2, 65/255, 68/255, 135/255, 0.3, 53/255, 95/255, 141/255, 0.4, 42/255, 120/255, 142/255, 0.5, 33/255, 145/255, 140/255, 0.6, 34/255, 168/255, 132/255, 0.7, 68/255, 191/255, 112/255, 0.8, 122/255, 209/255, 81/255, 0.9, 189/255, 223/255, 38/255, 1.0, 253/255, 231/255, 37/255,]
var inferno = [ 0.0, 0/255, 0/255, 4/255, 0.1, 22/255, 11/255, 57/255, 0.2, 66/255, 10/255, 104/255, 0.3, 106/255, 23/255, 110/255, 0.4, 147/255, 38/255, 103/255, 0.5, 188/255, 55/255, 84/255, 0.6, 221/255, 81/255, 58/255, 0.7, 243/255, 120/255, 25/255, 0.8, 252/255, 165/255, 10/255, 0.9, 246/255, 215/255, 70/255, 1.0, 252/255, 255/255, 164/255, ]
var magma = [ 0.0, 0/255, 0/255, 4/25, 0.1, 20/255, 14/255, 54/255, 0.2, 59/255, 15/255, 112/255, 0.3, 100/255, 26/255, 128/255, 0.4, 140/255, 41/255, 129/255, 0.5, 183/255, 55/255, 121/255, 0.6, 222/255, 73/255, 104/255, 0.7, 247/255, 112/255, 92/255, 0.8, 254/255, 159/255, 109/255, 0.9, 254/255, 207/255, 146/255, 1.0, 252/255, 253/255, 191/255, ]
var plasma = [ 0.0, 13/255, 8/255, 135/255, 0.1, 65/255, 4/255, 157/255, 0.2, 106/255, 0/255, 168/255, 0.3, 143/255, 13/255, 164/255, 0.4, 177/255, 42/255, 144/255, 0.5, 204/255, 71/255, 120/255, 0.6, 225/255, 100/255, 98/255, 0.7, 242/255, 132/255, 75/255, 0.8, 252/255, 166/255, 54/255, 0.9, 252/255, 206/255, 37/255, 1.0, 240/255, 249/255, 33/255, ]
setPalette(viridis)
// setPalette(inferno)
// setPalette(magma)
// setPalette(plasma)
export function beforeRender(delta) {
t1 = time(.05)*PI2
t2 = time(.09)*PI2
zoom = 1+ wave(time(.2))*3
t3 = time(.1)
}
export function render(index) {
render3D(index, index/pixelCount, 0, 0)
}
export function render2D(index, x, y) {
render3D(index, x, y, 0)
}
export var v, min, max
export function render3D(index, x, y, z) {
h = (1 + sin(x*zoom + t1) + cos(y*zoom + t2) + sin(z*zoom + t1 - t2))*.5
v = (h*h*h+1)/9
// figure out range of v so we can normalize above the +1 and /9 above
if(v < min) {min=v}
if(v > max) {max=v}
// hsv(h,1,v/2) // old
paint(v, v/2)
}
NB: You probably want to clamp the values to 0..1
so the values don’t wrap; or 0..n
for some integer n
if you want it to wrap n
times. When converting an existing pattern I look at the min/max
values and clamp manually.