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

Participant ,
Dec 30, 2019

Copy link to clipboard

Copied

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

 

TOPICS
Draw and design, How to, Scripting, Third party plugins, Tools

Views

1.4K

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

1 Correct Answer

Adobe Community Professional , Jan 04, 2020
Ton Frederiks Adobe Community Professional , Jan 04, 2020
You can find many Compound Patths after Image Trace. Just check the Document Info Panel on Objects.

Likes

Translate

Translate
Jump to answer Jump to answer
Engaged ,
Dec 30, 2019

Copy link to clipboard

Copied

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. 

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Adobe Community Professional ,
Dec 30, 2019

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Lee8x AUTHOR
Participant ,
Dec 30, 2019

Copy link to clipboard

Copied

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)

 

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Adobe Community Professional ,
Dec 30, 2019

Copy link to clipboard

Copied

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.

Actions.png

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Lee8x AUTHOR
Participant ,
Dec 30, 2019

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Adobe Community Professional ,
Dec 31, 2019

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Adobe Community Professional ,
Dec 31, 2019

Copy link to clipboard

Copied

We've worked around the inability of AI Actions to record an Image Trace preset by editing the "Vectorizing Presets" in Preferences so the "Default" preset has the settings we want to use in the Action. If this would help in your process I can document the process. 
But a script is the best route since any Tracing preset can be used. 

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Engaged ,
Jan 01, 2020

Copy link to clipboard

Copied

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:

 

ice_screenshot_20200101-115616.png

 

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.

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Adobe Community Professional ,
Jan 02, 2020

Copy link to clipboard

Copied

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

Screenshot 2020-01-02 at 14.08.35.png

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Adobe Community Professional ,
Jan 02, 2020

Copy link to clipboard

Copied

I got that error too.

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Engaged ,
Jan 02, 2020

Copy link to clipboard

Copied

Looks like the prototype calls weren't being auto-executed in standalone files. Fixed by placing the function invocation at the bottom of the script and updated the original post, tested it outside my IDE as well. Should work now.

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Adobe Community Professional ,
Jan 02, 2020

Copy link to clipboard

Copied

Inventsable--Thank you for your hard work. Did  you edit the script on the original post? 

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Engaged ,
Jan 02, 2020

Copy link to clipboard

Copied

No worries. Yes I did, but it'd be nice to have someone else verify just in case I missed something again. Thanks!

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Adobe Community Professional ,
Jan 02, 2020

Copy link to clipboard

Copied

No errors this time, but it seems the script changes the colors of some objects.

Script vs actions.png

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Engaged ,
Jan 02, 2020

Copy link to clipboard

Copied

Hi Ton, the script should never assign to PathItem.fillColor and only read  from it. Can you provide this file?

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Adobe Community Professional ,
Jan 02, 2020

Copy link to clipboard

Copied

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Engaged ,
Jan 02, 2020

Copy link to clipboard

Copied

Hi Ton, the issue for this file is that the layers are created in the order which colors are detected, and there is no way to stack these 3 final layers in such a way that it completely preserves the all original path zIndex positions since overlaps exist within this sample. I may be wrong and can try flipping the stacking order to place objects at the bottom as they move to the layer, but I assume this is the problem. For example, in the script output when you rearrange the layer order:

 

ice_screenshot_20200102-164905.png

 

As you can see, nothing was actually removed or recolored but rather the stacking order was changed since the request was to put them in separate layers and layers must exist below or above one another. If a setting for Image Trace which had no overlap were used this would not be a problem, and I could solve this in a few different ways but it wasn't technically in scope of the original question so I didn't know that I had to, also without knowing what settings OP is using or anything about their desired output I'd only be making further assumptions that might not be what they'd intended.

 

If it's needed I can add it in

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Engaged ,
Jan 02, 2020

Copy link to clipboard

Copied

So just to clarify:

 

  • The script inevitably rearranges your artwork
  • In the event of overlaps, the color more recently found will always be placed on top
  • In most instances, Traces with larger color profiles will work better than lower, as seen in the 16 color capture below:

 

ice_screenshot_20200102-171733.png

 

In the above, the stacking order doesn't matter nearly as much so the inevitable rearranging tends to impact less because Curve Fitting for higher profiles seems to avoid overlapping (so when you toggle visibility of a color off, you don't see anything below except canvas/transparent) whereas 3 - 6 colors seem to prefer overlap. I did switch the color detection order in the original script and that produces the above result, which I think is 98% indistinguisable from the original and certainly pretty good.

 

But again, without knowing the actual requirements of the OP, it's not easy to create a script that has a perfect result.

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Engaged ,
Jan 02, 2020

Copy link to clipboard

Copied

It looks as if a single color is missing rather than it assigning new fills. Are these layers within layers or items which are not default PathItems? Is the Fill assigned to the object or group/layer Appearance?

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Adobe Community Professional ,
Jan 03, 2020

Copy link to clipboard

Copied

As far as I can see the script removes all compound paths, resulting in flat areas where holes should be.

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Adobe Community Professional ,
Jan 04, 2020

Copy link to clipboard

Copied

I did a simple testt, the script does not select and move compound paths, but selects and moves the individual paths that make the compound paths (and have the same color).

Before after.jpg

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Engaged ,
Jan 04, 2020

Copy link to clipboard

Copied

Does Image Trace ever produce CompoundPathItems? I thought it only produced
PathItems

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Adobe Community Professional ,
Jan 04, 2020

Copy link to clipboard

Copied

You can find many Compound Patths after Image Trace.

Just check the Document Info Panel on Objects.

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Lee8x AUTHOR
Participant ,
Jan 03, 2020

Copy link to clipboard

Copied

sorry for delay in replying, so much input from you all i'm flattered 🙂 thanks!

i'm gonna read all replies and check the script, and will also give an example of my workload/flow to have an idea what I meant and what I was looking for. Will get back to you all later today since i'm currently at work.

 

thanks once again for now and will get back to you all shortly

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more