Jump to content

User:Vanisaac/scripts/TartanBuilder.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Vanisaac (talk | contribs) at 01:54, 26 June 2020. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/* The TartanBuilder userscript is used in conjunction with template:Tartan to build an SVG
   image of a tartan pattern from its sett description and upload the file to Wikipedia for
   use as a default image for template:Tartan to use */

function TartanBuild(){
var settinput = prompt("Sett pattern");	// input of a tartan sett pattern, to be replaced
										// by data from HTML scrape.
var sett = settinput.toLowerCase();
var colorarray = [];					// Array containing color codes for thread count.
var lenarray = [];						// Array of color code locations in sett string.
var threadarray = [];					// Array containing thread count for each color.
var othercount = 0;						// Variable for keeping track of 
var dasharray;							// String for insertion in SVG stroke-dasharrays.

sett = sett.replace(" ", "");

if (sett !== null){
	for (i=0; i<sett.length; i++){		// Goes through each character of sett to extract
										// color codes and their location in the string.
		if (sett.substr(i,1) >= "a" && sett.substr(i,1) <= "z" ){
			colorarray.push (sett.substr(i,1));
			lenarray.push (i);
		}
	}
	for (i=0; i<colorarray.length; i++){	// Extracts thread counts from sett string
		threadarray.push (sett.substring(lenarray[i], lenarray[i+1]-1));
	}
	for (i="a"; i<="z"; i++){			//Generates dasharray for each color
	dasharray = [];
		for (j=0; j<=colorarray.length; j++){		// Go through colorarray.
			if (colorarray[j] == i) {				// If colorarray value is current color,
				if (j!==0) {						// push count of other other thread,
					dasharray.push (othercount);	// unless this is the start of the sett.
					othercount = 0;					// Reset the count of other threads
				}
				dasharray.push (threadarray[j]);	// Then push the thread count for this
			}else{									// color. If it's not the right color,
				othercount += threadarray[j];		// add it to the count of other threads.
			}
		}
	dasharray.push (othercount);		// Push the final threadcount to finish.
	}
	if (dasharray.length !== 0){
		console.log("#", i, " {stroke:", colorcodes [i], "; stroke-dasharray:",
		dasharray.toString(), ";}\n");
	}
}
}