简体   繁体   English

使用extendscript更改对象的颜色

[英]Change the color of objects with extendscript

I've had a look around and can't find anything to do this, so I think I'm probably asking the wrong question. 我环顾四周,找不到任何可以执行的操作,所以我想我可能是在问错问题。

I have a series of objects on a page in illustrator with a specific colour in RGB, say its 255 red. 我在Illustrator的页面上有一系列对象,这些对象具有RGB的特定颜色,例如255红色。 I want to select items with this color or loop through to see if an item is this color and change it to a CMYK colour, say 75% grey 我想选择具有这种颜色的项目,或循环浏览以查看项目是否为该颜色并将其更改为CMYK颜色,例如75%灰色

It done this but it always selects the item to change it 它做到了,但总是选择要更改的项目

var currPageItem=app.activeDocument.activeLayer;
var myColour = new RGBColor("255,0,0")//initial default colour
var myGrey= new CMYKColor("0,0,0,75")//initial default grey


// Stepping through each item on the layer.
for (var i = 0; i < currPageItem.pageItems.length; i++) {
    var currentItem = currPageItem.pageItems[i];
    //$.writeln("Object name=", currentItem);
    if (currentItem.RGBColor=myColour) {
           $.writeln("Colour function",i);
           };

}

I'd like to be able to change stroke colours also. 我也希望能够更改笔触颜色。 Any help really appreciated, very stuck on this 任何帮助都非常感激,对此非常卡住

Thanks for putting me in the correct direction Josh. 感谢您将我引向正确的方向。 I think I've now got it. 我想我已经明白了。 Firstly the document color mode under the file menu has to be set to RGB. 首先,必须将文件菜单下的文档颜色模式设置为RGB。 If this isn't done then as it script reads through the page items it checks them as CMYK and so doesn't recogniise any RGB values. 如果未完成此操作,则脚本将读取页面项目,并将其检查为CMYK,因此无法识别任何RGB值。

Also it check the values to a gazillion decimal places so these need rounding. 此外,它还会将值检查到万亿位小数位,因此需要四舍五入。 Have made the following adjustments and this seems to work. 进行了以下调整,这似乎可行。 Any other improvement most welcome 任何其他改进都值得欢迎

var layer = app.activeDocument.activeLayer;
var testColor = new RGBColor()//initial default colour
testColor.red = 180;
testColor.green = 93;
testColor.blue = 120;
var myGrey= new CMYKColor()//initial default grey
myGrey.black=75;


// Stepping through each item on the layer.
for (var i = 0; i < layer.pathItems.length; i++) {
    var item = layer.pathItems[i];
    $.writeln("Test colour ",Math.round( item.fillColor.red))
    if (Math.round(item.fillColor.red) == testColor.red &&
    Math.round(item.fillColor.green)== testColor.green &&
    Math.round(item.fillColor.blue) == testColor.blue)
   {
      $.writeln("Color function",i );
      item.fillColor = myGrey;
   }

}

Using something similar for pre-press work. 在印前工作中使用类似的方法。 I adapted the RGB version above convert all rich black to K only. 我改写了上面的RGB版本,将所有丰富的黑色仅转换为K。 Still experimenting with color values and will add a way to also check the stroke color. 仍在尝试使用颜色值,这将增加一种检查笔触颜色的方法。 Thank you! 谢谢! Was searching for a solution for a long time. 一直在寻找解决方案。

 var layer = app.activeDocument.activeLayer; var testColor = new CMYKColor(); //initial default colour testColor.cyan = 62; testColor.magenta = 62; testColor.yellow = 62; testColor.black = 62; var myGrey = new CMYKColor(); //initial default grey myGrey.black = 100; // Stepping through each item on the layer. for (var i = 0; i < layer.pathItems.length; i++) { var item = layer.pathItems[i]; $.writeln("Test colour ", Math.round(item.fillColor.cyan)); if (Math.round(item.fillColor.cyan) > testColor.red && Math.round(item.fillColor.magenta) > testColor.magenta && Math.round(item.fillColor.yellow) > testColor.yellow && Math.round(item.fillColor.black) > testColor.black) { $.writeln("Color function", i); item.fillColor = myGrey; item.selected = true; } } 

This should give you a good start with what you're trying to accomplish. 这应该为您要完成的工作提供一个良好的开端。 *There is probably a better way to code this, but the illustrator documentation doesn't seem to be too helpful in figuring out how to instantiate or compare colors. *可能有更好的编码方式,但是illustrator文档似乎在弄清楚如何实例化或比较颜色方面似乎并没有太大帮助。

var layer = app.activeDocument.activeLayer;

var testColor = new RGBColor(); // initial default colour
testColor.red = 255; // rest of the values default to 0

var greyColor = new CMYKColor(); // initial default grey
greyColor.black = 75; // rest of the values default to 0

for (var i=0; i<layer.pathItems.length; i++) {
   var item = layer.pathItems[i];

   if (item.fillColor.red === testColor.red &&
       item.fillColor.blue === testColor.blue &&
       item.fillColor.green === testColor.green)
   {
      item.fillColor = greyColor;
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM