
////////////////////////////////////////////////////////////////////////////////
// Note from the Scriptographer.org Team
//
// In Scriptographer 2.9, we switched to a top-down coordinate system and
// degrees for angle units as an easier alternative to radians.
// 
// For backward compatibility we offer the possibility to still use the old
// bottom-up coordinate system and radians for angle units, by setting the two
// values bellow. Read more about this transition on our website:
// http://scriptographer.org/news/version-2.9.064-arrived/

script.coordinateSystem = 'bottom-up';
script.angleUnits = 'radians';

/*

				---============================================---
                 Mosaic Staple Raster v1.05 by Håkan Lundgren
				---============================================---

				
				My second script ever. Maybe not TOO useful but it
				generates quite interessting results.
				Just place an image on the artboard. Input your
				desired values and press render.



				Thank you so much for your help, Jonathan Puckey!
				
				
				...and thanks Jürg Lehni for Scriptographer!!!
				

				---============================================---
                 Mosaic Staple Raster v1.05 by Håkan Lundgren
				  changed with an extra option by El Tibur0n
				  				v 1.05.01
				---============================================---
				
				
				Thank you so much for the script Håkan Lundgren
				I hope you are not afraid about my changes.
				
				
*/
var raster = null;
var values = {
	scanInput: 10,
	heightVal: 40,
	widthVal: 30,
	render: 'Render'
};

var items = {
	scanInput: { type: 'number', label: 'Square Value', },
	heightVal: { type: 'number', label: 'Staple Height', units: 'point' },
	widthVal: { type: 'number', label: 'Staple Width', units: 'point' },
	variationVal: { type: 'number', label: 'Variation Value', },
	render: { type: 'button', value: 'Render',
		onClick: function() {
			initRaster(values.variationVal, values.scanInput, values.heightVal, values.widthVal);}
	}
};
var palette = new Palette('Mosaic Staple Raster v1.05.01', items, values);


function initRaster(variationVal, scanInput, heightVal, widthVal) {
	var rasters = document.getItems([Raster, PlacedFile],{ selected: true });

	if(!rasters.length) {
		Dialog.alert('Select an image first.');
		return false;
	}
	
	raster = rasters.first;
	if(raster instanceof PlacedFile && !raster.eps) {
		raster = raster.embed(false);
	}

	var rasterGroup = new Group();
	rasterGroup.name = 'Raster';


	var rectSize = new Size(scanInput, scanInput);
	var picXsize = raster.bounds.width; 
	var picYsize = raster.bounds.height;
	var columns = Math.round(picXsize / scanInput); 
	var rows = Math.round(picYsize / scanInput); 
	var offset = raster.bounds.bottomLeft; 


	for (var iy = 0; iy < rows; iy++) {

		var row = new Group();
		row.name = 'Row';

		for (var ix = 0; ix < columns; ix++) {

	        var bottomLeft = offset + new Point(ix, iy) * rectSize; 
			var rectangle = new Rectangle(bottomLeft, rectSize); 
			var color = raster.getAverageColor(rectangle); 
			
			var hVal = 0;
			if(variationVal <= 0) {
				hVal = color.gray * heightVal;
			} else {
				hVal = (variationVal - color.gray) * heightVal;
			};

			var staple = new Group;
			staple.name = 'Staple';
			
			var sx = -((iy * widthVal / 2) - (ix * widthVal / 2));
			var sy = ((ix * widthVal / 4) + (iy * widthVal / 4));
						
			var blockPaths = new Path();
			blockPaths.strokeWidth = widthVal / 100;
			blockPaths.strokeJoin = 'round';
			blockPaths.strokeColor = '#000000';
			blockPaths.fillColor = new RGBColor(color);
			blockPaths.add(new Point(sx, sy+hVal));
			blockPaths.add(new Point(sx+(widthVal/2), sy+hVal+(widthVal/4)));
			blockPaths.add(new Point(sx+widthVal, sy+hVal));
			blockPaths.add(new Point(sx+(widthVal/2), sy+hVal-(widthVal/4)));
			blockPaths.closed = true;
			staple.appendChild(blockPaths);

			if (hVal > 0) {
				var blockPaths = new Path();
				blockPaths.strokeWidth = widthVal / 100;
				blockPaths.strokeJoin = 'round';
				blockPaths.strokeColor = '#000000';
				blockPaths.fillColor = new RGBColor(color.red * 0.5, color.green * 0.5, color.blue * 0.5);
				blockPaths.add(new Point(sx, sy));
				blockPaths.add(new Point(sx, sy+hVal));
				blockPaths.add(new Point(sx+(widthVal/2), sy+hVal-(widthVal/4)));
				blockPaths.add(new Point(sx+(widthVal/2), sy-(widthVal/4)));
				blockPaths.closed = true;
				staple.appendChild(blockPaths);	
			
				var blockPaths = new Path();
				blockPaths.strokeWidth = widthVal / 100;
				blockPaths.strokeJoin = 'round';
				blockPaths.strokeColor = '#000000';
				blockPaths.fillColor = new RGBColor(color.red * 0.75, color.green * 0.75, color.blue * 0.75);
				blockPaths.add(new Point(sx+(widthVal/2), sy-(widthVal/4)));
				blockPaths.add(new Point(sx+(widthVal/2), sy+hVal-(widthVal/4)));
				blockPaths.add(new Point(sx+widthVal, sy+hVal));
				blockPaths.add(new Point(sx+widthVal, sy));
				blockPaths.closed = true;
				staple.appendChild(blockPaths);
			}
		
			row.appendBottom(staple);
			rasterGroup.appendTop(row);			
		}

		rasterGroup.appendBottom(row);

	}

}