In this video, we explore a dynamic color-changing expression in After Effects that allows a layer to adapt its color based on its distance from a target layer. Using JavaScript expressions, we set a minimum and maximum distance range to create a transition between two colors as the layer moves closer to or further from the target. Watch as I break down each line of code, explaining how variables like minDistance, maxDistance, and nearColor come together to produce smooth color transitions with the linear function. Perfect for motion designers looking to add interactivity to their compositions!
Expression:
// Define the target layer whose position will influence this layer's color
var targetLayer = thisComp.layer("Shape Layer 1");
// Set the minimum distance for the color transition to start
var minDistance = 50;
// Set the maximum distance for the color transition to end
var maxDistance = 200;
// Define the color when the layer is near the target (red in this case)
var nearColor = [0.9373, 0.0275, 0.2902, 1]; // RGBA values
// Define the default color when the layer is farther away (dark gray in this case)
var defaultColor = [0.0667, 0.0667, 0.0667, 1]; // RGBA values
// Calculate the distance between this layer and the target layer
var distance = length(thisLayer.position, targetLayer.position);
// Use the linear function to transition the color based on distance
linear(distance, minDistance, maxDistance, nearColor, defaultColor);