Sg2.9
User defined color distribution
Recent RSS
User defined color distribution
From:  Ryan Kee
Date:  20. January 2007, 20:49

Hey everyone, I'm trying to write a script that gathers all selected objects and applies user defined CMYK values at random to the objects. It's based of the random color tutorial that Simon did, only the user should be able to set all color values before it applies those values at random. I'm new to javascript, so it might just be that I'm an idiot and this isn't anywhere close to being a working script. Thanks.

//initiate values for user input
function onInit(){
c1 = 10;
c2 = 10;
c3 = 10;
m1 = 10;
m2 = 10;
m3 = 10;
y1 = 10;
y2 = 10;
y3 = 10;
k1 = 10;
k2 = 10;
k3 = 10;
}

//allow user to input color
function onOptions(){
cmyks = Dialog.prompt("cmyk randomizer:",[
{value: c1, description: "Cyan 1"},
{value: c2, description: "Cyan 2"},
{value: c3, description: "Cyan 3"},
{value: m1, description: "Magenta 1"},
{value: m2, description: "Magenta 2"},
{value: m3, description: "Magenta 3"},
{value: y1, description: "Yellow 1"},
{value: y2, description: "Yellow 2"},
{value: y3, description: "Yellow 3"},
{value: k1, description: "Black 1"},
{value: k2, description: "Black 2"},
{value: k3, description: "Black 3"}
]);
if (values != null) {
c1 = cmyks{0};
c2 = cmyks{1};
c3 = cmyks{2};
m1 = cmyks{3};
m2 = cmyks{4};
m3 = cmyks{5};
y1 = cmyks{6};
y2 = cmyks{7};
y3 = cmyks{8};
k1 = cmyks{9};
k2 = cmyks{10};
k3 = cmyks{11};
}
}

//set input values to cmyk colors
var cmyk = new Array();
cmyk[0] = new CMYKColor((c1)/255,(m1)/255,(y1)/255,(k1)/255);
cmyk[1] = new CMYKColor((c2)/255,(m2)/255,(y2)/255,(k2)/255);
cmyk[2] = new CMYKColor((c3)/255,(m3)/255,(y3)/255,(k3)/255);

//randomization for color array selection
var raw_random_number = Math.random();
var random_number = Math.round(raw_random_number * (cmyk.length));

if (random_number == cmyk.length){random_number = 0};

//set color variable
var color = cmyk[random_number];

//apply color
var sel = activeDocument.getSelectedItems();
for (var i = 0; i < sel.length; i++) {
art = sel[i];
if (art instanceof Path) {
if (art.style.fill.color != null) art.style.fill.color = color;
}
}

Re: User defined color distribution
From:  Ryan Kee
Date:  21. January 2007, 21:24

Thanks Simon. Your script works great for placing random CMYK values and thanks for pointing out that "values" in onOptions. Your method for randomizing the array number is much better than what I had.

What I'm trying to do now is rather than inserting random values into the CMYK array, inserting user defined values from the options dialogue. I'm not sure what exactly I need to do to get the script working. Do I need to place the assignment of the 'cmyk' array and the application of color into some sort of function or should it work as is?

Heres the updated script.

//initiate values for user input
function onInit(){
c1 = 10;
c2 = 10;
c3 = 10;
m1 = 10;
m2 = 10;
m3 = 10;
y1 = 10;
y2 = 10;
y3 = 10;
k1 = 10;
k2 = 10;
k3 = 10;
}

//allow user to input color
function onOptions(){
cmyks = Dialog.prompt("cmyk randomizer:",[
{value: c1, description: "Cyan 1"},
{value: c2, description: "Cyan 2"},
{value: c3, description: "Cyan 3"},
{value: m1, description: "Magenta 1"},
{value: m2, description: "Magenta 2"},
{value: m3, description: "Magenta 3"},
{value: y1, description: "Yellow 1"},
{value: y2, description: "Yellow 2"},
{value: y3, description: "Yellow 3"},
{value: k1, description: "Black 1"},
{value: k2, description: "Black 2"},
{value: k3, description: "Black 3"}
]);
if (cmyks != null) {
c1 = cmyks{0};
c2 = cmyks{1};
c3 = cmyks{2};
m1 = cmyks{3};
m2 = cmyks{4};
m3 = cmyks{5};
y1 = cmyks{6};
y2 = cmyks{7};
y3 = cmyks{8};
k1 = cmyks{9};
k2 = cmyks{10};
k3 = cmyks{11};
}
}

//set input values to cmyk colors
var cmyk = new Array();
cmyk[0] = new CMYKColor((c1)/255,(m1)/255,(y1)/255,(k1)/255);
cmyk[1] = new CMYKColor((c2)/255,(m2)/255,(y2)/255,(k2)/255);
cmyk[2] = new CMYKColor((c3)/255,(m3)/255,(y3)/255,(k3)/255);

//apply color
var sel = activeDocument.getSelectedItems();
for (var i = 0; i < sel.length; i++) {
art = sel[i];
if (art instanceof Path) {
if (art.style.fill.color != null) {
var random_number = Math.round(Math.random() * (cmyk.length-1));
art.style.fill.color = cmyk[random_number];
}
}
}

Re: User defined color distribution
From:  Ryan Kee
Date:  22. January 2007, 16:08

Of course, I completely looked over that. Thanks again Simon. I still seem to be receiving this error when attempting to run the script. I'm not exactly sure how to read the error, although I do see that "c1 is not defined". I attempted adding 'var' before the variables in 'onInit', but that didn't get rid of the error. Anyone have any ideas?

/Applications/Adobe Illustrator CS2/Plug-ins.localized/Scriptographer/scripts/scripts/Random_Selected_Colors.js:51,0: ReferenceError: "c1" is not defined. (/Applications/Adobe Illustrator CS2/Plug-ins.localized/Scriptographer/scripts/scripts/Random_Selected_Colors.js#51)
java.lang.NullPointerException
at com.scriptographer.ai.Tool.setScript(Tool.java:68)
at com.scriptographer.gui.MainDialog$ToolButton.onClick(MainDialog.java:286)
at com.scriptographer.adm.Button.onNotify(Button.java:75)
at com.scriptographer.adm.NotificationHandler.onNotify(NotificationHandler.java:55)

Scripts
08.08.14, 15:24
15.05.14, 14:23
02.03.14, 19:16
18.11.13, 14:48
22.03.13, 03:05
22.02.13, 15:45
Posts
10.01.17, 16:37
19.02.16, 06:03
19.02.16, 06:00
17.01.16, 11:00
12.01.16, 13:10
25.11.15, 08:19
Script of the Moment
Parallel Arrangement 07.04.12