Faking color ramps in Blender 2.79

How to fake a two handle black and white linear color ramp.

If a color ramp only has two handles, the left one is black and the right one is white, it acts like a gradient texture except that moving the handles changes the slope of the gradient. That is, it implements a two point form linear equation.  We can do that with math nodes.

Visualizing 2D equations

Suppose we want to draw the line representing where the X coordinate of a plane is equal to the Y coordinate.  The easiest way to do this would seem to be to use a mixRGB node. The top color would be used for the points where X is not the same as Y and the bottom color for the points where they are equal.

Unfortunately, Blender doesn’t have an equals math node. To make one, consider that X is equal to Y is the same as saying that X is not greater than Y and X is not less than Y. Blender also doesn’t have logic nodes either, so we have to fake the logic.  We get a node group

But this won’t show our line, because there is no thickness to it. Let’s make the line wider, by using a delta factor.  If Y is close to X use black, otherwise use white. That node group looks like this

From left to right

  • The Texture Coordinate node provides a vector giving the X, Y and Z values of the location of a point on the surface of our object.  In this case, Z will always be 0, since it is a flat plane.  X will vary from 0 on the left to 1 on the right.  Y will vary from 0 on the bottom to 1 on the top.
  • The Separate XYZ node gives us the individual values.
  • We add a constant to X to give us the upper bound, and subtract the same constant giving the lower bound.  Using the same constant for both will give us a strip that is centered on the actual line. The size of this constant gives the width of the line.
  • We compare Y to each of these.  The compare Math nodes return 0 if their expression is false and 1 if it is true.
  • We apply the minimum operator.  If Y lies outside either range, the minimum will be 1. If it is inside both ranges it will be 0.
  • Not shown: This reverses the result we want.  The top input of the mix node will be used if Y is outside the range, the bottom will be used if it is inside.  There are at least two ways to fix this. Subtract the result from 1 or use a color invert node between the minimum and the mix node.  For this discussion we’ll leave it as is: Black will be inside, white, outside.

A color ramp

If we add a color ramp to the X value, we can control where the line enters and leaves the image.

Since the black handle is at position .25 and the white handle at position .75, the ramp returns 0 outside that range, and a linear value within it.

The color ramp then is returning the result of a linear equation.  It provides us with two points (x=handle 1 pos, y= 0) and (x = handle 2 pos, y =1). The equation for the line can be found on the web, for instance, here. We can use that equation to produce the same results as the color ramp would. Here is the node group for this

But the result isn’t exactly the same as the color ramp

that’s because the color ramp clamps the results.  Anything to the left of the black handle is set to 0 and anything to the right of the white handle is set to 1. Two nodes will resolve that

Since these node groups are fairly useful, I’ve made node groups of them.


One thought on “Faking color ramps in Blender 2.79

  1. Pingback: A reusable apple material in Blender 2.79b | A Dinosaur Contemplating Asteroids

Leave a Reply