Adobe Illustrator JS脚本:如何识别全局CMYK印刷色?
识别Illustrator全局CMYK印刷色色板的JSX脚本问题
编写了一段Adobe Illustrator JSX脚本,旨在遍历当前文档色板中的所有色样,识别出CMYK印刷色并进行处理,但在识别全局色时遇到困难:大部分色样为全局色,使用swatch.color.typename、swatch.color.space、swatch.color.colorType和swatch.color.colorModel属性时,全局印刷色的颜色模式返回undefined,typename返回"SPOT COLOR",无法正确识别。
原脚本内容
// Get the swatches var swatches = app.activeDocument.swatches; /*** DO NOT USE var ColorModel = {CMYK: "CMYK", RGB: "RGB", LAB: "LAB", GRAY: "GRAY", SPOT: "SPOT", CMYK_PROCESS: "CMYK_PROCESS"}; ***/ // Set the progress bar var progressWindow = new Window("palette", "Script Progress", [150, 150, 600, 200]); progressWindow.bar = progressWindow.add("progressbar", [20, 35, 420, 60], 0, swatches.length); progressWindow.show(); // Function to round colors function roundColorValues(color) { var col = color; col.cyan = Math.round(col.cyan); col.magenta = Math.round(col.magenta); col.yellow = Math.round(col.yellow); col.black = Math.round(col.black); } // Function to set colors function setColorValues(color) { var col = color; if (col.cyan <= 3) { col.cyan = 0; } else if (col.magenta <= 3) { col.magenta = 0; } else if (col.yellow <= 3) { col.yellow = 0; } else if (col.black <= 3) { col.black = 0; } } // Loop through the swatches for (i = 0; i < swatches.length; i++) { // Update the progress bar progressWindow.bar.value = i + 1; progressWindow.update(); // Get the swatch var swatch = swatches[i]; var colorModel = swatch.color.colorModel; // Check if swatch is CMYK if (colorModel == ColorModel.CMYK) { alert("CMYK Color Found"); roundColorValues(swatch.color); setColorValues(swatch.color); } } // Close the progress bar progressWindow.close(); // Show the alert alert("Complete!");
问题原因与解决方案
Illustrator中全局印刷色(Process Global Color)的结构特殊:它的swatch.color是SpotColor类型,但内部通过processColor属性存储实际的CMYK颜色信息。直接访问swatch.color.colorModel会返回undefined,因为SpotColor本身没有这个属性,需要访问其内部的processColor对象。
同时原脚本的setColorValues函数使用else if会导致仅第一个符合条件的通道被置0,需改为独立的if判断,确保所有符合条件的通道都能被处理。
修改后的脚本
// Get the swatches var swatches = app.activeDocument.swatches; // Set the progress bar var progressWindow = new Window("palette", "Script Progress", [150, 150, 600, 200]); progressWindow.bar = progressWindow.add("progressbar", [20, 35, 420, 60], 0, swatches.length); progressWindow.show(); // Function to round CMYK color values function roundColorValues(color) { color.cyan = Math.round(color.cyan); color.magenta = Math.round(color.magenta); color.yellow = Math.round(color.yellow); color.black = Math.round(color.black); } // Function to set low color values to 0 function setColorValues(color) { if (color.cyan <= 3) color.cyan = 0; if (color.magenta <= 3) color.magenta = 0; if (color.yellow <= 3) color.yellow = 0; if (color.black <= 3) color.black = 0; } // Loop through all swatches for (var i = 0; i < swatches.length; i++) { // Update progress bar progressWindow.bar.value = i + 1; progressWindow.update(); var swatch = swatches[i]; var targetColor = null; // Check for standard CMYK swatches if (swatch.color.colorModel === ColorModel.CMYK) { targetColor = swatch.color; } // Check for global CMYK process swatches (SpotColor with Process type) else if (swatch.color.typename === "SpotColor" && swatch.color.colorType === ColorType.PROCESS) { if (swatch.color.processColor.colorModel === ColorModel.CMYK) { targetColor = swatch.color.processColor; } } // Process the color if it's a CMYK type if (targetColor) { roundColorValues(targetColor); setColorValues(targetColor); } } // Clean up and notify progressWindow.close(); alert("Complete!");
关键修改点
- 新增全局印刷色的判断逻辑:通过
swatch.color.typename === "SpotColor"和swatch.color.colorType === ColorType.PROCESS识别全局色,再检查processColor.colorModel是否为CMYK。 - 修改
setColorValues函数的条件判断为独立if,确保所有低数值通道都能被置0。 - 使用Illustrator内置的
ColorModel和ColorType枚举,无需自定义。
内容的提问来源于stack exchange,提问作者ClintonPM
相关产品推荐
相关产品推荐

