# Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Forums Adobe After Effects Create a shape with fill color that averages of the colors of the lower layer

• # Create a shape with fill color that averages of the colors of the lower layer

3 Members · 4 Posts
• ### Vipul Rajan

November 28, 2021 at 1:00 pm

The question is somewhat more complex than what the title says.

Here’s my problem i have a grid of squares with different shades of gray on one layer (layer 1). I need a square on another layer (layer 2) on top of it that would mask the grid layer.

Now, I want the color of that square on layer 2 to be average of the colors of all the pixels that are within it’s boundaries on the lower layer (layer 1)

I’m a programmer by profession and don’t shy away from code, so feel free to suggest such solutions as well.

• ### Graham Quince

November 29, 2021 at 1:35 pm

I take it the Mosaic effect wouldn’t do this for you? It might not, just throwing it out there.

There is an expression function called sampleImage() That’s probably what you’re after.

Dan Ebberts has a pretty good tutorial on it: http://www.motionscript.com/design-guide/sample-image.html

• ### Levi Borland

December 4, 2021 at 8:55 am

Hello Vipul Rajan,

You may be able to write an expression for the color of the Square on Grid 2.

Here is suggested Pseudocode as I don’t have the project file to reference or try and write the real code for.

var squareOneColorR = R Value of Square One’s Color

var squareOneColorG = G Value of Square One’s Color

var squareOneColorB = B Value of Square One’s Color

var squareOneAlpha = Alpha Value of Square One’s Color

var squareOneSat = Saturation Value of Square One’s Color

Repeat this process for each square on layer one. Then Compute Averages

var squareOneOnLayerTwoR = (squareOneColorR + squareTwoColorR + squareThreeColorR) / n //Where n is the total number of squares you are referencing, in this case three.

That will give you the average for all R Numeric Values of all squares on layer one averaged together and then assign it to the R Field on the Layer 2 Square. Repeat this math for each of the Fields, RGB, Alpha, and Saturation.

If you do the math this way as a JavaScript Expression, the color of Layer Two’s Square should automatically change whenever you adjust the value of any squares on layer 1.

This is of course only workable so long as you can access the individual color values through Expressions. If you send or attach your AE file, I can play around with it on my end and see if I can make workable expression code for you. Good Luck

• ### Levi Borland

December 4, 2021 at 9:10 am

I have included a project file where I found an easier solution than I previously offered. What I did was write an expression on the Shape on Layer Two’s Fill Color Value.

ShapeOnLayer2 = (color from Layer 1 Shape + color from Layer 2 Shape + color from Layer 3 Shape)/3 //3 would be replaced with whatever amount of source colors you chose.

You can see if you change the color value for any of the three shapes on layer 1 it will automatically average out a new color for the single shape that is on layer 2.

Here is the code I used

content(“Rectangle 1”).content(“Fill 1”).color = (

thisComp.layer(“Shape Layer 1”).content(“Polystar 1”).content(“Fill 1”).color +

thisComp.layer(“Shape Layer 1”).content(“Rectangle 1”).content(“Fill 1”).color +

thisComp.layer(“Shape Layer 1”).content(“Ellipse 1”).content(“Fill 1”).color

) / 3;

I didn’t need to access separate color values like R, G, and B in my previous post suggestion. I just used the expression pick whip tool and pointed it to each color value of each shape’s color value in layer 1. It automatically averaged out the values of all of those individual colors as the color values are probably an array of some kind.

Viewing 1 - 4 of 4 posts