Check if given layer/channel has any color in it

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Tom, Kukurykus

Dariusz
Posts: 1
Joined: Sun Oct 30, 2016 12:43 pm

Check if given layer/channel has any color in it

Post by Dariusz »

Hey

I'm loading layer in to stack using my script. As I load them I'd like to check if a given layer has any color in it or its just black... - same goes for channels

Could any1 help me with how I can do it?

I know in channels if I ctrl + Click on channel thumbnail and there are no pixel in it I will get a warning message, maybe I can somehow use that to check if the a given layer/channel has color in it. Not sure.

The point of this tool is so that I can remove empty layers from start.

Regards
Dariusz
JavierAroche
Posts: 29
Joined: Sat Jul 30, 2016 3:52 am
Location: San Francisco

Re: Check if given layer/channel has any color in it

Post by JavierAroche »

That's a good idea to try to get the selection of the layer to see if it has any pixels. There is an Action Descriptor for it, but I'm not sure if it returns an error when executing it on an empty layer.

I have a different solution. You can validate the width and height of the selected layer (assuming the layer is selected when you load it in). If the width and height are 0, then your layer is empty. Actually, you only need to validate 1 of those. Either way, I have both in my code.

Code: Select all


// Get selected layer Action Descriptor
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var desc = executeActionGet(ref);

// Get layer bounds Action Descriptor
var boundsDesc = desc.getObjectValue( stringIDToTypeID( 'bounds' ) );

// Get layer width and height
var boundsWidth = boundsDesc.getUnitDoubleValue( stringIDToTypeID('width') );
var boundsHeight = boundsDesc.getUnitDoubleValue( stringIDToTypeID('height') );

// Validate layer pixels
if( boundsWidth > 0 && boundsHeight > 0 ) {
alert( 'Layer has pixels' );
} else {
alert( 'Empty layer' );
}