Here is an overview of some MathMap functions which are very useful in many situations. This is not a complete function reference. If you are looking for one, you'll find it in the Reference Manual.
Quite often you find that you have a value which varies within a given range, but you want the range to be different. Take, for example the gray gradient:
The variable x varies from -W/2 to W/2 but you want it to be between 0 and 1. In such cases you can use the scale function. The expression scale(x,-W/2,W/2,0,1) is 0 when x is -W/2 and 1 when x is W/2. Hence, you can create the above image with the expression
grayColor(scale(x,-W/2,W/2,0,1))
Suppose we want to produce a gradient from red to green:
We know from above that we can use scale(x,-W/2,W/2,0,1) for a value which is 0 at the left image edge and 1 at the right edge. lerp does the rest: it takes two tuples and produces a value which is "in between" these two values by the same amount as its first argument is in between 0 and 1. Hence, the gradient above can be produced by this expression:
lerp(scale(x,-W/2,W/2,0,1),rgbColor(1,0,0),rgbColor(0,1,0))
The function inintv makes it easy to check whether a value lies within a given range. inintv has a value of 1 if the condition is fulfilled, and of 0 otherwise. You can use this as a condition if in if expression, or as a value in its own right. For example, this expression draws a white ring with an inner radius of 50 and an outer radius of 60:
grayColor(inintv(r,50,60))
Sometimes you have values which you want to lie within a given range. In case they don't, you simply want them to take on the largest value within the range, if they are too large, or the smallest if they are too small.
MathMap often does such operations authomatically, for example if you produce colors with components larger than 1 or smaller than 0.
If you have to it yourself, clamp can help you. For example, clamp(v,[0,0,0],[1,1,1]) restricts every element of v to be in the range from 0 to 1.
The function rand generates a random number. It takes two arguments: The minimum and the maximum value of the number to be generated. This expression, for examples, randomly scatters the pixels of the input image (but not more than 10 pixels away from their original location in both directions):
origValXY(x+rand(-10,10),y+rand(-10,10))
In image manipulation, one often needs a functions which is random but doesn't change as abruptly as rand does. noise is a so-called solid noise function. It takes a tuple of three numbers and returns a value between -1 and 1. If the input arguments change only by a little, so does the resulting value. The overall "look" of the function is random, though. It's hard to describe, so it's best you see for yourself:
grayColor(scale(noise([x/20,y/20,t*2]),-1,1,0,1))
As you can see, the third input value depends on t, so try out changing t. For t being 0, the resulting image looks like this:
Next topic: Further Information