1

Okay, I can't believe I never noticed this until today but... the MixRGB node's Subtract and Difference blend modes both appear to produce the same output.

Start with white and take blue away from it... both blend modes produce yellow.

To specify precisely with hex codes: #ffffff minus #0000ff equals #ffff00 no matter which blend mode is used.

So pardon my wording but, is there any difference?

Let's consult the Blender Manual:

Subtract

Taking Blue away from white leaves Red and Green, which combined make Yellow. Taking Blue away from Purple leaves Red. Use this to desaturate an image. Taking away yellow makes an image bluer and more depressing.

Difference

It takes out a color. The color needed to turn Yellow into White is Blue. Use this to compare two very similar images to see what had been done to one to make it the other; sort of like a change log for images. You can use this to see a watermark (see Watermark images) you have placed in an image for theft detection.

So the descriptions are a little different... but I reiterate: is there any difference?

(If the answer is that they are identical, that's fine. I just want to know before I go creating a bunch of node setups based on an assumption.)

Mentalist
  • 19,092
  • 7
  • 94
  • 166
  • 1
    No, they are not producing the same result. It might look like it when you use white as the first color, but change that to red and you will already see a difference. – Gordon Brinkmann May 31 '22 at 11:28
  • Thanks. I tried setting Color1 to red (#ff0000) and plugged an image into Color2. In this configuration there is a clear difference. The best I can explain it though is that Subtract takes away non-reds leaving a red and black two-tone image, while Difference takes away from red according to what the other colors are, which leaves red where darker areas of Color2's image would be. – Mentalist May 31 '22 at 11:48
  • 2
    Here is a more technical description: https://en.wikipedia.org/wiki/Blend_modes#Subtract (Subtract simply subtracts pixel values of one layer with the other. In case of negative values, black is displayed / Difference subtracts the bottom layer from the top layer or the other way around, to always get a non-negative value.) – Blunder May 31 '22 at 11:55
  • 1
    @Mentalist Actually, when Color1 is red and you subtract a colorful image, it's not taking away non-reds from the image. All red values of the image are subtracted from the red color, and since the green and blue channels are 0 in Color1, every other color of the image gets subtracted from 0 leaving a negative value (if the node is not clamped) which shows as black. – Gordon Brinkmann May 31 '22 at 11:55
  • I think I get the gist of it. If I have red as Color1and I want to invert it to cyan, I should use Difference, with white as Color2. I someone posts an answer that can be understood intuitively (maybe with some visual examples), I will most likely accept it. – Mentalist May 31 '22 at 12:40

1 Answers1

5

Difference is the absolute value of Subtract ie. $|\mathrm{Color1} - \mathrm{Color2}|$. So they will be the same if Color1 is >= Color2.

See this answer for more.


edit: I'll give a small example with grayscale colors.

Subtract vs Difference with grayscale colors

With Subtract, Color2 is subtracted from Color1.

  1. When Color2 is black, you get Color1.
  2. As Color2 gets brighter, the output gets darker.
  3. When Color2 reaches Color1, you get black.
  4. When Color2 is brighter than Color1, it can't get any darker so it just stays black.

Difference is the same except in case 4.

  1. (For Difference) When Color2 is brighter than Color1, instead of being stuck at black, it starts getting lighter again.

If Subtract means "subtract Color2 from Color1" then Difference means "subtract the darker from the lighter".

scurest
  • 10,349
  • 13
  • 31
  • I don't see "Subtract" mentioned in that answer. Maybe it's a newer blend mode? – Mentalist May 31 '22 at 11:41
  • Dunno, it doesn't list them all, I always just check the source code (linked in a comment). – scurest May 31 '22 at 11:43
  • @Mentalist Well, Subtract is just as the name says, a subtraction, which means Color1 - Color2 <> Color2 - Color1, whereas Difference means |Color1 - Color2| = |Color2 - Color1|. – Gordon Brinkmann May 31 '22 at 11:45
  • 1
    So you could think of Difference as entry-wise Distance? – Markus von Broady May 31 '22 at 12:34
  • @MarkusvonBroady No, Vector Math > Distance results in a 1-dimensional value, whereas MixRGB > Difference still results in RGB. In mathematical terms it's (like mentioned above), Difference = abs(Color1 - Color2Fac) and Subtract = (Color1 - Color2Fac). – Gordon Brinkmann May 31 '22 at 13:15
  • 1
    @GordonBrinkmann That's what "entry-wise" means. Also that's not the right formula for Fac in Difference, Fac lerps between Color1 and |Color1-Color2|. – scurest May 31 '22 at 13:23
  • @scurest Sorry, I'm no native speaker, I didn't understand what entry-wise meant. And I have no idea what "lerps" means... but if I use a value of Fac < 1 and Color2 is black, then Color1 is not affected by Fac. So what do you mean mathematically is the influence of Fac in Difference? – Gordon Brinkmann May 31 '22 at 14:36
  • Lerp = linear interpolate. When Fac=0 the output is Color1. When Fac=1 the output is determined by the mode, |Color1-Color2| for Difference. In between it varies linearly between those endpoints. – scurest May 31 '22 at 14:49
  • 1
    @GordonBrinkmann An entry is a component, so Vectors and colors have 3 entries. Entry, or component-wise means separately for each. Vector Math > Multiply is an entry-wise operation, because it multiplies V1.x by V2.x, V1.y by V2.y, and V1.z by V2.z and that's it - you would have the same by separating V1 into 3 values, separating V2 into 3 values, using 3 scalar math > multiply nodes, and then combining the results into a vector. Vector Math > Distance isn't an entry-wise operation - you can use Subtract > Absolute, but apparently RGB Difference ix node is a single node replacement. – Markus von Broady May 31 '22 at 15:06
  • @scurest If you use Vector Math > Scale on Color2 with the Fac value for scaling, then plug Color1 and the scaled Color2 into a Vector Math > Subtract node and this into a Vector Math > Absolute node, this node setup gets you the exact same color as MixRGB > Difference, so what is incorrect with my formula abs(Color1 - Color2*Fac) which does exactly what the Vector Math nodes do? – Gordon Brinkmann May 31 '22 at 15:16
  • @GordonBrinkmann For example, take Color1 = Color2 / 2, Then the difference is also Color1, so the output will always be Color1 regardless of Fac (you can test this). But your formula gives abs(1/2 - Fac)*Color2, which at say Fac=1/2 is totally black. – scurest May 31 '22 at 15:47
  • @scurest So what is the formula behind this? If I use for example white (1, 1, 1) and blue (0, 0, 1) for example no matter which is Color1 or Color2, my formula works. As soon as I change RGB values to something else than 0 or 1, I cannot calculate the result anymore, for example with (1, 0.5, 0.5) and (0, 0, 1). – Gordon Brinkmann May 31 '22 at 18:04
  • 2
    @GordonBrinkmann (1-Fac)Color1 + Fac|Color1-Color2| – scurest May 31 '22 at 18:10
  • @scurest Okay thanks, have no more time to check that today but it sounds reasonable. Looks like the factor is working as the Opacity value in Photoshop when using the layer blend mode Difference, but I thought the factor would work like the Fill value. – Gordon Brinkmann May 31 '22 at 18:25
  • 1
    @GordonBrinkmann Wow, this is great! In the past I googled how Fill differs from Opacity in Photoshop and I only learned the former doesn't affect effects like shadow, while the latter does. I nodified the formula you described: https://i.imgur.com/UR9Vnbu.png and got the same color in Photoshop with blend mode = difference and Fill = 0.35 (factor used in Blender). So in Photoshop the opacity lerps between layers, and Fill is a color multiplier. – Markus von Broady Jun 01 '22 at 10:10
  • 1
    @MarkusvonBroady Yes, it seems like that. That's a really cool byproduct of this discussion, now I got formulas for the blend mode in Photoshop. In the past I always read that Difference in PS is calculated |color1 - color2| and read that Difference is one of 8 blend modes where Opacity and Fill behave different (apart from having no influence on effects etc.) - but never an explanation how they affect the blend mode differently. – Gordon Brinkmann Jun 01 '22 at 10:18