Sg2.9
random colors within a range
Recent RSS
random colors within a range
From:  jirka
Date:  14. March 2007, 10:17

hello, I am completely lost. I need a script which applies random colors to selected objects but within a defined range of colors. e.g. red, green and blue. I tried hard with no result :) I think it's probably really simple to do that, but I am a programming beginner so for me it is not, especially when the reference shows no examples... if anyone could help me out and kick me right way, I'd be very very grateful.

Thanks, Jirka

Re: random colors within a range
From:  ben
Date:  14. March 2007, 12:32

I 'm going to do it in few hours ^^
It's easy just create Array for setup colors and choose random in Array.

Re: random colors within a range
From:  jirka
Date:  14. March 2007, 12:35

hi ben, that would be great!! I did an array, but don't have any idea how to continue..

Re: random colors within a range
From:  ben
Date:  14. March 2007, 12:48

here it is : colorz are in rgb 1 is for 255 ... 0.5 for 127 // black is 1,1,1

var sel = activeDocument.getSelectedItems();
var colorz = new Array();
colorz[0]=new RGBColor(0.5,0.5,0.5);
colorz[1]=new RGBColor(1,0,0);
colorz[2]=new RGBColor(0,1,0);
colorz[3]=new RGBColor(0,0,1);
for (var i = 0; i < sel.length; i++) {
art = sel[i];
if (art instanceof Path) {
var color = colorz[Math.round(Math.random() * (colorz.length-1))];
if (art.style.stroke.color != null) art.style.stroke.color = color;
if (art.style.fill.color != null) art.style.fill.color = color;
}
}

Re: random colors within a range
From:  jirka
Date:  14. March 2007, 12:54

thank you about million times!! I owe you a beer :) - thanks!

Re: random colors within a range
Date:  16. March 2007, 00:28

I ran the script and it works well. I have a request/idea to expand it, though. (I understand the logic, but don't know the coding language—anyone want to help?)

Can a menu be called that gives you 6 input fields: high and low R,G, and B value (range: 0-1, or maybe let the user input the exact 0-255 RGB range then divide by 255?) and a value for array element number (2-max)? You could randomize the range of the RBG colors and use that to populate a user defined number of array elements. Sounds easy enough, right?

Alternatively, you could do a center point value and a variance value (in % maybe?) to define the random range of color. Values (in percentage) for a CMYK set would be nice too.

Re: random colors within a range
From:  jirka
Date:  16. March 2007, 10:53

yeah, it was the first idea I had (although I didn't expect someone will write it, I am ok with this simple script written by Ben). But maybe there's one problem with it: the number of colors cannot be controlled in any way. I think the better idea is to let the script choose colors randomly from the Swatches pallete, then you can control appearance and number. Also gradients or patterns if I want to. I'm gonna try to write it, at least I have a goal to start learning JavaScript...

Re: random colors within a range
Date:  17. March 2007, 13:11

Yeah, good idea to use the swatches palette for that.
I'm not sure if Gradients and Patterns will work yet though..
If you need any help, feel free to post your questions here and we'll help you along with your code.

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
RandomTools 21.08.08