Skip to main content
New Participant
September 21, 2023
Answered

A script that autofill layers with a random color?

  • September 21, 2023
  • 1 reply
  • 1375 views

Hello everyone! I have cryptomatte layers (output from a renderer) that are colored white. I need them to be filled with random colors so that when all the layers are turned on, I can identify the objects on the image. I have made a Photoshop action that does this on a single layer, but I've found out that it only selects the recorded color from the action and has no chance to randomize it every time I select a different layer. But I don't think making several actions just so I'll have different colors is practical. So I think scripting is required? This is the idea:

Currently, each object is isolated in separate layers. Thank you in advance!

This topic has been closed for replies.
Correct answer Stephen Marsh

@Stephen Marsh - Locked transparency and fill with random color is what I have in mind! 


quote

@Stephen Marsh - Locked transparency and fill with random color is what I have in mind! 


By @Enixanne

 

 

Try this:

 

/*
Fill Art Layers with Random Colour.jsx
v1.0 - 22nd September 2023, Stephen Marsh
https://community.adobe.com/t5/photoshop-ecosystem-discussions/a-script-that-autofill-layers-with-a-random-color/m-p/14103781
*/

#target photoshop

function main() {
    processAllLayersAndSets(app.activeDocument);
}

activeDocument.suspendHistory('Fill Art Layers with Random Colour.jsx', 'main()');


///// FUNCTIONS /////

function processAllLayersAndSets(obj) {
    // Process Layers 
    for (var i = obj.artLayers.length - 1; 0 <= i; i--) {
        activeDocument.activeLayer = obj.artLayers[i];
        var randomSolidColor = new SolidColor();
        var R = randomSolidColor.rgb.red = Math.round(Math.random() * 255);
        var G = randomSolidColor.rgb.green = Math.round(Math.random() * 255);
        var B = randomSolidColor.rgb.blue = Math.round(Math.random() * 255);
        lockTransparentPixels(true);
        randomFGcol(R, G, B);
        fill(100, true);
    }
    // Process Layer Set Layers 
    for (var i = obj.layerSets.length - 1; 0 <= i; i--) {
        processAllLayersAndSets(obj.layerSets[i]);
    }
}

function randomFGcol(red, grain, blue) {
	function s2t(s) {
        return app.stringIDToTypeID(s);
    }
	var descriptor = new ActionDescriptor();
	var descriptor2 = new ActionDescriptor();
	var reference = new ActionReference();
	reference.putProperty(s2t("color"), s2t("foregroundColor"));
	descriptor.putReference(s2t("null"), reference);
	descriptor2.putDouble(s2t("red"), red);
	descriptor2.putDouble(s2t("grain"), grain);
	descriptor2.putDouble(s2t("blue"), blue);
	descriptor.putObject(s2t("to"), s2t("RGBColor"), descriptor2);
	descriptor.putString(s2t("source"), "photoshopPicker");
	executeAction(s2t("set"), descriptor, DialogModes.NO);
}

function lockTransparentPixels(protectTransparency) {
	function s2t(s) {
        return app.stringIDToTypeID(s);
    }
	var descriptor = new ActionDescriptor();
	var descriptor2 = new ActionDescriptor();
	var reference = new ActionReference();
	reference.putEnumerated( s2t( "layer" ), s2t( "ordinal" ), s2t( "targetEnum" ));
	descriptor.putReference( s2t( "null" ), reference );
	descriptor2.putBoolean( s2t( "protectTransparency" ), protectTransparency );
	descriptor.putObject( s2t( "layerLocking" ), s2t( "layerLocking" ), descriptor2 );
	executeAction( s2t( "applyLocking" ), descriptor, DialogModes.NO );
}

function fill(opacity, preserveTransparency) {
        var s2t = function (s) {
            return app.stringIDToTypeID(s);
        };
        var descriptor = new ActionDescriptor();
        descriptor.putEnumerated(s2t("using"), s2t("fillContents"), s2t("foregroundColor"));
        descriptor.putUnitDouble(s2t("opacity"), s2t("percentUnit"), opacity);
        descriptor.putEnumerated(s2t("mode"), s2t("blendMode"), s2t("normal"));
        descriptor.putBoolean(s2t("preserveTransparency"), preserveTransparency);
        executeAction(s2t("fill"), descriptor, DialogModes.NO);
}

 

https://prepression.blogspot.com/2017/11/downloading-and-installing-adobe-scripts.html

1 reply

Stephen Marsh
Community Expert
Community Expert
September 21, 2023

@Enixanne 

 

A screenshot of the layers panel, or even better - a sample PSD is needed to illustrate your post.

EnixanneAuthor
New Participant
September 22, 2023

Thanks for the response! Here's a small section of the file. Currently, I have 100+ of these object layers that fills up the frame.

 

 

 

 

Stephen Marsh
Community Expert
Community Expert
September 22, 2023

@Enixanne – OK, there are two ways to go...

 

1) Lock transparency and fill each layer with a random colour

 

or

 

2) Add a clipped solid fill layer above each layer, to allow you to refine the non-destructive adjustment as required (doubling the layer count!)

 

Sample PSD attached... Which option do you prefer?