There are many ways to work with color in After Effects, including expressions.
To change a color in After Effects, you normally set an RGB value in a color picker.
After Effects interprets these values as four dimensional arrays in RGBA format.
A color array starts with an open square bracket, followed by four values separated by commas,
and a closing square bracket.
Notice in the example that the entire array was divided by 255?
While RGB values in the After Effects color picker reference the standard
0 to 255 color spectrum,
the expression syntax only accepts values from 0 to 1.
Just like regular arrays, you can reference a specific element within the array using its index.
This can be used to evaluate or manipulate a single color channel without affecting
all three channels at once.
The rgbToHsl and hslToRgb methods provide a way to easily convert colors
from one value type to the other.
It starts with a method name, followed by a set of parenthesis, and inside the parenthesis
is a single argument representing the color value to convert.
To see the method in action, you can make some adjustments to the Rocket's lightness amount.
You'll notice a few Color Controls and a Slider Control on the Rocket layer.
First, add an expression to the Body Color, Color Control
by Alt + Clicking (or Option + Clicking on a Mac) the stopwatch.
Now on the first line, we're going to replace what's already there and create a variable
called moreLight, followed by an equals sign.
And then I'm going to drag the Pick Whip up to the Color Brightness slider and let go.
And after this I'm going to add a Division operator, which is a forward slash, followed by 100.
And this is going to divide our slider by 100 so that it's more compatible with our expression.
So this line is done, I'm going to end it with a semi colon.
And I'm going to move on to the next line.
On the second line, you can begin a variable called hsl.
This is going to be followed by an equal sign.
And then we're going to start the rgbToHsl method, so rgb all lowercase,
To, capital T, and then Hsl with a capital H.
This is going to be followed by an open parenthesis,
and value is going to be the input, so just type in value.
And by doing this, we're going to take the color control's value and convert it to HSL.
So close parenthesis and semi colon to end that statement.
And on the third line, we're going to create a variable called assemble.
And then immediately after, an quals sign, and then an open square bracket.
Now this is going to take the HSL conversion and split it out into four indices
so that we can affect each one independently.
So bear with me, we're just going to do hsl, open square bracket 0 for the first, comma, space,
hsl, open, 1, comma, space, hsl, open, 2, close, comma, space,
and finally hsl open, 3, close.
And then close the entire array with another close bracket.
So here, we've split hsl into all the array indices and I'm going to end this line with a semi colon.
And then I'm going to go back to hsl[2] which is our hue, saturation, lightness.
And make sure your cursor is right before the comma, and add an Addition operator here,
and then the moreLight variable, capital L. So come back to the end,
go to the fourth and final line,
and this line is going to execute a round-trip conversion from HSL
back into an RGB array.
So we're going to start this with hsl, To, capital T, Rgb, capital R, open parenthesis,
and we're just going to put the assemble variable in here.
So this is linking moreLight to our slider, and then converting the value to HSL,
opening up all the hsl indices, and adding the moreLight slider to the lightness,
and then bringing it all right back to RGB.
So if you click away from the expression field, nothing's really changed.
And that's because the lightness is still at 0.
But if I drag this slider up, you'll notice that now that blue value is a lot brighter.
You can also drag it back, and it's going to do the same.
I'm going to keep it at 0 for now.
So each of these Color Controls have a different color.
And I'm going to add a new expression to the other two that we haven't used yet.
And just for organization sake I'm going to collapse this and then E + E
to open up just the expressions.
And here's our three colors, right at the bottom.
I'm going to right click the Color Control we've been working with, and Copy Expression Only.
And then I'm going to highlight the Color property of Fin Color,
and Control + V (or Command + V on a Mac) to paste, and I'm want to do the same for Accent Color,
Control + V (or Command + V on a Mac) to paste.
So now, all of these expressions are applied to each of the Color Controls,
and if I come up here and grab this Color Brightness slider and slide it a bit,
you'll notice the entire Rocket is affected now.
And you can see the colors update here in the expression control panel.
If you've ever wanted to sample in real time an image or footage item's color, hue, lightness,
or alpha value, then you'll love the sampleImage method.
The sampleImage method allows you to specify a sample point and a sample radius
on your desired source or footage.
It starts with sampleImage, capital I, followed by an open parenthesis, and four arguments
separated by commas, and ending in a close parenthesis.
The first argument is the sample position, and it accepts a two dimensional Position array.
The second argument is the sample radius, also in the form of a two dimensional array,
and it sets the X and Y sample radius.
The last two arguments are optional, and are for pre and post effect evaluation,
and for changing the evaluation time.
More detail can be found in the After Effects Expression Language Reference on Adobe's website.
Now you're going to make the Rocket go stealth mode, and match the color behind it as it
travels with a little sampleImage expression magic.
This is a little more advanced, but it has a fun result and it fits nicely with this color lesson.
Okay so let's start by highlighting the Rocket layer and coming up to Body Color
and Alt + Clicking (or Option + Clicking on a Mac) the stopwatch to add a new expression.
After that, I'm going to highlight the Color property here and solo it by tapping S + S
just so I can get a little bit more organized.
Now you can click into the expression and we're going to override this default expression.
You can start with a variable, and call it array, followed by an equals sign.
And now we're going to begin the Position calculation with transform.position.value
Now value is here to output the Position property as an actual array instead of just a Property object.
So right after value, add a period operator and then we're want to use a new method called splice.
Now, right after splice we're want to open parenthesis, and the first argument is going
to be the array to split.
So we're going to give it the Rocket's current Position in Composition Space.
So we need to use a toComp method for this, so start with toComp, capital C,
and then open parenthesis, open square bracket, 0, comma, 0, comma, 0, close square bracket,
and close parenthesis.
So this is a pretty default toComp conversion.
Now, after this, since this is the first argument, we need to add another comma, so comma, space.
The second argument specifies how many indices you want to keep from the original array,
starting from the beginning.
So we're just going to use X and Y for this, so we can just use two array indices,
and then close parenthesis, and semi colon.
Now, on the next line, we're going to grab the Pick Whip tool and come down to this
hidden layer called Color Source, and let go on the title.
We'll come back to look at the Color Source, but right now let's just finish the expression.
Now we're going to use sampleImage, so period operator, sampleImage, capital I, open parenthesis,
and now we're going to bring in our variable from earlier, so array, comma.
So this is going to look at the Rocket's Position and sample the color behind it.
And now we need to give it a sample radius, so for this case I'm just going to use
100, 100 for X and Y sample radius.
And then close parenthesis.
So this ends our expression, and if we click away from the expression field,
you'll notice the Rocket has turned white.
And that's because the color source has a white fill.
But we're not actually going to use the Color Source to pull the color for this Rocket,
we're going to turn this into an adjustment layer and use the color under it.
So first, turn on the layer, and that's going to give you this solid white layer.
And now, we can use the magic mechanics of adjustment layers to finalize this stealth effect.
So if I enable the adjustment layer, the white layer disappears and suddenly the Rocket Body
has disappeared as well.
You can only attach one layer to the sampleImage method, which is why we're using the
color source as an adjustment layer.
You could also precomp everything here under the adjustment layer, insert it into this
composition and reference that instead, but the adjustment layer is cleaner
and it avoids precomping which is kinda nice.
So basically, this expression is looking at our array, which is the Rocket Position
in Composition Space, and then it's taking the Color Source adjustment layer,
which is calculating all the effects and colors and combining it into one layer,
and then sampling the image based on where the Rocket is,
and giving it a 100 by 100 search radius.
So if I scrub the timeline, you can start to see how this works.
As the Rocket passes through the different bands of color on its way to the planet,
you can see a gradual change in color.
It's most noticeable right here as it passes through the planet,
and it has a nice soft transition.
So this is kinda cool how the fins are still there, but the body's missing,
but let's go ahead and do the whole Rocket.
So I'm going to right click the Color property and Copy Expression Only,
and then I'm going to add a new expression to Fin Color and Accent Color,
and I'm going to highlight both of those here in the timeline
and Control + V (or Command + V on a Mac)
and now the entire Rocket is kinda ghosted and all you can see is this little highlight.
And here's the final effect, it looks pretty cool.
You can see that as it passes over the Planet,
there's a little bit of lag there as it's trying to catch up.
And this is because of the soft sample radius that we've given it.
It doesn't quite camouflage itself as it passes over the edge, but it's a really cool effect.
We've arrived at the end of Lesson 307: Evaluating and Controlling Color Properties
If you enjoy this expressions course, consider purchasing the paid content.
It includes in-depth documentation, extra tutorial content, high definition videos,
and all the project files used in the training.
Your purchase will help to create more free courses like this in the future.