我正在尝试进行编码以找到一定范围的颜色并进行选择。 Someone was able to help with this code.
with (app.activeDocument) {
if (pathItems.length > 0) {
alert(pathItems.length);
for (var g = 0 ; g < pathItems.length; g++) {
if (pathItems[g].filled == true) {
if (
pathItems[g].fillColor.red > 200 == true &&
pathItems[g].fillColor.red < 210 == true &&
pathItems[g].fillColor.green > 200 == true &&
pathItems[g].fillColor.green < 210 == true &&
pathItems[g].fillColor.blue > 200 == true &&
pathItems[g].fillColor.blue < 210 == true
)
{
alert('R' + pathItems[g].fillColor.red + ' G' + pathItems[g].fillColor.green + ' B' + pathItems[g].fillColor.blue);
}
}
}
}
}
我一直试图让它使用警报中找到的数字并将其用作RGB颜色进行选择
with (app.activeDocument) {
if (pathItems.length > 0) {
alert(pathItems.length);
for (var i = 0 ; i < pathItems.length; i++) {
if (pathItems[i].filled == true) {
if (
pathItems[i].fillColor.red > 200 == true &&
pathItems[i].fillColor.red < 210 == true &&
pathItems[i].fillColor.green > 200 == true &&
pathItems[i].fillColor.green < 210 == true &&
pathItems[i].fillColor.blue > 200 == true &&
pathItems[i].fillColor.blue < 210 == true
);
var newRGBColor = pathItems[i].fillColor.red &&
pathItems[i].fillColor.green &&
pathItems[i].fillColor.blue;
{
app.activeDocument.defaultFillColor = newRGBColor;
app.executeMenuCommand("Find Fill Color menu item");
}
}
}
}
}
但这不起作用。我不是一个编码人员,我的大脑只能理解它。我知道基本知识,但那几乎就是我的大脑能保留的一切。这是我的第100个版本的代码。我确实在尝试,所以如果我缺少任何东西,请不要生我的气。 我参加了一个JavaScript课,阅读了Illustrator脚本公会,遍及W3schools。我的大脑只是不太了解编码,所以请不要生我的气。我真的只是在寻求帮助。
更新
谢谢您让我知道with
已过时,但是,我从其他人那里得到了此编码,如上图所示,我不知道该如何更新。我正在尝试做的是在Illustrator中找到对象的RGB颜色分解。 app.activeDocument.defaultFillColor
将更改我的设置,即默认颜色填充,而app.executeMenuCommand("Find Fill Color menu item")
将对我选择的颜色进行优化(如果未选择,则为默认颜色)
if (
pathItems[g].fillColor.red > 200 == true &&
pathItems[g].fillColor.red < 210 == true &&
pathItems[g].fillColor.green > 200 == true &&
pathItems[g].fillColor.green < 210 == true &&
pathItems[g].fillColor.blue > 200 == true &&
pathItems[g].fillColor.blue < 210 == true
)
这将找到我需要选择的颜色,但是我可以将defaultFillColor用作之前代码的颜色
答案 0 :(得分:1)
我觉得我们这里有一个XY problem,因为这将有助于您了解最终目标。
由于您提到使用“查找填充颜色”菜单项,因此我假设您希望脚本选择具有给定范围内的颜色(R,G和B在200至210之间)的所有对象。
修改Mouser的脚本将为我们提供:
var appActiveDocumentPathItems = app.activeDocument.pathItems; //create var for this object;
var selectionArray = []; //create array for objects to be selected
if (appActiveDocumentPathItems.length > 0) {
for (var i = 0; i < appActiveDocumentPathItems.length; i++) {
if (appActiveDocumentPathItems[i].filled == true) {
var fill = appActiveDocumentPathItems[i].fillColor;
if (
fill.red > 200 == true &&
fill.red < 210 == true &&
fill.green > 200 == true &&
fill.green < 210 == true &&
fill.blue > 200 == true &&
fill.blue < 210 == true
) {
selectionArray [selectionArray.length] = appActiveDocumentPathItems[i]; //if an object matches the requirement, add it to the array
}
}
}
}
activeDocument.selection = selectionArray; //select the objects in the array
答案 1 :(得分:0)
由于运算符&&
,这很容易导致不必要的值。您想将不同的值组合成一个代表RGB颜色的字符串:
看看:
var pathItems = [{fillColor : {red : 200, green : 240, blue : 156}}]; //array - filled with colors
var newRGBColor = pathItems[0].fillColor.red && pathItems[0].fillColor.green && pathItems[0].fillColor.blue;
console.log(newRGBColor); //not the result expected : 156
要使其正常工作,必须定义对象red
的{{1}},green
和blue
属性
RGBColor
最终解决方案(无var pathItems = [{
fillColor: {
red: 205,
green: 206,
blue: 209
}
}]; //array - filled with colors
var newRGBColor = new RGBColor() //initial default colour
newRGBColor.red = pathItems[0].fillColor.red;
newRGBColor.green = pathItems[0].fillColor.green;
newRGBColor.blue = pathItems[0].fillColor.blue;
console.log(newRGBColor);
//dummy function to make demo work, not part of the solution
function RGBColor()
{
this.red;
this.green;
this.blue;
}
):
with
请记住,这将循环所有已填充颜色的形状,并且循环的最后一次迭代(最后一次填充的形状)将为
var appActiveDocumentPathItems = app.activeDocument.pathItems; //create var for this object; if (appActiveDocumentPathItems.length > 0) { for (var i = 0; i < appActiveDocumentPathItems.length; i++) { if (appActiveDocumentPathItems[i].filled == true) { var fill = appActiveDocumentPathItems[i].fillColor; if ( fill.red > 200 == true && fill.red < 210 == true && fill.green > 200 == true && fill.green < 210 == true && fill.blue > 200 == true && fill.blue < 210 == true ) { var newRGBColor = new RGBColor(); //this is how you define a new color in Illustrator's extendscript newRGBColor.red = pathItems[i].fillColor.red; newRGBColor.green = pathItems[i].fillColor.green; newRGBColor.blue = pathItems[i].fillColor.blue; app.activeDocument.defaultFillColor = newRGBColor; app.executeMenuCommand("Find Fill Color menu item"); } } } }
。您需要更多的逻辑来选择不同于上一种的颜色。