(Page 1 of 2 pages for this article 1 2 >)
Sunday, November 07, 2010
Deeper Modes of Expression, Part 11: Dreaming in Color
Chris Meyer | 11/07
Using expressions to choose, modify, or create master colors.
The next-to-last subject we’re going to cover is manipulating color inside expressions. This is powerful, as so much of graphics is about color, and also because it is very handy to set up a master color on one layer and then have a large number of other layers or effects look to this master – a great way to accommodate sweeping client changes at the last minute.
Ground Rules
One of the main points to know about color in expressions is that color swatches and pickers define color as RGB (red/green/blue) values. However, it is usually easier to manipulate color in the HSL (hue/saturation/lightness) domain. Fortunately, After Effects has a pair of color conversion methods: rgbToHsl and hslToRgb (which we’ll be discussing in greater detail in just a second).
Secondly, color is an array, akin to position or scale. To access the “R” value in RGB, you would use a statement similar to my_rgb[0]; to pick the “S” value in HSL, you would use a statement akin to my_hsl[1].
The third main point to understand is that color values inside expressions range between 0 and 1 rather than 0 to 255, or 0° to 359° for hue. A white color swatch would have a value of 1,1,1 inside expressions, not 255,255,255. Related to this is that the hue value cannot exceed 1; you cannot keep wrapping around the color wheel. Therefore, if you are going to be altering hue inside an expression, you will need to use the modulus math operator % mentioned at the start of this series to wrap it back in bounds. This usually requires just adding a line to your expression, akin to corrected_hue = my_hsl[0] % 1.
The final point – which can cause unexpected errors when you’re working with color expressions – is that both RGB and HSL value arrays have a fourth dimension: alpha (in other words, they are really RGBA and HSLA arrays). You cannot set the alpha strength with a color picker or swatch, but expressions will break if you don’t place at least a dummy number (a good number is 1, for a fully opaque alpha) in this fourth dimension of an array that holds a color value.
Mastering Color
Our first example employs an example of expressions that manipulate a master color for multiple layers in a comp. The layer master_color below has a Color Control expression controller applied to it. The text layers in this comp have been expressed to use the same Fill color as defined by this master color – we just used the pick whip to assign these:

Let’s say you and the client have agreed on a scheme in which the drop shadows for the text have a complementary color (a hue shift of 180°) at only 40% of the lightness of the text, as pictured at left. After enabling expressions for the Drop Shadow’s Shadow Color, setting up a variable called in_rgb, and dragging the pick whip to our master Color Control to assign it, we then added these lines:
in_hsl = rgbToHsl(in_rgb);
new_hue = (in_hsl[0] + 0.5) % 1;
new_lightness = in_hsl[2] * 0.4;
mod_hsl = [new_hue, in_hsl[1], new_lightness, in_hsl[3]];
hslToRgb(mod_hsl)
- The first line of this expression converts the RGB color to HSL.
- The second line adds 0.5 to the hue value (the first dimension of our color array, or in_hsl[0]), and uses the modulus operator % to wrap around the resulting value to stay within the 0–1 range. We chose to add 0.5 because we wanted a 180° hue shift, and 180 ÷ 360 = 0.5.
- The third line creates our new lightness value by grabbing its value out of our color array (in_hsl[2]) and multiplying it by 0.4 to scale it 40%.
- The fourth line plugs our new hue and lightness values into a color array, while keeping the original saturation (in_hsl[1]) and alpha (in_hsl[3]) values intact.
- The fifth line converts our modified color back to RGB, which is what the Shadow Color parameter expects.
After creating this expression for one layer, we copied and pasted the Fill and Drop Shadow effects - with expressions - to the other text layers. As a result, changing the Color Control will affect the fill and shadow of all the layers automatically.
If we wanted to be really clever, we could add Effect Controls to master_color to allow us to interactively set all of these color adjustments for Shadow Color. This is what we’ve done below:

A series of Expression Controls (each renamed to start with the word “set_”) provide a user interface for us to adjust how our shadow color is derived from our master color. We used the pick whip to connect the Fill effect for each text item to set_master, and to connect their Shadow Color to out_shadow color.
Yes, we know it would be easier to just precompose the title layers and apply one Drop Shadow effect to the group, but we’re trying to teach a concept here! Remember that you can use the pick whip to connect expressions across different comps. This means you can set up a set of master color controls in one comp, and wire all of your other comps to this master comp. Change the colors in this master comp, and all of your other wired comps will follow automatically. Just don’t tell the clients – they’ll take this as an opportunity to sit in your studio and play with different color combinations until you fall asleep!
next page: sampling colors from an existing image
(Page 1 of 2 pages for this article 1 2 >)
You must be registered to comment. This is an effort to reduce spam. Please REGISTER HERE.
Hi,
I’ve been using this technique to control the hue values of some layers from a master color picker but notice if I set the the master color to white, grey or black the original hue value isn’t retained and instead is reset to 0 degrees (red) which resets all the hue values on the other layers!
Any idea why the AE color picker does this? It’s really annoying!
Great article by the way!
Posted by .(JavaScript must be enabled to view this email address) on 08/13 at 07:32 AM
|