ccvip200 发表于 2022-8-22 20:14:11

AI随机填充色、轮廓色、不透明度脚本

//Randomize Colors.
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的数值(数值越大,明度越小)\n";
var line2 = "2) 要随机轮廓色,输入1-100的数值并以英文“S”结尾(数值越大,明度越大)\n";
var line3 = "3) 要随机不透明度,输入1-100的最小值和最大值,如30,100\n";

var data = prompt(line1+line2+line3, "");
if ( data == null || data.length ==0 )
{
//User canceled process or didn't enter anything, so do nothing and end nicely.
} else{
if (data.indexOf(",")>0) {
   action = data.split(",");
} else {
   action = data.split(" ");
}
//if the 's' is it's own value in the array, detect and snip
if (action == "s") {
   action.length = action.length-1;
   strokes = true;
}
// or, detect and snip the 's' from the last number in the array
if (action.charAt(action.length-1) == "s") {
   action = action.substr(0,action.length-1)
   //alert(action);
   strokes = true;
}
var baseVal = action;
for (j=0; j<len; j++) {
   //
   //change OPACITY here
   if (isNumber(action) && isNumber(action) && action.length == 2) {
    calcNum = ((action*1)+((Math.random(1)*(action-action))));
    ob.opacity = setLegal(calcNum);
    //
    //Change COLOR here:
   } else if (isNumber(action, 255) && action.length == 1) {
    if(app.activeDocument.documentColorSpace == "DocumentColorSpace.RGB"){

    action = Math.round(baseVal*2.55);

    }
    if (strokes == false) {
   if (ob.filled == true) {
      setColor(ob.fillColor, setLegal(action, 255));
   }
    } else {
   if (ob.stroked == true) {
      setColor(ob.strokeColor, setLegal(action,255));
   }
    }
    //
   } else {
    //break
    j = len;
    error(action);
   }
}
}}
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) {
   //do nothing
} else if (obj.typename == "RGBColor") {
   //alert("RGB");
   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") {
   //alert("Spot");
   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") {
   //alert("Gray");
   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") {
   //alert("CMYK");
   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.");
} else {
   //alert("Nuthin...");
}
}


-------------------------------
AI随机填色脚本,复制以上代码到文本,另存为JS格式文件,在AI中脚本调用即可。这个挺老了不知道论坛里有没有

页: [1]
查看完整版本: AI随机填充色、轮廓色、不透明度脚本