Skip to main content
Inspiring
June 6, 2020
Answered

Split into Layers by Index Colors, Read Index Colors

  • June 6, 2020
  • 2 replies
  • 6791 views

Hello,

I would like to split an image into a layer by its colors. I've searched the forum and found one, but it crashes, unfortunately. The other one seems to be working which I've found on StackExchange. But it's also extremely slow. So I thought maybe if it would be more practical to convert image into index color space. And let the user define 16,32,64...256 colors. Till this step, I think it can be done via script listener. (By the way, I guess it's only possible with Local Adaptive, Perceptual, and Selective options to define the custom color amount. So, for example first temporary convert colors into index colors in another temporary document. Create channels, copy and paste channels into previous RGB image as channels and then split into layers from channel selections.

 

But I couldn't be sure how to read each color in an index color table and their overall values.

I need this script mostly for separating vector-based or simple illustrations which include for example 16 colors only.

I wanted to ask before working on a script because maybe you can enlighten me with some other or easier method to do that.

 

Thank you.

 

Examples I've found;

1st one: This one separates only RGB channels as far as I understood.(from sizeoverload.com site)

#target photoshop
var doc = app.activeDocument;
var fuzz = 200;
var shadeNumber = 3;
var shadeValue = 110;

var colorRange = new Array();
colorRange[0] = {
    red: 255,
    green: 0,
    blue: 0
};
colorRange[1] = {
    red: 0,
    green: 255,
    blue: 0
};
colorRange[2] = {
    red: 0,
    green: 0,
    blue: 255
};

function convertToDuotone() {
    // write options for indexed color mode here 
    myObject = new IndexedConversionOptions();
    myObject.palette = Palette.LOCALADAPTIVE;
    // how many colors to split/filter the image into
    myObject.colors = shadeNumber;
    doc.changeMode(ChangeMode.INDEXEDCOLOR, myObject);
};

function convertToRGB() {
    doc.changeMode(ChangeMode.RGB);
}

function setColorTable() {
    var idsetd = charIDToTypeID("setd");
    var desc20 = new ActionDescriptor();
    var idnull = charIDToTypeID("null");
    var ref6 = new ActionReference();
    var idClr = charIDToTypeID("Clr ");
    var idClrT = charIDToTypeID("ClrT");
    ref6.putProperty(idClr, idClrT);
    desc20.putReference(idnull, ref6);
    var idT = charIDToTypeID("T   ");
    var list1 = new ActionList();
    for (var i = 0; i < shadeNumber; i++) {
        var tableColor = new ActionDescriptor();
        var idRd = charIDToTypeID("Rd  ");
        tableColor.putDouble(idRd, colorRange[i]['red']);
        var idGrn = charIDToTypeID("Grn ");
        tableColor.putDouble(idGrn, colorRange[i]['green']);
        var idBl = charIDToTypeID("Bl  ");
        tableColor.putDouble(idBl, colorRange[i]['blue']);
        var idRGBC = charIDToTypeID("RGBC");
        list1.putObject(idRGBC, tableColor);
    }
    desc20.putList(idT, list1);
    var idTrnI = charIDToTypeID("TrnI");
    desc20.putInteger(idTrnI, shadeNumber);
    executeAction(idsetd, desc20, DialogModes.NO);
}


convertToDuotone();

setColorTable();

convertToRGB();

doc.selection.deselect();


for (var i = 0; i < 3; i++) {

    var selectColour = new SolidColor();
    selectColour.rgb.red = colorRange[i]['red'];
    selectColour.rgb.green = colorRange[i]['green'];
    selectColour.rgb.blue = colorRange[i]['blue'];

    var scaleA = (selectColour.lab.a + 128.5) / 256;
    var desc11 = new ActionDescriptor();
    desc11.putInteger(charIDToTypeID("Fzns"), fuzz);
    var desc12 = new ActionDescriptor();
    desc12.putDouble(charIDToTypeID("Lmnc"), selectColour.lab.l);
    desc12.putDouble(charIDToTypeID("A   "), selectColour.lab.a + scaleA);
    desc12.putDouble(charIDToTypeID("B   "), selectColour.lab.b + scaleA);
    desc11.putObject(charIDToTypeID("Mnm "), charIDToTypeID("LbCl"), desc12);
    desc11.putObject(charIDToTypeID("Mxm "), charIDToTypeID("LbCl"), desc12);
    executeAction(charIDToTypeID("ClrR"), desc11, DialogModes.NO);
    if (hasSelection() == true) {
        var layerRef = doc.artLayers.add();

        var desc140 = new ActionDescriptor();
        desc140.putClass(charIDToTypeID('Nw  '), charIDToTypeID('Chnl'));
        var ref51 = new ActionReference();
        ref51.putEnumerated(charIDToTypeID('Chnl'), charIDToTypeID('Chnl'), charIDToTypeID('Msk '));
        desc140.putReference(charIDToTypeID('At  '), ref51);
        desc140.putEnumerated(charIDToTypeID('Usng'), charIDToTypeID('UsrM'), charIDToTypeID('RvlS'));
        executeAction(charIDToTypeID('Mk  '), desc140, DialogModes.NO);
        doc.activeChannels = [doc.channels[0], doc.channels[1], doc.channels[2]]

        var myColor = new SolidColor();
        myColor.rgb.red = myColor.rgb.green = myColor.rgb.blue = shadeValue;
        app.activeDocument.selection.selectAll();

        app.activeDocument.selection.fill(myColor, undefined, undefined, true);
        app.activeDocument.selection.fill(myColor);
        
        if ((shadeValue+36) > 255)
        {
            shadeValue = 255;
        } else {
            shadeValue += 36;
        };
    };
};

function hasSelection() {
    var ref = new ActionReference();
    ref.putEnumerated(charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
    var docDesc = executeActionGet(ref);
    var hasSelection = docDesc.hasKey(stringIDToTypeID("selection"));
    return hasSelection;
};

doc.selection.deselect();

 

And the other one from stackexchange.com;

#target photoshop

var ChannelIDs = {
    RED: "Rd  ",
    GREEN: "Grn ",
    BLUE: "Bl  ",
    RGB: "RGB "
};

function isSelectionEmpty(doc) {
    try {
        return (doc.selection.bounds) ? false : true;
    } catch (err) {
        return true;
    }
}

function createChannelFromSelection(doc, channelName) {
    var chan = doc.channels.add();
    chan.name = channelName;
    chan.kind = ChannelType.SELECTEDAREA;
    doc.selection.store(chan, SelectionType.REPLACE);
}

function cutToLayer() { // "Layer via cut"
    executeAction(charIDToTypeID("CtTL"), undefined, DialogModes.NO);
}

function createSnapshot(snapshotName) {
    var makDescriptor = new ActionDescriptor(),
        snapshotAction = new ActionReference(),
        fromRef = new ActionReference();

    snapshotAction.putClass(charIDToTypeID("SnpS"));
    makDescriptor.putReference(charIDToTypeID("null"), snapshotAction);
    fromRef.putProperty(charIDToTypeID("HstS"), charIDToTypeID("CrnH"));
    makDescriptor.putReference(charIDToTypeID("From"), fromRef);

    if (snapshotName) { // Assign snapshot name
        makDescriptor.putString(charIDToTypeID("Nm  "), snapshotName );
        makDescriptor.putEnumerated(charIDToTypeID("Usng"), charIDToTypeID("HstS"), charIDToTypeID("FllD") );
    }

    executeAction(charIDToTypeID("Mk  "), makDescriptor, DialogModes.NO);
}

function restoreSnapshot(snapshotName) {
    if (!snapshotName) { throw new Error("Expected snapshot name"); }

    var selectDescriptor = new ActionDescriptor(),
        snapshotRef = new ActionReference();

    snapshotRef.putName(charIDToTypeID("SnpS"), snapshotName);
    selectDescriptor.putReference(charIDToTypeID("null"), snapshotRef);
    executeAction(charIDToTypeID("slct"), selectDescriptor, DialogModes.NO);
}

function selectChannel(channelId) {
    if (!channelId) { throw new Error("Expected channel ID"); }

    var setDescriptor = new ActionDescriptor(),
        selectRef = new ActionReference(),
        channelRef = new ActionReference(),
        idChnl = charIDToTypeID("Chnl");

    selectRef.putProperty(idChnl, charIDToTypeID("fsel"));
    setDescriptor.putReference(charIDToTypeID("null"), selectRef);
    channelRef.putEnumerated(idChnl, idChnl, charIDToTypeID(channelId));
    setDescriptor.putReference(charIDToTypeID("T   "), channelRef);
    executeAction(charIDToTypeID("setd"), setDescriptor, DialogModes.NO);
};

function loadSelection(docName, channelName) {
    var setDescriptor = new ActionDescriptor(),
        selectRef = new ActionReference(),
        docRef = new ActionReference(),
        idChnl = charIDToTypeID("Chnl");

    selectRef.putProperty(idChnl, charIDToTypeID("fsel"));
    setDescriptor.putReference(charIDToTypeID("null"), selectRef);
    docRef.putName(idChnl, channelName);
    docRef.putName(charIDToTypeID("Dcmn"), docName);
    setDescriptor.putReference(charIDToTypeID("T   "), docRef);
    executeAction(charIDToTypeID("setd"), setDescriptor, DialogModes.NO);
}

function convertToIndexedColour(doc) {
    var opts = new IndexedConversionOptions();

    opts.dither = Dither.NONE;
    opts.forced = ForcedColors.NONE;
    opts.matte = MatteType.NONE;
    opts.palette = Palette.EXACT;
    opts.transparency = false;

    doc.changeMode(ChangeMode.INDEXEDCOLOR, opts);
}

function selectColourTableEntry(i) {
    var actionSet = charIDToTypeID("setd");
    var setDescriptor = new ActionDescriptor();
    var ref = new ActionReference();
    ref.putProperty(charIDToTypeID("Clr "), charIDToTypeID("ClrT"));
    setDescriptor.putReference(charIDToTypeID("null"), ref);

    var colourTable = new ActionList();

    if (i > 0) { // Fill low part of colour table
        for (var n = 0; n < i; ++n) {
            addColourTableEntry(colourTable, 0, 0, 0);
        }
    }

    // Select single entry
    addColourTableEntry(colourTable, 255, 255, 255);

    if (i < 255) { // Fill high part of colour table
        for (var n = i + 1; n < 256; ++n) {
            addColourTableEntry(colourTable, 0, 0, 0);
        }
    }

    setDescriptor.putList(charIDToTypeID("T   "), colourTable);
    executeAction(actionSet, setDescriptor, DialogModes.NO);
}

function addColourTableEntry(colourTable, r, g, b) {
    var entry = new ActionDescriptor();
    entry.putDouble(charIDToTypeID(ChannelIDs.RED), r);
    entry.putDouble(charIDToTypeID(ChannelIDs.GREEN), g);
    entry.putDouble(charIDToTypeID(ChannelIDs.BLUE), b);
    colourTable.putObject(charIDToTypeID("RGBC"), entry);
}

function main() {
    var doc = activeDocument;
    var lyr = doc.activeLayer;

    // Create indexed duplicate
    var dupDoc = doc.duplicate();
    activeDocument = dupDoc;
    convertToIndexedColour(dupDoc);

    var snapshotName = "indexed";
    var channelName = "matte";
    var i;

    // Snapshot the current history state
    createSnapshot(snapshotName);

    for (i = 0; i < 256; ++i) {
        selectColourTableEntry(i);

        // For some reason selecting channels doesn't work in indexed mode?
        dupDoc.changeMode(ChangeMode.RGB);

        // Create new matte from red channel
        selectChannel(ChannelIDs.RED);

        // If there is no selection then we're done
        if (isSelectionEmpty(dupDoc)) { break; }

        createChannelFromSelection(dupDoc, channelName);

        // Use matte to cut new layer in original document
        activeDocument = doc;
        loadSelection(dupDoc.name, channelName);
        cutToLayer();
        doc.activeLayer.name = "Region " + (i + 1);

        // Reset ready for next layer
        doc.activeLayer = lyr;
        activeDocument = dupDoc;
        restoreSnapshot(snapshotName);
    }

    // Clean up
    activeDocument = dupDoc;
    dupDoc.close(SaveOptions.DONOTSAVECHANGES);

    activeDocument = doc;
    doc.activeLayer.remove();

    alert("Found " + i + " region" + (i === 1 ? "" : "s"));
}

main();

 

This topic has been closed for replies.
Correct answer r-bin
Try it.
In the conversion dialog, specify the number of colors and the method visually controlling the image when the Preview checkbox is turned on.
if (app.documents.length) app.activeDocument.suspendHistory("Split Colors", "main()"); 

function main()
    {
    try {
        var doc0 = app.activeDocument;
    
        doc0.flatten();
    
        var layer0 = doc0.activeLayer;
    
        var doc1 = doc0.duplicate("tmp");
    
        var color_cnt = 16;
    
        var d = new ActionDescriptor();
        var d1 = new ActionDescriptor();
        d1.putEnumerated(stringIDToTypeID("palette"), stringIDToTypeID("colorPalette"), stringIDToTypeID("perceptual"));
        d1.putInteger(stringIDToTypeID("colors"), color_cnt);
        d1.putEnumerated(stringIDToTypeID("forcedColors"), stringIDToTypeID("forcedColors"), stringIDToTypeID("none"));
        d1.putBoolean(stringIDToTypeID("transparency"), false);
        d.putObject(stringIDToTypeID("to"), stringIDToTypeID("indexedColorMode"), d1);
        d = executeAction(stringIDToTypeID("convertMode"), d, DialogModes.ALL);
    
        if (!d.hasKey(stringIDToTypeID("to"))) return;
    
        color_cnt = d.getObjectValue(stringIDToTypeID("to")).getInteger(stringIDToTypeID("colors"));
    
        var tmp_file = new File(Folder.temp.fsName + "/" + "~tmp.psd");
    
        var d = new ActionDescriptor();
        var d1 = new ActionDescriptor();
        d1.putBoolean(stringIDToTypeID("maximizeCompatibility"), false);
        d.putObject(stringIDToTypeID("as"), stringIDToTypeID("photoshop35Format"), d1);
        d.putPath(stringIDToTypeID("in"), tmp_file);
        d.putBoolean(stringIDToTypeID("copy"), true);
        d.putBoolean(stringIDToTypeID("spot"), false);
        d.putBoolean(stringIDToTypeID("alphaChannels"), false);
        d.putBoolean(stringIDToTypeID("layers"), false);
        d.putBoolean(stringIDToTypeID("embedProfiles"), false);
        d.putBoolean(stringIDToTypeID("annotType"), false);
        executeAction(stringIDToTypeID("save"), d, DialogModes.NO);
        
        var s = "";
        
        if (!tmp_file.open("r")) { throw("tmp file open error"); }
        tmp_file.encoding = "BINARY";
    
        tmp_file.seek(25, 0);
        
        s = tmp_file.read(1);
        
        if (s.charCodeAt(0) != 2) { throw("wrong color mode"); }
        
        s = tmp_file.read(4);
        
        var len = s.charCodeAt(0)<<24 | s.charCodeAt(1)<<16 | s.charCodeAt(2)<<8 | s.charCodeAt(3);
        
        if (len != 0x300) { throw("wrong color len"); }
        
        var table = tmp_file.read(len);
        
        s = tmp_file.read(4);
        
        var len = s.charCodeAt(0)<<24 | s.charCodeAt(1)<<16 | s.charCodeAt(2)<<8 | s.charCodeAt(3);
        
        var data = tmp_file.read(len);
    
        tmp_file.close();
        tmp_file.remove();
    
        var color_cnt = 256;
    
        var n = data.indexOf("8BIM"+ String.fromCharCode(0x04,0x16,0,0,0,0,0,2));
    
        if (n >= 0) color_cnt = data.charCodeAt(n+12)<<8 | data.charCodeAt(n+13);
    
        app.preferences.rulerUnits = Units.PIXELS;
    
        var w = Number(doc1.width.value);
        var h = Number(doc1.height.value);
    
        doc1.resizeCanvas(w+1, h, AnchorPosition.TOPLEFT);
        
        var x = w;
        var y = 0;
    
        for (var i = 0; i < color_cnt; i++)
            {
            app.activeDocument = doc1;
    
            var color = new SolidColor();
    
            color.rgb.red   = table.charCodeAt(i);    
            color.rgb.green = table.charCodeAt(256+i);
            color.rgb.blue  = table.charCodeAt(512+i);
    
            doc1.selection.select([[x,y],[x+1,y],[x+1,y+1],[x,y+1]]);
            doc1.selection.fill(color);
    
            var d = new ActionDescriptor();
    
            var r = new ActionReference();
            r.putProperty(stringIDToTypeID("channel"), stringIDToTypeID("selection"));
            d.putReference(stringIDToTypeID("null"), r);
            var d1 = new ActionDescriptor();
            d1.putUnitDouble(stringIDToTypeID("horizontal"), stringIDToTypeID("pixelsUnit"), x);
            d1.putUnitDouble(stringIDToTypeID("vertical"),   stringIDToTypeID("pixelsUnit"), y);
            d.putObject(stringIDToTypeID("to"), stringIDToTypeID("point"), d1);
            d.putInteger(stringIDToTypeID("tolerance"), 0);
            d.putBoolean(stringIDToTypeID("contiguous"), false);
            executeAction(stringIDToTypeID("set"), d, DialogModes.NO);
    
            var sel_name = save_selection();
    
            dup_channel(sel_name, doc1, doc0);

            del_channel(sel_name);
    
            app.activeDocument = doc0;
            doc0.activeLayer = layer0;
    
            load_selection(sel_name);
    
            del_channel(sel_name);
    
            executeAction(stringIDToTypeID("copyToLayer"), undefined, DialogModes.NO); 
            doc0.activeLayer.name = color.rgb.red + " " + color.rgb.green + " " + color.rgb.blue;
            }
    
    
        doc1.close(SaveOptions.DONOTSAVECHANGES);
    
        app.activeDocument = doc0;
        layer0.visible = false;
    
        alert("Done");
        }
    catch (e) { if (doc1) doc1.close(SaveOptions.DONOTSAVECHANGES); alert(e); }
    }

function save_selection(name)
    {
    try {
        if (name == undefined) name = Number(Math.random().toString().substr(2)).toString(36);

        var d = new ActionDescriptor();
        var r = new ActionReference();
        r.putProperty(stringIDToTypeID("channel"), stringIDToTypeID("selection"));
        d.putReference(stringIDToTypeID("null"), r);
        d.putString(stringIDToTypeID("name"), name);

        executeAction(stringIDToTypeID("duplicate"), d, DialogModes.NO);

        return name;
        }
    catch (e) { throw(e); }
    }

function load_selection(name)
    {
    try {
        var d = new ActionDescriptor();
        var r = new ActionReference();
        r.putProperty(stringIDToTypeID("channel"), stringIDToTypeID("selection"));
        d.putReference(stringIDToTypeID("null"), r);
        var r1 = new ActionReference();
        r1.putName(stringIDToTypeID("channel"), name);
        d.putReference(stringIDToTypeID("to"), r1);
        executeAction(stringIDToTypeID("set"), d, DialogModes.NO);
        }
    catch (e) { throw(e); }
    }

function del_channel(name)
    {
    try {
        var d = new ActionDescriptor();
        var r = new ActionReference();
        r.putName(stringIDToTypeID("channel"), name);
        d.putReference(stringIDToTypeID("null"), r);
        executeAction(stringIDToTypeID("delete"), d, DialogModes.NO);
        }
    catch (e) { throw(e); }
    }

function dup_channel(name, src_doc, dst_doc)
    {
    try {
        app.activeDocument = src_doc;

        var d = new ActionDescriptor();

        var r1 = new ActionReference();
        r1.putName(stringIDToTypeID("channel"), name);
        d.putReference(stringIDToTypeID("null"), r1);

        var r2 = new ActionReference();
        r2.putIdentifier(stringIDToTypeID("document"), dst_doc.id);
        d.putReference(stringIDToTypeID("to"), r2);

        d.putString(stringIDToTypeID("name"), name);

        executeAction(stringIDToTypeID("duplicate"), d, DialogModes.NO);
        }
    catch (e) { throw(e); }
    }

2 replies

r-binCorrect answer
Legend
June 7, 2020
Try it.
In the conversion dialog, specify the number of colors and the method visually controlling the image when the Preview checkbox is turned on.
if (app.documents.length) app.activeDocument.suspendHistory("Split Colors", "main()"); 

function main()
    {
    try {
        var doc0 = app.activeDocument;
    
        doc0.flatten();
    
        var layer0 = doc0.activeLayer;
    
        var doc1 = doc0.duplicate("tmp");
    
        var color_cnt = 16;
    
        var d = new ActionDescriptor();
        var d1 = new ActionDescriptor();
        d1.putEnumerated(stringIDToTypeID("palette"), stringIDToTypeID("colorPalette"), stringIDToTypeID("perceptual"));
        d1.putInteger(stringIDToTypeID("colors"), color_cnt);
        d1.putEnumerated(stringIDToTypeID("forcedColors"), stringIDToTypeID("forcedColors"), stringIDToTypeID("none"));
        d1.putBoolean(stringIDToTypeID("transparency"), false);
        d.putObject(stringIDToTypeID("to"), stringIDToTypeID("indexedColorMode"), d1);
        d = executeAction(stringIDToTypeID("convertMode"), d, DialogModes.ALL);
    
        if (!d.hasKey(stringIDToTypeID("to"))) return;
    
        color_cnt = d.getObjectValue(stringIDToTypeID("to")).getInteger(stringIDToTypeID("colors"));
    
        var tmp_file = new File(Folder.temp.fsName + "/" + "~tmp.psd");
    
        var d = new ActionDescriptor();
        var d1 = new ActionDescriptor();
        d1.putBoolean(stringIDToTypeID("maximizeCompatibility"), false);
        d.putObject(stringIDToTypeID("as"), stringIDToTypeID("photoshop35Format"), d1);
        d.putPath(stringIDToTypeID("in"), tmp_file);
        d.putBoolean(stringIDToTypeID("copy"), true);
        d.putBoolean(stringIDToTypeID("spot"), false);
        d.putBoolean(stringIDToTypeID("alphaChannels"), false);
        d.putBoolean(stringIDToTypeID("layers"), false);
        d.putBoolean(stringIDToTypeID("embedProfiles"), false);
        d.putBoolean(stringIDToTypeID("annotType"), false);
        executeAction(stringIDToTypeID("save"), d, DialogModes.NO);
        
        var s = "";
        
        if (!tmp_file.open("r")) { throw("tmp file open error"); }
        tmp_file.encoding = "BINARY";
    
        tmp_file.seek(25, 0);
        
        s = tmp_file.read(1);
        
        if (s.charCodeAt(0) != 2) { throw("wrong color mode"); }
        
        s = tmp_file.read(4);
        
        var len = s.charCodeAt(0)<<24 | s.charCodeAt(1)<<16 | s.charCodeAt(2)<<8 | s.charCodeAt(3);
        
        if (len != 0x300) { throw("wrong color len"); }
        
        var table = tmp_file.read(len);
        
        s = tmp_file.read(4);
        
        var len = s.charCodeAt(0)<<24 | s.charCodeAt(1)<<16 | s.charCodeAt(2)<<8 | s.charCodeAt(3);
        
        var data = tmp_file.read(len);
    
        tmp_file.close();
        tmp_file.remove();
    
        var color_cnt = 256;
    
        var n = data.indexOf("8BIM"+ String.fromCharCode(0x04,0x16,0,0,0,0,0,2));
    
        if (n >= 0) color_cnt = data.charCodeAt(n+12)<<8 | data.charCodeAt(n+13);
    
        app.preferences.rulerUnits = Units.PIXELS;
    
        var w = Number(doc1.width.value);
        var h = Number(doc1.height.value);
    
        doc1.resizeCanvas(w+1, h, AnchorPosition.TOPLEFT);
        
        var x = w;
        var y = 0;
    
        for (var i = 0; i < color_cnt; i++)
            {
            app.activeDocument = doc1;
    
            var color = new SolidColor();
    
            color.rgb.red   = table.charCodeAt(i);    
            color.rgb.green = table.charCodeAt(256+i);
            color.rgb.blue  = table.charCodeAt(512+i);
    
            doc1.selection.select([[x,y],[x+1,y],[x+1,y+1],[x,y+1]]);
            doc1.selection.fill(color);
    
            var d = new ActionDescriptor();
    
            var r = new ActionReference();
            r.putProperty(stringIDToTypeID("channel"), stringIDToTypeID("selection"));
            d.putReference(stringIDToTypeID("null"), r);
            var d1 = new ActionDescriptor();
            d1.putUnitDouble(stringIDToTypeID("horizontal"), stringIDToTypeID("pixelsUnit"), x);
            d1.putUnitDouble(stringIDToTypeID("vertical"),   stringIDToTypeID("pixelsUnit"), y);
            d.putObject(stringIDToTypeID("to"), stringIDToTypeID("point"), d1);
            d.putInteger(stringIDToTypeID("tolerance"), 0);
            d.putBoolean(stringIDToTypeID("contiguous"), false);
            executeAction(stringIDToTypeID("set"), d, DialogModes.NO);
    
            var sel_name = save_selection();
    
            dup_channel(sel_name, doc1, doc0);

            del_channel(sel_name);
    
            app.activeDocument = doc0;
            doc0.activeLayer = layer0;
    
            load_selection(sel_name);
    
            del_channel(sel_name);
    
            executeAction(stringIDToTypeID("copyToLayer"), undefined, DialogModes.NO); 
            doc0.activeLayer.name = color.rgb.red + " " + color.rgb.green + " " + color.rgb.blue;
            }
    
    
        doc1.close(SaveOptions.DONOTSAVECHANGES);
    
        app.activeDocument = doc0;
        layer0.visible = false;
    
        alert("Done");
        }
    catch (e) { if (doc1) doc1.close(SaveOptions.DONOTSAVECHANGES); alert(e); }
    }

function save_selection(name)
    {
    try {
        if (name == undefined) name = Number(Math.random().toString().substr(2)).toString(36);

        var d = new ActionDescriptor();
        var r = new ActionReference();
        r.putProperty(stringIDToTypeID("channel"), stringIDToTypeID("selection"));
        d.putReference(stringIDToTypeID("null"), r);
        d.putString(stringIDToTypeID("name"), name);

        executeAction(stringIDToTypeID("duplicate"), d, DialogModes.NO);

        return name;
        }
    catch (e) { throw(e); }
    }

function load_selection(name)
    {
    try {
        var d = new ActionDescriptor();
        var r = new ActionReference();
        r.putProperty(stringIDToTypeID("channel"), stringIDToTypeID("selection"));
        d.putReference(stringIDToTypeID("null"), r);
        var r1 = new ActionReference();
        r1.putName(stringIDToTypeID("channel"), name);
        d.putReference(stringIDToTypeID("to"), r1);
        executeAction(stringIDToTypeID("set"), d, DialogModes.NO);
        }
    catch (e) { throw(e); }
    }

function del_channel(name)
    {
    try {
        var d = new ActionDescriptor();
        var r = new ActionReference();
        r.putName(stringIDToTypeID("channel"), name);
        d.putReference(stringIDToTypeID("null"), r);
        executeAction(stringIDToTypeID("delete"), d, DialogModes.NO);
        }
    catch (e) { throw(e); }
    }

function dup_channel(name, src_doc, dst_doc)
    {
    try {
        app.activeDocument = src_doc;

        var d = new ActionDescriptor();

        var r1 = new ActionReference();
        r1.putName(stringIDToTypeID("channel"), name);
        d.putReference(stringIDToTypeID("null"), r1);

        var r2 = new ActionReference();
        r2.putIdentifier(stringIDToTypeID("document"), dst_doc.id);
        d.putReference(stringIDToTypeID("to"), r2);

        d.putString(stringIDToTypeID("name"), name);

        executeAction(stringIDToTypeID("duplicate"), d, DialogModes.NO);
        }
    catch (e) { throw(e); }
    }
Inspiring
June 8, 2020

@r-bin Thank you very much, it's a very handy script. And it's also very fast. It's far better than I've excepted. Thank you once again.

Regards. 

Legend
June 9, 2020
Part of the code is taken from here.
 
JJMack
Community Expert
Community Expert
June 6, 2020

I think the script would need to convert the image to Index color  16 to 256 mapped colors. then  make selections for each of the index colors  convert selections to a path and create shape layer for each of the index colors.  I would expect the script to take quite some time to run. You may need to fiddle with the convert to Path process to get an acceptable path.  You can also try the Photopea website to see how long it takes their process to create the PSD file you want from your image.  I believe their process support 256 colors.  Photopea is somewhat like a web application photoshop, photopea 

 

If you install Illustrator I don't it has some trace feature you may want to look into. Image trace 

JJMack
JJMack
Community Expert
Community Expert
June 6, 2020

Just looked at Photopea it only goes up to 50 colors and it take a long time

 

Photopea converts smaller simple images with few color fast and well.

 

JJMack
Inspiring
June 6, 2020

I didn't know such application it's good to know thank you. Actually as you said illustrator's image trace would be really handy since you are able to define amount of colors. But on the hand as rasters it would be more practical for me and I guess it would also be useful for some artists who are doing matte painting,comics artists and etc...It's not my field but as I've noticed speaking about comics they generally have limited color palette compared to a photograph. So I still believe index color would be possible solution but I couldn't find an info to read index color table. I just need, how many colors are there and their rgb values to make separation. For the rest I'll try to do it myself and will share here. (Btw i'm not a coder but still trying to learn and improve myself).