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:48, 27 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.
/**
 * TartanBuilder
 * 
 * This tool is used in conjunction with template:Infobox Tartan and
 * template:Tartan to build an SVG image of a tartan pattern from its sett
 * description and allow a user to upload the file to Wikipedia for use as a
 * default image for those templates.
 * 
 * Author: Van Anderson
 * Version: 0.1 (alpha), June 2020
 * License: CC-by-SA 4.0
 * Source location: User:Vanisaac/scripts/TartanBuilder.js
 * Documentation: User:Vanisaac/scripts/TartanBuilder
 * 
 */
// <pre><nowiki>

function TartanBuild(){
var colors={
	a:	'#80D0FF',
	b:	'#2888C4',
	c:	'#2C2C80',
	d:	'#404040',
	e:	'#808080',
	f:	'#004028',
	g:	'#008000',
	h:	'#7CC06C',
	i:	'#808080',
	j:	'#808080',
	k:	'#000000',
	l:	'#C0C0C0',
	m:	'#880000',
	n:	'#808080',
	o:	'#EC8048',
	p:	'#780078',
	q:	'#808080',
	r:	'#C80000',
	s:	'#FF6060',
	t:	'#604000',
	u:	'#441800',
	v:	'#D0A870',
	w:	'#FFFFFF',
	x:	'#808080',
	y:	'#FCCC00',
	z:	'#C89800',
	dummy: null
}


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(/ /g, "");

var codeoutput = "Sett: " + sett;			// String for output of code for display
window.alert(codeoutput);


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.charat(i) >= "a" && sett.charat(i) <= "z" ){
			colorarray.push (sett.charat(i));
			lenarray.push (i);
		}
	}
	for (i=0; i<colorarray.length; i++){	// Extracts thread counts from sett string
		threadarray.push (sett.substring(lenarray[i]+1, 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
			if (colorarray[j] == i) {				// value is current color, push count
				if (j!==0) {						// of other other thread, unless this
					dasharray.push (othercount.toString());	// 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){
		window.alert ("#" + i + " {stroke:" + colors[i] + "; stroke-dasharray:" +
		dasharray.toString() + ";}\n");
	}
}
} // </nowiki></pre>