Illustrator JavaScript只能工作一次 - 不会切换

时间:2014-06-03 12:31:27

标签: javascript adobe-illustrator extendscript

我正在写一个改变棍子男人手臂和腿部颜色的脚本。一旦"他"被选中,它实际上只是镜像颜色:

//Declare and initialize variables
var msgType = "";
var app;
var docRef = app.activeDocument;
var testColor = docRef.swatches.getByName("CMYK Green");
var leftColor = docRef.swatches.getByName("LeftColor");
var rightColor = docRef.swatches.getByName("RightColor");

function sameColor(CMYKColor1, CMYKColor2) {
    "use strict";
    var isTheSameColor;
    if ((CMYKColor1.cyan === CMYKColor2.cyan) && (CMYKColor1.magenta === CMYKColor2.magenta) && (CMYKColor1.yellow === CMYKColor2.yellow) && (CMYKColor1.black === CMYKColor2.black)) {
        isTheSameColor = true;
    } else {
        isTheSameColor = false;
    }
    return isTheSameColor;
}


// check if a document is open in Illustrator.
if (app.documents.length > 0) {
    var mySelection = app.activeDocument.selection;
    var index;
    // Loop through all selected objects
    for (index = 0; index < mySelection.length; index += 1) {
        // Switch left and right colours
        if (sameColor(mySelection[index].strokeColor, leftColor.color)) {
            mySelection[index].strokeColor = rightColor.color;
        }
        if (sameColor(mySelection[index].strokeColor, rightColor.color)) {
            mySelection[index].strokeColor = leftColor.color;
        }
        if (sameColor(mySelection[index].fillColor, leftColor.color)) {
            mySelection[index].fillColor = rightColor.color;
        }
        if (sameColor(mySelection[index].fillColor, rightColor.color)) {
            mySelection[index].fillColor = leftColor.color;
        }

    }
}

它有效,但它只能工作一次(即我无法再次切换更改)。如果我撤消更改并再次尝试它再次起作用。这是为什么?

1 个答案:

答案 0 :(得分:2)

经过大量的讨论/调试后发现它正在改变CMYK值并不完全相同(只有很小一部分)。

enter image description here

更改了以下内容:

if ((CMYKColor1.cyan === CMYKColor2.cyan) ...

为:

if ((Math.round(CMYKColor1.cyan) === Math.round(CMYKColor2.cyan)) ... 

现在工作正常。