Menu

Color Sampler

Joe Clay | Jan 25, 2018

You've probably had to color match footage in After Effects, or maybe you're trying to remove something from an image like a colored mark. Or maybe you've had to remove a color cast from something. That's generally pretty easy in Photoshop where you can set up a color sampler. But AE doesn't have that functionality—at least as far as I've seen. You can sample colors under your cursor in the info palette, but as soon as you move your cursor, the information is gone.

So let's use a little expression magic to set up a color sampler. The code below (which might scroll on your screen) is added to a text layer's Source Text property. We have two sliders set up. One is a Layer Control—appropirately called Layer. And the other is a Slider Control called Radius.

r = effect("Radius")("Slider");
target = effect("Layer")("Layer");
color = target.sampleImage(position, [r,r], true);
color = [Math.round(color[0]*255), Math.round(color[1]*255), Math.round(color[2]*255), Math.round(color[3]*255)];
"+\t\t\tR: " + color[0] + "\n\t\t\tG: " + color[1] + "\n\t\t\tB: " + color[2] + "\n\t\t\tA: " + color[3];

The first two lines set up variables for our sliders. Then we take the layer we have selected in the Layer Control, and run sampleImage() on that. We pass it the position of this layer, our text layer, and give it our Radius value as an array using the same value for each dimension. Generally I want this to be tiny. You can't set it to zero though. It'll throw an error so be aware of that. The third argument is for post effect. That is, after the effects have been added. Since we usually want to sample the colors as they are, not before effects are applied, we set this to true. This gets us an RGBA array with colors from 0-1. If you're working in a higher color space, I think this will continue past 1 and you'd likely want to ignore the next line.

color = [Math.round(color[0]*255), Math.round(color[1]*255), Math.round(color[2]*255), Math.round(color[3]*255)];

The next line converts the original array's range of 0-1 into 0-255. First, we would normally divide each value in the array by the highest number in the range we have, but in this case it's 1 so we don't need to. So instead we just multiply each value by the highest number in the range we're mapping to—255. We also round each value with Math.round().

"+\t\t\tR: " + color[0] + "\n\t\t\tG: " + color[1] + "\n\t\t\tB: " + color[2] + "\n\t\t\tA: " + color[3];

This final line is our text output. We put a plus in, which is where I set the anchor point of the layer, so it serves as a marker for our sample point, then we add in some tabs—at least the best we can hope for in After Effects—using \t and then a color channel, a colon and space and then a color. We do that for each piece adding in a newline character—\t—for each new color channel. And that's that.

Now, you can only sample one layer at a time. What if you need to sample how different layers interact, like transparency or blend modes? Put a blank Adjustment Layer above the others and select that layer in your Layer Control. The Adjustment Layer kind of takes on the values of the underlying layers, which is pretty useful. But it's also nice that you can sample specific layers without worrying about what's on top of them if you want to. So in the end, it's pretty flexible compared to the Photoshop version.

If you want to make this really helpful, turn it into a preset. Depending upon the font, font size, and font weight you choose, you may need to modify the amount of tabs—\t—you use in the final line of the expression. If you're Patron, you don't need to worry about that as I've made a preset for you. It's available in the same folder as all of the other presets!