Skip to main content
Known Participant
December 30, 2019
Answered

is it possible to move each color on its own layer?

  • December 30, 2019
  • 4 replies
  • 9473 views

hi all

I'm in search of a way (or script) that can seperate each object with the same fill color on its own layer.

The aim/scope is so that when I trace an image and limit the amount of colors I would like to have them in seperate layers for later processing in another app. Right now im doing it manually and its ok, but since I have plenty of artworks to do this manner it would help to have some automation/script/easier method of doing it.

 

thnks in advance

 

Correct answer Ton Frederiks
Does Image Trace ever produce CompoundPathItems? I thought it only produced
PathItems

You can find many Compound Patths after Image Trace.

Just check the Document Info Panel on Objects.

4 replies

Inventsable
Legend
January 1, 2020

Hi, here's a simple script that will arrange all pathItems of a specific color into separate layers:

 

function deliminateColors() {
    var doc = app.activeDocument, items = [];
    for (var i = doc.pathItems.length - 1; i >= 0; i--)
        doc.pathItems[i].filled ? items.push(doc.pathItems[i]) : null;
    items.forEach(function (item) {
        checkForLayer(rgbToHex(item.fillColor)) ? addToLayer(item) : createLayer(item);
    });
}
function checkForLayer(name) {
    var doc = app.activeDocument, found = false;
    for (var i = 0; i < doc.layers.length; i++)
        if (new RegExp("^" + name + "$").test(doc.layers[i].name))
            found = true;
    return found;
}
function createLayer(item) {
    var layer = app.activeDocument.layers.add();
    layer.name = rgbToHex(item.fillColor);
    layer.color = item.fillColor;
    addToLayer(item);
}
function addToLayer(item) {
    var layer = app.activeDocument.layers.getByName(rgbToHex(item.fillColor));
    item.move(layer, ElementPlacement.PLACEATEND);
}
function rgbToHex(color) {
    return "#" + [color.red, color.green, color.blue]
        .map(function (c) {
            c = c < 256 ? Math.abs(c).toString(16) : 0;
            return c.length < 2 ? "0" + c : c;
        }).join("");
}
Array.prototype.forEach = function (callback) {
    for (var i = 0; i < this.length; i++)
        callback(this[i], i, this);
};
Array.prototype.map = function (callback) {
    var mappedParam = [];
    for (var i = 0; i < this.length; i++)
        mappedParam.push(callback(this[i], i, this));
    return mappedParam;
};
deliminateColors();

 

Results in Layers panel on the left with a 16-color image trace for simplicity:

 

 

Since you're using Image Trace and didn't give too much detail, I assumed you only wanted fills to be checked, item names intact, layer names to be the specified color in hexadecimal form, the layer label to be equal to this color (though this would make selection hard, comment out the layer.color line in createLayer() to remove), and didn't touch the original layers just in case there's more to the document than these items.

Ton Frederiks
Community Expert
Community Expert
January 2, 2020

I somehow do not get the script working, it gives the following error:

Dave Creamer of IDEAS
Community Expert
Community Expert
January 2, 2020

I got that error too.

David Creamer: Community Expert (ACI and ACE 1995-2023)
Ton Frederiks
Community Expert
Community Expert
December 30, 2019

Actions will get you a long way.

You can start by opening an image in Illustrator, this has the advantage that the Swatches panel is empty.

Unfortunately Image Trace cannot be recorded with a preset, so you need to select one manually.

The rest can be semi automated:

An action to expand the Image Trace and adding the colors to the Swatches Panel

When a swatch is selected, an action can select the same fill colors, cut the selection, create a new Layer (recorded by clicking the New Layer Icon with the Cmd (or Ctrl for Win) key depressed to avoid recording naming the layer) and paste the result. You need to play this action for every color.

Lee_SKAuthor
Known Participant
December 31, 2019

never thought about using this method and record it into an action. It's not far off from my manual method, however this might help speed up the things a bit more. Will have to play around with it to see how well will I manage. I'll get back to you if you dont mind if i get stuck

thnks

Ton Frederiks
Community Expert
Community Expert
December 31, 2019

Hope you will get it working. I forgot to mention one thing:

To make it work, turn OFF "Paste Remembers Layers" in the Layers Panel menu.

Dave Creamer of IDEAS
Community Expert
Community Expert
December 30, 2019

When you say you are "doing it manually", you don't mean one object at a time--do you? Are you using the Select>Same>Fill Color menu? If you combine that with the Collect in New Layer menu option, you should be able to make an Action.

David Creamer: Community Expert (ACI and ACE 1995-2023)
Lee_SKAuthor
Known Participant
December 31, 2019

yes I use the same fill color  option, then when all that particular color is highlighted i just drag it to a new layer. Unfortunately everytime I tried the collect to new layer it doesnt work since it just make a duplicate of the whole layer into a sub layer (despite only 1 color is selected)

 

Inspiring
December 30, 2019

yes it is possible. but it will also depend on your artworks appearance layers. scripting has no way of cycling through appearance fills, and strokes to determine what is the top fill. it will also get tricky to determine the fill color of a group vs the fill color of each group member if you have any.