用颜色填充对象的脚本 - 均匀分布但没有可辨别的图案

Script to fill objects with colours - uniform distribution but in no discernible pattern

我正在 javascript 中为 Adob​​e Illustrator 编写脚本,以使用选定的颜色填充对象。

我有 32 个对象和 12 个色样。我想用每种颜色填充 32 个对象两次,然后用随机选择填充剩余的 8 个对象。我想以不可识别的模式填充对象,所以只是遍历每个对象并为其分配下一个样本颜色是行不通的。

这是我目前所拥有的,但是它并没有使用每种颜色至少两次并随机填充。

myObjects = app.activeDocument.selection;
myDocument = app.activeDocument;

if (myObjects instanceof Array) {
    colourSwatches = myDocument.swatches.getSelected();

    //if there are swatches
    if (colourSwatches.length != 0) {
        for (i = 0; i < myObjects.length; i++) {

            //if the selection is a path or compound path
            if (myObjects[i].typename == "PathItem" || myObjects[i].typename == "CompoundPathItem") {

                selItem = myObjects[i];
                selItem.filled = true;

                //select colour from swatches at random and then fill 
                swatchIndex = Math.round( Math.random() * (colourSwatches.length - 1 ));

                if (selItem.typename == "PathItem") {
                    selItem.fillColor = colourSwatches[swatchIndex].color;
                } else {
                    selItem.pathItems[0].fillColor = colourSwatches[swatchIndex].color;
                }
            }
        }
    }
}

与其随机挑选样本(那是你失去控制的地方),不如这样做:

  1. 构建一个 目标 大小 (32) 的数组,并用所需的样本 (12) 填充它。您不必随机化此数组。
  2. 对于每个对象,随机选择一个条目。
  3. 从数组中删除选取的条目。
  4. 循环直到满意。

也就是说,未经测试,但像这样:

var targetArray = [];
// fill with the set of required colors, times 2
for (i=0; i < 12; i++)
{
  targetArray[2*i] = i;
  targetArray[2*i+1] = i;
}
// fill up to contain 32 items
for (i=24; i<32; i++)
  targetArray[i] = Math.floor(Math.random() * colourSwatches.length);

(有关 random 表达式,请参阅 Getting random value from an array。)

现在您有一个包含 indexes 的数组供样本使用。遍历你的对象并从数组中随机选择项目,直到你完成(这很好)或者你 运行 没有要选择的项目(这表明你要填充的项目数量不正确):

for (i = 0; i < myObjects.length; i++)
{
    arrayIndex = Math.floor(Math.random() * targetArray.length);
    swatchIndex = targetArray[arrayIndex];
    targetArray.splice (arrayIndex,1);
    selItem.fillColor = colourSwatches[swatchIndex].color;
}

32 个对象:

12 种颜色:

var document     = app.activeDocument;
var objects      = document.selection; // 32 objects
var swatchColors = document.swatches.getSelected(); // 12 colors
var colors       = swatchColors; // 12 colors
colors           = colors.concat(swatchColors); // duplicate the 12 colors, 12*2 colors

// add 8 random colors
for (var i = 0; i < 8; i++) {
    // create random index in [0, 11]
    var randomIndex = Math.floor(Math.random() * swatchColors.length);
    // add the random color to colors array
    colors.push(swatchColors[randomIndex]);
    // remove the added color from the swatches so no extra duplicates
    swatchColors.splice(randomIndex, 1);
}

// now we have 32 objects and 32 colors
// in colors array, there are (12 * 2) + 8 random colors
for (var i = 0; i < objects.length; i++) {
    objects[i].fillColor = colors[i].color;
}

执行后:

对象从上到下、从左到右迭代。这是 document.selection 命令。因此,前 24 种颜色的应用顺序与它们在样本中的顺序相同。如果你想要随机顺序,你可以随机播放colors