ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

# An expression to find the maximum value over time?

FAQ   •   VIEW ALL
 An expression to find the maximum value over time? on Dec 22, 2008 at 8:33:55 pm

I have two values I'm trying to animate, a hue and a position. I'm animating the position with keyframes. I'd like the hue to depend on the position of a layer, and change from red to blue and back. In order to do my hue calculation, I need to use the minimum and maximum values for the position during the entire length of the layer.

As we go along in production, the minimum and maximum values for position may change, and the position keyframes will be re-timed, so I need a flexible way so that the hue will stay within red and blue, but also stay time-synched to the position.

Is there an expression that will find the maximum and minimum value of another property for use?

Thanks!

 Re: An expression to find the maximum value over time?on Dec 22, 2008 at 8:48:05 pm

As far as minumum (and maximum), do you mean x, y, or both (which might not occur at the same time)?

Dan

 Re: An expression to find the maximum value over time?on Dec 22, 2008 at 9:12:13 pm

OK - here's an example that gets minimum and maximum x and y position. You'd need to add some code at the end that uses the variables. This works by going back through the timeline frame-by-frame, so it might bog down if your comp gets too long.

p = transform.position;
minX = p[0];
minY = p[1];
maxX = p[0];
maxY = p[1];

t = time - thisComp.frameDuration;
while (t >= 0){
p = transform.position.valueAtTime(t);
if(p[0] > maxX) maxX = p[0];
if(p[0] < minX) minX = p[0];
if(p[1] > maxY) maxY = p[1];
if(p[1] < minY) minY = p[1];
t -= thisComp.frameDuration;
}

Dan

 Re: An expression to find the maximum value over time?on Dec 22, 2008 at 9:48:35 pm

Dan,
Looking at your code it seems you're looking for minimum/maximum values only from the current time and backwards until the beginning of the comp, instead of going from layer's in-point to out-point.

Am I missing anything?
Thanks

 Re: An expression to find the maximum value over time?on Dec 22, 2008 at 9:57:33 pm

Ah, good point. Not sure what I was thinking. That changes it to this:

p = transform.position.valueAtTime(inPoint);
minX = p[0];
minY = p[1];
maxX = p[0];
maxY = p[1];

t = inPoint + thisComp.frameDuration;
while (t <= outPoint){
p = transform.position.valueAtTime(t);
if(p[0] > maxX) maxX = p[0];
if(p[0] < minX) minX = p[0];
if(p[1] > maxY) maxY = p[1];
if(p[1] < minY) minY = p[1];
t += thisComp.frameDuration;
}

If the layer is too long, you could add a little script that would harvest the min and max values, plug those into sliders and turn off the expression.

Dan

 Re: An expression to find the maximum value over time?on Dec 22, 2008 at 9:12:52 pm

Oh, sorry... It's just the y

 Re: An expression to find the maximum value over time?on Dec 22, 2008 at 9:44:11 pm

There is a way to get maximum and minimum values.
I'd go with traversing all layer's frames and getting the minimum and maximum (for now we'll deal with one dimension) and then know your relative value according to those min & max.

A different approach (and better one) will be to:
1) Add the following effect to the layer to be colored:
* 2 "Color Control" effects (name ColorA & ColorB)
* "Slider Control" effect
* "Fill" effect
2) Create a null layer (called "Null 1") and add to it 2 "Point Control" effects (called PointA & PointB)
3) Paste the following expressions to the Fill effect and position of the layer.

```Fill.Color:
c1 = effect("ColorA")("Color");
c2 = effect("ColorB")("Color");
sliderValue = clamp(effect("Slider Control")("Slider"), 0, 100); // making sure slider value is between 0 to 100
linear(sliderValue, 0, 100, c1, c2)

-----------------------------------------
Position:
l = thisComp.layer("Null 1");
p1 = l.toComp(l.effect("PointA")("Point"));
p2 = l.toComp(l.effect("PointB")("Point"));
sliderValue = clamp(effect("Slider Control")("Slider"), 0, 100); // making sure slider value is between 0 to 100
linear(sliderValue, 0, 100, p1, p2)```

In case this isn't clear send me your email and I'll email you the project

Regards