Skip to main content
Inspiring
July 16, 2024
Answered

script to get the color name of compound path

  • July 16, 2024
  • 1 reply
  • 207 views

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 has been closed for replies.
Correct answer m1b

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
July 16, 2024

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