# Build color ramp out of math nodes?

I want to use color ramps or RGB curve effects in my node groups. This runs into the problem that the only sockets these nodes have is their input image and factor. This means if I have them in a group, I cannot control the actual ramp/curve stops with inputs from outside the group.

Is it possible to build a custom math node group that would do the same thing as a color ramp or RGB curve, but would allow me access to an equivalent of stops?

• You'll have to deal with the fact that there will be a "given number" of handles though. I don't think that the node interface nowdays allows to let you add them interactively from the node group imput. If you want to use just 2 handles, I would suggest to give a look at this related answer: blender.stackexchange.com/a/34593/15140 (the first part: Create a Map Range node) where is shown how to reproduce the behaviour of a linear-2handles-color ramp node. – Carlo Oct 16 '15 at 7:29
• I want to make a group that could have several handles. Most uses would need only two, but sometimes I may go as far as 4. Thanks for the link. – Drudge Oct 16 '15 at 17:17
• You mean like this? – PGmath Dec 31 '15 at 21:40
• If your group is not instanced consider using colored frames instead of groups to organize your nodes. – Jaroslav Jerryno Novotny Jan 8 '16 at 15:42

Unfortunately there is no way to create a group node exactly like the Color Ramp, where you can add and remove color swatches at will. To get around this I have created a node with two movable swatches, you can then combine multiple of these nodes together to have the functionality of multiple swatches.

## The theory:

The two input colors are plugged directly into a Mix RGB node. The two Pos inputs need to be sent through a function and plugged into the mix factor. The position of the first swatch needs to be mapped to $0$ to get just the first color out of the mix node. The position of the second swatch needs to be mapped to $1$ to get just the second color out of the mix node.

## The math:

Here's a graph to visualize what we are trying to do, on the x-axis is the input factor of the color ramp, on the y-axis is the desired output, $a$ and $b$ are the positions of the two swatches.

With some simple algebra we can find the equation of the line to be:

$$y = \frac{1}{b-a}x + \frac{a}{a-b}$$

The math nodes below are simply replicating this equation. The final Add node also has Clamp checked to clamp the output to the interval $[0,1]$, which is what the mix node accepts.

• Taken from my answer here. – PGmath Jan 8 '16 at 15:32
• Thanks for this nice setup, really helpful. I wonder if there's a way to replicate the constant interpolation type of the color ramp...? I always manually add a rgb curve and shift the black point to the right, but I guess this also can be done with simple math tricks, right? – p2or Feb 23 '17 at 12:54
• @poor I believe that can be done with a greater than node plugged into the mix factor. – PGmath Feb 23 '17 at 13:10