| **Expression to calculate Color Blending Mode on 2 color values?**
on Sep 10, 2019 at 11:53:29 am Last Edited By Jacek Skrobiasz on Sep 10, 2019 at 3:46:19 pm |

Hi all,

I try to make flickering light changes effects stack like this:

https://vimeo.com/338472778
I made it with help of precompositing, but now I try to recreate it as an single adjustment to more convenient use.

So my question is: is there expression to calculate Color Blending Mode on 2 color values?

To be more specific:

I have sampled RGBvalue1 and RGBvalue2 and I want to make some expression on it to receive RGBresult as a result of LinearLight Blending Mode beetween the two, same as it would be in the following setup:

- Solid1 layer with color RGBvalue1 (lower)

- Solid2 layer with color RGBvalue2 (over previous one) with blending mode LinearLight (or any other standard blending mode to choose).

I didn't find any built-in command in AE's expressions to blend (mix) color values. Or I missed someting?

Or maybe some math on RGBvalue1 and RGBvalue2?

I found

a webpage where LinearLight is defined as:

(Blend > 0.5) * (Target + 2*(Blend-0.5)) + (Blend <= 0.5) * (Target + 2*Blend - 1)

But I struggle how convert it to AE's JavaScript expression.

| **Re: Expression to calculate Color Blending Mode on 2 color values?** on Oct 29, 2019 at 5:39:42 pm |

So you have two color vectors:

[r2, g2, b2, a2] for the top solid (Solid2) corresponding to Blend (also called source)

[r1, g1, b1, a1] for the bottom solid (Solid1) corresponding to Target (also called backdrop)

The formula you've found simplifies to

Target + 2*Blend - 1

Linear Light is a separable blend mode, meaning that the formula is applied to each color channel separately.

For instance, for the red channel it will be:

`r = r1 + 2*r2 - 1`

- same for g and b.

If both the Blend and the Target colors have alphas a1=a2=1, then the resulting color is simply:

[r, g, b, 1]

If not, then it gets worse, cause alphas have to be taken into account.

Let:

C2 = [r2, g2, b2]

C1 = [r1, g1, b1]

B=[r, g, b]

C - resulting RGB vector (without alpha component)

a = a1+a2 - a1*a2 - resulting alpha

Then:

C = (1 - a2/a)*C1+(a2/a)*((1-a1)*C2+a1*B)

And the resulting color vector will be:

[Cr, Cg, Cb, a], where Cr, Cg, Cb are the components of C.

JS code:

`color2 = thisComp.layer("Pre-comp 1").sampleImage([1800,100], radius = [.5, .5]);`

color1 = thisComp.layer("Pre-comp 1").sampleImage([100,800], radius = [.5, .5]);

C2 = color2.slice(0,3);

C1 = color1.slice(0,3);

a2=color2[3];

a1=color1[3];

a=a2+a1-a2*a1;

function linearLight(r1, r2){

return r1 + 2*r2 - 1;

};

B = [linearLight(C1[0], C2[0]), linearLight(C1[1], C2[1]), linearLight(C1[2], C2[2])];

aC=(a - a2)*C1+(a2)*((1-a1)*C2+a1*B);

z=[aC[0]/a, aC[1]/a, aC[2]/a, a];

It appears that it gives the right result.