illustrator插件--常用功能开发--随机填色--js脚本开发--AI插件

  illustrator是矢量编辑软件,画板是绘制处理的重要容器,在印刷方面的一个重要功能就是颜色处理,开发一个随机填充颜色功能,以下功能仅用于学习交流,请勿用于非法用途和商业用途,源代码如下所示:

len = app.activeDocument.selection.length;
gradAlert = 0;
strokes = false;
if (len == 0) {
    alert("Please select one or more objects. Grouped objects will not be affected.");
} else {
    ob = app.activeDocument.selection;
    var line1 = "1. 要随机填充色,输入1-100的数值(数值越大,明度越小) ";
    var line2 = "2.要随机轮廓色,输入1-100的数值并以英文“S”结尾(数值越大,明度越大) ";
    var line3 = "3.要随机不透明度,输入1-100的最小值和最大值,如30,100 ";
    var data = prompt(line1 + line2 + line3, "");
    if (data == null || data.length == 0) {

    } else {
        if (data.indexOf(",") > 0) {
            action = data.split(",");
        } else {
            action = data.split(" ");
        }
        if (action[action.length - 1] == "s") {
            action.length = action.length - 1;
            strokes = true;
        }
        if (action[action.length - 1].charAt(action[action.length - 1].length - 1) == "s") {
            action[action.length - 1] = action[action.length - 1].substr(0, action[action.length - 1].length - 1);
            strokes = true;
        }
        var baseVal = action[0];
        for (var j = 0; j < len; j += 1) {
            if (isNumber(action[0]) && isNumber(action[1]) && action.length == 2) {
                calcNum = (action[0] * 1) + (Math.random(1) * (action[1] - action[0]));
                ob[j].opacity = setLegal(calcNum);
            } else if (isNumber(action[0], 255) && action.length == 1) {
                if (app.activeDocument.documentColorSpace == "DocumentColorSpace.RGB") {
                    action[0] = Math.round(baseVal * 2.55);
                }
                if (strokes == false) {
                    if (ob[j].filled == true) {
                        setColor(ob[j].fillColor, setLegal(action[0], 255));
                    }
                } else {
                    if (ob[j].stroked == true) {
                        setColor(ob[j].strokeColor, setLegal(action[0], 255));
                    }
                }
            } else {
                j = len;
                error(action[0]);
            }
        }
    }
}

function error(val) {
    alert(val + " 数值与脚本不符,应控制在1-100之间!");
}

function isNumber(num, max) {
    if (max == undefined) {
        max = 100;
    }
    if (num >= 0 && num <= max) {
        return true;
    }
    return false;
}

function setLegal(num, max) {
    if (max == undefined) {
        max = 100;
    }
    if (num < 0) {
        return -num % max;
    } else if (num > max) {
        return max - (num % max);
    } else {
        return num;
    }
}

function setColor(obj, dev) {
    var d = Math.round(Math.random(1) * dev);
    if (obj == undefined) {

    } else if (obj.typename == "RGBColor") {
        d = Math.round(Math.random(1) * dev);
        var r = obj.red;
        obj.red = setLegal((r - dev) + (d * 2), 255);
        d = Math.round(Math.random(1) * dev);
        var g = obj.green;
        obj.green = setLegal((g - dev) + (d * 2), 255);
        d = Math.round(Math.random(1) * dev);
        var b = obj.blue;
        obj.blue = setLegal((b - dev) + (d * 2), 255);
    } else if (obj.typename == "SpotColor") {
        var d = Math.round(Math.random(1) * dev);
        var t = obj.tint;
        obj.tint = setLegal((t - dev) + (d * 2), 100);
    } else if (obj.typename == "GrayColor") {
        var d = Math.round(Math.random(1) * dev);
        var t = obj.gray;
        obj.gray = setLegal((t - dev) + (d * 2), 100);
    } else if (obj.typename == "CMYKColor") {
        d = Math.round(Math.random(1) * dev);
        var c = obj.cyan;
        obj.cyan = setLegal((c - dev) + (d * 2), 100);
        d = Math.round(Math.random(1) * dev);
        var m = obj.magenta;
        obj.magenta = setLegal((m - dev) + (d * 2), 100);
        d = Math.round(Math.random(1) * dev);
        var y = obj.yellow;
        obj.yellow = setLegal((y - dev) + (d * 2), 100);
        d = Math.round(Math.random(1) * dev);
        var k = obj.black;
        obj.black = setLegal((k - dev) + (d * 2), 100);
    } else {
        if (gradAlert != 1 && obj.typename == "PatternColor" || obj.typename == "GradientColor") {
            gradAlert = 1;
            alert("Patterns and Gradients will not be transformed.");
        }
    }
}

  合理的脚本代码可以有效的提高工作效率,减少重复劳动。

文章引用至 作者知了-联系方式1

文章引用至 作者知了-联系方式2

你可能感兴趣的:(illustrator插件--常用功能开发--随机填色--js脚本开发--AI插件)