Skip to main content
Inspiring
Jul 16, 2024 6:03 AM UTC
Answered

script to get the color name of compound path

Hello, below function don't get the color name if parameter is compound path Item, how can this be achieved?

function getColor(compoundItem) {
var colorName;
if (compoundItem.strokeColor instanceof SpotColor) { 
var stColorName = compoundItem.stroke.spot.name;
if(stColorName === "Diecut"){
colorName= "Diecut"; 
}
}
return colorName;
}

This topic is closed to new replies. Start a new post to keep the conversation going.
m1b
m1bCorrect answer
Community Expert
Jul 16, 2024 7:48 AM UTC

Hi @rui huang, I've made a few tweaks to your function. To access the color(s) of a compoundPathItem, you must access it's first PathItem. For example:

function getStrokeColorName(compoundItem) {

    var colorName;

    if (
        !compoundItem.hasOwnProperty('pathItems')
        || 0 === compoundItem.pathItems.length
    )
        return;

    var firstPathItem = compoundItem.pathItems[0];

    if (firstPathItem.strokeColor instanceof SpotColor) {
        var stColorName = firstPathItem.strokeColor.spot.name;
        if (stColorName === "Diecut") {
            return stColorName;
        }
    }

    return colorName;
};

Note that this function returns undefined if no spot stroke color is found. As written, there is no reason for the `colorName` variable, but I assume you will need it for your code.

- Mark 

1 reply

m1b
Community Expert
m1bCommunity ExpertCorrect answer
Community Expert
Jul 16, 2024 7:48 AM UTC

Hi @rui huang, I've made a few tweaks to your function. To access the color(s) of a compoundPathItem, you must access it's first PathItem. For example:

function getStrokeColorName(compoundItem) {

    var colorName;

    if (
        !compoundItem.hasOwnProperty('pathItems')
        || 0 === compoundItem.pathItems.length
    )
        return;

    var firstPathItem = compoundItem.pathItems[0];

    if (firstPathItem.strokeColor instanceof SpotColor) {
        var stColorName = firstPathItem.strokeColor.spot.name;
        if (stColorName === "Diecut") {
            return stColorName;
        }
    }

    return colorName;
};

Note that this function returns undefined if no spot stroke color is found. As written, there is no reason for the `colorName` variable, but I assume you will need it for your code.

- Mark 

Translate