User:Vanisaac/scripts/TartanBuilder.js
Appearance
< User:Vanisaac | scripts
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
![]() | This user script seems to have a documentation page at User:Vanisaac/scripts/TartanBuilder. |
/* 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");
}
}
}