Skip to main content
Participant
August 27, 2021
Question

Is it possible to automate filling multiple selections with random colors?

  • August 27, 2021
  • 2 replies
  • 1226 views

Let's say I've got 1000 discreet squares in my .psd file. Would it be possible to fill them all with random colors? Preferably with no duplicates?

 

The actual final file will have probably somewhere between ~2000 and ~5000 shapes (not all of them regular), which should all filled with a flat, distinct RGB color. It could be done by hand... but I'd much prefer to automate this task, if possible.

 

Thanks in advance for reading and assistance!

This topic has been closed for replies.

2 replies

JJMack
Community Expert
Community Expert
August 27, 2021

Shape layer are fill layers with vector layer mask.  To have 5000 different solid color discreet shapes you would be dealing withe 5000 layers.   A pattern could have 5000 colors so you could flatten your 5000 shape layer define a define the is the merge if 5000 colored shapes. however if there was overlap between layers some shapes will be altered,  wiped, out or even blender into more than 5000 colors and shapes.

 

An active selection can have 5000  separate areas but it can not bet filled with 5000 colors.  5000 different selection can be filled with 5000 colors like as swatch color panel.

JJMack
Legend
August 27, 2021

There is one problem with shape layers - they need to be selected before changing the color (at least I couldn't do it any other way).I made a test document with 3200 layers. This code changes the color of the selected layers at a rate of about 3 layers per second. It's very slow 😞

 

#target photoshop

var s2t = stringIDToTypeID;

doProgress("", "main()")

function main() {

    (r = new ActionReference()).putProperty(s2t('property'), p = s2t('targetLayersIDs'));
    r.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));
    var lrs = executeActionGet(r).getList(p);

    for (var i = 0; i < lrs.count; i++) {
        app.updateProgress(i + 1, lrs.count);
        app.changeProgressText(i);

        (r = new ActionReference()).putIdentifier(s2t('layer'), lrs.getReference(i).getIdentifier(s2t('layerID')));
        (d = new ActionDescriptor()).putReference(s2t('null'), r);
        executeAction(s2t('select'), d, DialogModes.NO);
        (r = new ActionReference()).putEnumerated(s2t("contentLayer"), s2t("ordinal"), s2t("targetEnum"));
        (d = new ActionDescriptor()).putReference(s2t("null"), r);
        (d1 = new ActionDescriptor()).putDouble(s2t('red'), Math.random() * 255);
        d1.putDouble(s2t('grain'), Math.random() * 255);
        d1.putDouble(s2t('blue'), Math.random() * 255);
        (d2 = new ActionDescriptor()).putObject(s2t("color"), s2t("RGBColor"), d1);
        (d3 = new ActionDescriptor()).putObject(s2t("fillContents"), s2t("solidColorLayer"), d2);
        d.putObject(s2t("to"), s2t("shapeStyle"), d3);
        executeAction(s2t("set"), d, DialogModes.NO);
    }
}

 

Chuck Uebele
Community Expert
Community Expert
August 27, 2021

Yea, that many layers is going to really slow down PS. I do a lot of scripting creating gradients to layers and creating new layers. I end up doing it in pieces now and flattening a portion of it, just so I'm not sitting there all day.

Legend
August 27, 2021

what are squares? this selection, mask, pixels in one layer, pixels in different layers,  closed paths in one layer, vector shapes in different layers, etc.

 

it would be ideal if you could upload a test file that would make your task clear

 

P.s. set random color to all selected layers using fill FX

 

Participant
August 27, 2021

Thanks for your answer, this is about the gist of it. Specifically, I should have said multiple, seperate active selections (as in several disconnected selected areas on the same layer).

Chuck Uebele
Community Expert
Community Expert
August 28, 2021

So how is this selection made? Can you post an example? From what you describe, it might be possible to create a alpha mask of the selection, then go through that and detect each separate selection, and apply a random color, but that would still take a long time, even with a script.