/* extention of Tutorial 2 that lets you choose a color range
 to randomly recolor art. Also, you can change transparency or use the document swatches.
 Pattern swathces unfortunatly cause an error. If anyone has any thoughts on how to fix that, let me know because I would love for them to work too.
 Great for large groups of shapes.*/

//color 1,
hue1 = 0;
sat1 = 100;
bright1 = 100;

//color 2
hue2 = 53;
sat2 =100;
bright2 = 100;
// transparency values
useopac = true;
opac1 = 100;
opac2 = 30;
// variable to use the documents swatches for random coloring
usedocswatch = false;
nofill = false;
// user has to enter rgb values in text fields, but i would love to use some kind of swatch picker
// or color sliders instead. Also would like to give the choice of color mode.
values = Dialog.prompt("set rando-color range", [
	{ value: hue1, description: "hue 1 (between 0 and 360)", width: 50 },
	{ value: sat1, description: "sat 1 (between 0 and 100)", width: 50 },
	{ value: bright1, description: "bright 1 (between 0 and 100)", width: 50 },
	{ value: hue2, description: "hue 2 (between 0 and 360)", width: 50 },
	{ value: sat2, description: "sat 2 (between 0 and 100)", width: 50 },
	{ value: bright2, description: "bright 2 (between 0 and 100)", width: 50 },
	{ value: useopac, description: "Change Transparency (between 0 and 100)", width: 50 },
	{ value: opac1, description: "Transparency 1", width: 50 },
	{ value: opac2, description: "Transparency 2", width: 50 },
	{ value: usedocswatch, description: "Use document swatches", width: 50 },
	//{ value: nofill, description: "Include the no fill swatch", width: 50 },
]);

if (values != null ) {
	hue1 = values[0];
	sat1 = values[1];
	bright1 = values[2];
	hue2 = values[3];
	sat2 = values[4];
	bright2 = values[5];
	useopac = values[6];
	opac1 = values[7];
	opac2 = values[8];
	usedocswatch = values[9];
	//nofill = values[10];
}


// determine which value is bigger to set the min and max values for r, g, b and the opacity
if (hue1 < hue2){
	minhue = hue1;
	maxhue = hue2;
} else{ 
	minhue = hue2;
	maxhue = hue1;
}
if (sat1 < sat2){
	minsat = sat1;
	maxsat = sat2;
} else{ 
	minsat = sat2;
	maxsat = sat1;
}
if (bright1 < bright2){
	minbright = bright1;
	maxbright = bright2;
} else{ 
	minbright = bright2;
	maxbright = bright1;
}
if (useopac == true){
	if (opac1 < opac2){
		minopac = opac1;
		maxopac = opac2;
	} else{ 
		minopac = opac2;
		maxopac = opac1;
	}
}

// a function to assign a random value between the min and max value
function assignRandom(minr, maxr){
	rand = minr + Math.random() * (maxr - minr);
	return rand;
}

function hsbToRgb(hsb){
	var br = Math.round(hsb[2] / 100 * 255);
	if (hsb[1] == 0){
		return [br, br, br];
	} else {
		var hue = hsb[0] % 360;
		var f = hue % 60;
		var p = Math.round((hsb[2] * (100 - hsb[1])) / 10000 * 255);
		var q = Math.round((hsb[2] * (6000 - hsb[1] * f)) / 600000 * 255);
		var t = Math.round((hsb[2] * (6000 - hsb[1] * (60 - f))) / 600000 * 255);
		switch (Math.floor(hue / 60)){
			case 0: return [br, t, p];
			case 1: return [q, br, p];
			case 2: return [p, br, t];
			case 3: return [p, q, br];
			case 4: return [t, p, br];
			case 5: return [br, p, q];
		}
	}
}

// get all the selected items and store them in the array sel
var sel = document.getItems({ type: Path, selected: true }); 
// loop through sel, assigning a new random color each time
for (var i = 0; i < sel.length; i++) {
	art = sel[i];
	if (usedocswatch == false){
/*		h = assignRandom(minhue, maxhue)/360;
		s = assignRandom(minsat, maxsat)/100;
		b = assignRandom(minbright, maxbright)/100;*/
		
		h = assignRandom(minhue, maxhue);
		s = assignRandom(minsat, maxsat);
		b = assignRandom(minbright, maxbright);
		rgb = hsbToRgb([h,s,b]);
		for (var j = 0; j < 3; j++) {
			rgb[j] = rgb[j] / 255;
		}
		
		var color = new RGBColor(rgb);
	} else {
		// get document swatches and assign them randomly to selected art
		var docsw = document.swatches; // docsw stores document swatches
		randswatch = docsw[Math.round(Math.random() * (docsw.length-1 - 2) + 2)]; //this picks a swatch, excluding the nofill and registration swatches.
		color = randswatch.color;
		print (color);
	}
	if (art.strokeColor != null) art.strokeColor = color; //set stroke if object has a stroke
	if (art.fillColor != null) art.fillColor = color; //set fill if object has a fill
	// if the user would like to change transparency, do it here
	if (useopac == true){
		art.opacity = assignRandom(minopac, maxopac)/100;
	}
}


