User:Canadabonk/sandbox.js: Difference between revisions

From TestWiki
Content added Content deleted
(wip)
No edit summary
Line 6: Line 6:
color: black;
color: black;
padding:10px;
padding:10px;
}
#cosmostweaks-coloreditor td, #cosmostweaks-coloreditor th {
border:1px solid black;
}
}
`);
`);
Line 35: Line 38:
for (const x of CSSvarsList) {
for (const x of CSSvarsList) {
lightmodevars[x] = cosmostweaksCSS[0].style.getPropertyValue(x);
lightmodevars[x] = tohex(cosmostweaksCSS[0].style.getPropertyValue(x));
darkmodevars[x] = cosmostweaksCSS[1].style.getPropertyValue(x);
darkmodevars[x] = tohex(cosmostweaksCSS[1].style.getPropertyValue(x));
}
}
Line 48: Line 51:
$('#cosmostweaks-coloreditor').html(`
$('#cosmostweaks-coloreditor').html(`
<h3>Use this to visualise what a color scheme will look like!</h3>
<h3 style="margin:0;color:black">Use this to visualise what a color scheme will look like!</h3>
<p>Anything done here is not saved, this is just for preview purposes.<br>
<p>Anything done here is not saved, this is just for preview purposes.<br>
Paste the resulting code into your "Mediawiki:Gadget-cosmostweaks.css" to apply it.<br>
Paste the resulting code into your "Mediawiki:Gadget-cosmostweaks.css" to apply it.<br>
This tool can only work with hexcodes (like "#000000"). Swap them out for RGBA values with opacity in the CSS page, if desired.</p>
This tool can only work with hexcodes (like "#000000"). Swap them out for RGBA values with opacity in the CSS page, if desired.</p>
<table style="width:100%">
<table style="width:100%">
<tr><td></td><th>Light mode</th><th>Dark mode</th></tr>
<tr><td style="width:30%"></td><th>Light mode</th><th>Dark mode</th></tr>
${CSSvarsRows()}
${CSSvarsRows()}
</table>
</table>
Line 60: Line 63:
}
}
});
});

function tohex(color){
if (color.match(/^rgb/)) {
rgb = color.match(/^rgba?[\s+]?\([\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?/i);
return (rgb && rgb.length === 4) ? "#" +
("0" + parseInt(rgb[1],10).toString(16)).slice(-2) +
("0" + parseInt(rgb[2],10).toString(16)).slice(-2) +
("0" + parseInt(rgb[3],10).toString(16)).slice(-2) : '';
} else if (color.match(/^#.../)) {
return "#" + color[1] + color[1] + color[2] + color[2] + color[3] + color[3];
} else {
return color;
}
}

Revision as of 04:08, 26 June 2024

$(function(){
	if ($('#cosmostweaks-coloreditor').length) {
		mw.util.addCSS(`
			#cosmostweaks-coloreditor {
				background-color: #eee;
				color: black;
				padding:10px;
			}
			#cosmostweaks-coloreditor td, #cosmostweaks-coloreditor th {
				border:1px solid black;
			}
		`);
		
		const CSSvarsList = [
			'--main-background-color', 
			'--content-background-color', 
			'--text-color', 
			'--link-color', 
			'--banner-color', 
			'--banner-text-color', 
			'--header-color', 
			'--header-text-color', 
			'--button-color', 
			'--button-text-color', 
			'--footer-color'];
			
		const cosmostweaksCSS = [];
		$.each(document.styleSheets, function(index, sheet){
			const source = sheet.ownerNode.href || 'none';
			if (source.match('Canadabonk')) {
				cosmostweaksCSS.push(sheet.cssRules[0]);
				cosmostweaksCSS.push(sheet.cssRules[1]);	
			}
		});
		
		const lightmodevars = {};
		const darkmodevars = {};
		
		for (const x of CSSvarsList) {
			lightmodevars[x] = tohex(cosmostweaksCSS[0].style.getPropertyValue(x));
			darkmodevars[x] = tohex(cosmostweaksCSS[1].style.getPropertyValue(x));
		}
		
		function CSSvarsRows() {
			text = "";
			for (const x of CSSvarsList) {
				text += `<tr><th>${x}</th><td>${lightmodevars[x]}</td><td>${darkmodevars[x]}</td></tr>\n`;
			}
			return text;
		}
		
		$('#cosmostweaks-coloreditor').html(`
			<h3 style="margin:0;color:black">Use this to visualise what a color scheme will look like!</h3>
			<p>Anything done here is not saved, this is just for preview purposes.<br>
			Paste the resulting code into your "Mediawiki:Gadget-cosmostweaks.css" to apply it.<br>
			This tool can only work with hexcodes (like "#000000"). Swap them out for RGBA values with opacity in the CSS page, if desired.</p>
			<table style="width:100%">
				<tr><td style="width:30%"></td><th>Light mode</th><th>Dark mode</th></tr>
				${CSSvarsRows()}
			</table>
		
		`);
	}
});

function tohex(color){
	if (color.match(/^rgb/)) {
		rgb = color.match(/^rgba?[\s+]?\([\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?/i);
		return (rgb && rgb.length === 4) ? "#" +
		("0" + parseInt(rgb[1],10).toString(16)).slice(-2) +
		("0" + parseInt(rgb[2],10).toString(16)).slice(-2) +
		("0" + parseInt(rgb[3],10).toString(16)).slice(-2) : '';
	} else if (color.match(/^#.../)) {
		return "#" + color[1] + color[1] + color[2] + color[2] + color[3] + color[3];
	} else {
		return color;
	}
}