12
$\begingroup$

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?

$\endgroup$
4
  • $\begingroup$ 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. $\endgroup$
    – Carlo
    Oct 16, 2015 at 7:29
  • $\begingroup$ 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. $\endgroup$
    – Ascalon
    Oct 16, 2015 at 17:17
  • $\begingroup$ You mean like this? $\endgroup$
    – PGmath
    Dec 31, 2015 at 21:40
  • $\begingroup$ If your group is not instanced consider using colored frames instead of groups to organize your nodes. $\endgroup$ Jan 8, 2016 at 15:42

1 Answer 1

11
$\begingroup$

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.

enter image description here

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.

enter image description here

$\endgroup$
3
  • 1
    $\begingroup$ Taken from my answer here. $\endgroup$
    – PGmath
    Jan 8, 2016 at 15:32
  • $\begingroup$ 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? $\endgroup$
    – p2or
    Feb 23, 2017 at 12:54
  • $\begingroup$ @poor I believe that can be done with a greater than node plugged into the mix factor. $\endgroup$
    – PGmath
    Feb 23, 2017 at 13:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .