User:Jamie Eilat/common.js

/* This is an altered version of a set of scripts written by Alex 21, in order to suit my own layout preferences */ var colour_names = {}; colour_names.aliceblue = "f0f8ff"; colour_names.antiquewhite = "faebd7"; colour_names.aqua = "00ffff"; colour_names.aquamarine = "7fffd4"; colour_names.azure = "f0ffff"; colour_names.beige = "f5f5dc"; colour_names.bisque = "ffe4c4"; colour_names.black = "000000"; colour_names.blanchedalmond = "ffebcd"; colour_names.blue = "0000ff"; colour_names.blueviolet = "8a2be2"; colour_names.brown = "a52a2a"; colour_names.burlywood = "deb887"; colour_names.cadetblue = "5f9ea0"; colour_names.chartreuse = "7fff00"; colour_names.chocolate = "d2691e"; colour_names.coral = "ff7f50"; colour_names.cornflowerblue = "6495ed"; colour_names.cornsilk = "fff8dc"; colour_names.crimson = "dc143c"; colour_names.cyan = "00ffff"; colour_names.darkblue = "00008b"; colour_names.darkcyan = "008b8b"; colour_names.darkgoldenrod = "b8860b"; colour_names.darkgray = "a9a9a9"; colour_names.darkgrey = "a9a9a9"; colour_names.darkgreen = "006400"; colour_names.darkkhaki = "bdb76b"; colour_names.darkmagenta = "8b008b"; colour_names.darkolivegreen = "556b2f"; colour_names.darkorange = "ff8c00"; colour_names.darkorchid = "9932cc"; colour_names.darkred = "8b0000"; colour_names.darksalmon = "e9967a"; colour_names.darkseagreen = "8fbc8f"; colour_names.darkslateblue = "483d8b"; colour_names.darkslategray = "2f4f4f"; colour_names.darkslategrey = "2f4f4f"; colour_names.darkturquoise = "00ced1"; colour_names.darkviolet = "9400d3"; colour_names.deeppink = "ff1493"; colour_names.deepskyblue = "00bfff"; colour_names.dimgray = "696969"; colour_names.dimgrey = "696969"; colour_names.dodgerblue = "1e90ff"; colour_names.firebrick = "b22222"; colour_names.floralwhite = "fffaf0"; colour_names.forestgreen = "228b22"; colour_names.fuchsia = "ff00ff"; colour_names.gainsboro = "dcdcdc"; colour_names.ghostwhite = "f8f8ff"; colour_names.gold = "ffd700"; colour_names.goldenrod = "daa520"; colour_names.gray = "808080"; colour_names.grey = "808080"; colour_names.green = "008000"; colour_names.greenyellow = "adff2f"; colour_names.honeydew = "f0fff0"; colour_names.hotpink = "ff69b4"; colour_names.indianred = "cd5c5c"; colour_names.indigo = "4b0082"; colour_names.ivory = "fffff0"; colour_names.khaki = "f0e68c"; colour_names.lavender = "e6e6fa"; colour_names.lavenderblush = "fff0f5"; colour_names.lawngreen = "7cfc00"; colour_names.lemonchiffon = "fffacd"; colour_names.lightblue = "add8e6"; colour_names.lightcoral = "f08080"; colour_names.lightcyan = "e0ffff"; colour_names.lightgoldenrodyellow = "fafad2"; colour_names.lightgray = "d3d3d3"; colour_names.lightgrey = "d3d3d3"; colour_names.lightgreen = "90ee90"; colour_names.lightpink = "ffb6c1"; colour_names.lightsalmon = "ffa07a"; colour_names.lightseagreen = "20b2aa"; colour_names.lightskyblue = "87cefa"; colour_names.lightslategray = "778899"; colour_names.lightslategrey = "778899"; colour_names.lightsteelblue = "b0c4de"; colour_names.lightyellow = "ffffe0"; colour_names.lime = "00ff00"; colour_names.limegreen = "32cd32"; colour_names.linen = "faf0e6"; colour_names.magenta = "ff00ff"; colour_names.maroon = "800000"; colour_names.mediumaquamarine = "66cdaa"; colour_names.mediumblue = "0000cd"; colour_names.mediumorchid = "ba55d3"; colour_names.mediumpurple = "9370db"; colour_names.mediumseagreen = "3cb371"; colour_names.mediumslateblue = "7b68ee"; colour_names.mediumspringgreen = "00fa9a"; colour_names.mediumturquoise = "48d1cc"; colour_names.mediumvioletred = "c71585"; colour_names.midnightblue = "191970"; colour_names.mintcream = "f5fffa"; colour_names.mistyrose = "ffe4e1"; colour_names.moccasin = "ffe4b5"; colour_names.navajowhite = "ffdead"; colour_names.navy = "000080"; colour_names.oldlace = "fdf5e6"; colour_names.olive = "808000"; colour_names.olivedrab = "6b8e23"; colour_names.orange = "ffa500"; colour_names.orangered = "ff4500"; colour_names.orchid = "da70d6"; colour_names.palegoldenrod = "eee8aa"; colour_names.palegreen = "98fb98"; colour_names.paleturquoise = "afeeee"; colour_names.palevioletred = "db7093"; colour_names.papayawhip = "ffefd5"; colour_names.peachpuff = "ffdab9"; colour_names.peru = "cd853f"; colour_names.pink = "ffc0cb"; colour_names.plum = "dda0dd"; colour_names.powderblue = "b0e0e6"; colour_names.purple = "800080"; colour_names.rebeccapurple = "663399"; colour_names.red = "ff0000"; colour_names.rosybrown = "bc8f8f"; colour_names.royalblue = "4169e1"; colour_names.saddlebrown = "8b4513"; colour_names.salmon = "fa8072"; colour_names.sandybrown = "f4a460"; colour_names.seagreen = "2e8b57"; colour_names.seashell = "fff5ee"; colour_names.sienna = "a0522d"; colour_names.silver = "c0c0c0"; colour_names.skyblue = "87ceeb"; colour_names.slateblue = "6a5acd"; colour_names.slategray = "708090"; colour_names.slategrey = "708090"; colour_names.snow = "fffafa"; colour_names.springgreen = "00ff7f"; colour_names.steelblue = "4682b4"; colour_names.tan = "d2b48c"; colour_names.teal = "008080"; colour_names.thistle = "d8bfd8"; colour_names.tomato = "ff6347"; colour_names.turquoise = "40e0d0"; colour_names.violet = "ee82ee"; colour_names.wheat = "f5deb3"; colour_names.white = "ffffff"; colour_names.whitesmoke = "f5f5f5"; colour_names.yellow = "ffff00"; colour_names.yellowgreen = "9acd32";

function colourCompliance(bground,fground){ var ratio; bground = bground.trim.toLowerCase;; if (colour_names[bground]) bground = colour_names[bground]; if (fground === false) { var ratio_w = getColorsRatio(bground,'FFFFFF'); var ratio_b = getColorsRatio(bground,'000000'); fground = (ratio_w > ratio_b ? 'FFFFFF' : '000000'); ratio = (ratio_w > ratio_b ? ratio_w : ratio_b); } else { ratio = getColorsRatio(bground,fground); }

if (ratio <= 7) { hsv = RGBtoHSV(br,bg,bb); bh = hsv.h; bs = hsv.s; bv = hsv.v;		if (bv < 100 && bv > 0) { bv += (fground == 'FFFFFF' ? -1 : 1); } else { bs += (fground == 'FFFFFF' ? 1 : -1); }		var new_bground = HSVtoRGB(bh,bs,bv); new_bground = RGBtoHEX(new_bground[0],new_bground[1],new_bground[2]); return colourCompliance(new_bground,fground,false); } else { if (bground.length == 3) bground = bground[0]+bground[0]+bground[1]+bground[1]+bground[2]+bground[2]; return bground.toUpperCase; } }

function getColorsRatio(bground,fground) { var bgroundH = HEXtoRGB(bground); var fgroundH = HEXtoRGB(fground); br = bgroundH.r; bg = bgroundH.g; bb = bgroundH.b;   fr = fgroundH.r; fg = fgroundH.g; fb = fgroundH.b;	var ratio = 1; var l1 = getLuminance([fr/255, fg/255, fb/255]); var l2 = getLuminance([br/255, bg/255, bb/255]);

if (l1 >= l2) { ratio = (l1 + 0.05) / (l2 + 0.05); } else { ratio = (l2 + 0.05) / (l1 + 0.05); }	ratio = Math.round(ratio * 100) / 100; return ratio; }

function getLuminance(rgb){ for (var i = 0; i < rgb.length; i++) { if (rgb[i] <= 0.03928) { rgb[i] = rgb[i] / 12.92; } else { rgb[i] = Math.pow( ((rgb[i]+0.055)/1.055), 2.4 ); }	}	var l = (0.2126 * rgb[0]) + (0.7152 * rgb[1]) + (0.0722 * rgb[2]); return l; }

function componentToHex(c) { var hex = parseInt(c).toString(16).toUpperCase; return hex.length == 1 ? "0" + hex : hex; }

function RGBtoHEX(r, g, b) { return componentToHex(r) + componentToHex(g) + componentToHex(b); }

function HEXtoRGB(hex) { if (hex.length == 6) { return { r: parseInt(hex[0]+''+hex[1], 16), g: parseInt(hex[2]+''+hex[3], 16), b: parseInt(hex[4]+''+hex[5], 16) };	} else { return { r: parseInt(hex[0]+''+hex[0], 16), g: parseInt(hex[1]+''+hex[1], 16), b: parseInt(hex[2]+''+hex[2], 16) };	} }

function RGBtoHSV { var rr, gg, bb, r = arguments[0] / 255, g = arguments[1] / 255, b = arguments[2] / 255, h, s,       v = Math.max(r, g, b), diff = v - Math.min(r, g, b), diffc = function(c){ return (v - c) / 6 / diff + 1 / 2; };

if (diff === 0) { h = s = 0; } else { s = diff / v;       rr = diffc(r); gg = diffc(g); bb = diffc(b);

if (r === v) { h = bb - gg; }else if (g === v) { h = (1 / 3) + rr - bb; }else if (b === v) { h = (2 / 3) + gg - rr; }       if (h < 0) { h += 1; }else if (h > 1) { h -= 1; }   }    return { h: Math.round(h * 360), s: Math.round(s * 100), v: Math.round(v * 100) }; }

function HSVtoRGB(h,s,v) { s = s / 100, v = v / 100;

var hi = Math.floor((h/60) % 6); var f = (h / 60) - hi; var p = v * (1 - s); var q = v * (1 - f * s); var t = v * (1 - (1 - f) * s);

var rgb = [];

switch (hi) { case 0: rgb = [v,t,p];break; case 1: rgb = [q,v,p];break; case 2: rgb = [p,v,t];break; case 3: rgb = [p,q,v];break; case 4: rgb = [t,p,v];break; case 5: rgb = [v,p,q];break; }

var r = Math.min(255, Math.round(rgb[0]*256)), g = Math.min(255, Math.round(rgb[1]*256)), b = Math.min(255, Math.round(rgb[2]*256));

return [r,g,b];

}

function RGBSTRtoHEX(rgb) { if (colour_names[rgb]) return '#'+colour_names[rgb]; rgb = rgb.split(','); rgb.r=parseInt(rgb[0].substring(4)).toString(16); rgb.g=parseInt(rgb[1]).toString(16); rgb.b=parseInt(rgb[2]).toString(16); var hex="#"+(rgb.r.length==1?'0':)+rgb.r+(rgb.g.length==1?'0':)+rgb.g+(rgb.b.length==1?'0':'')+rgb.b;	return hex.toUpperCase; }

function copyContent(text) { var textArea = document.createElement("textarea"); textArea.innerHTML = text; document.body.appendChild(textArea); textArea.select; if (!document.execCommand('copy')) console.log(text); document.body.removeChild(textArea); }

function newContentHeader(id) { var nav = document.createElement('nav'); nav.setAttribute('id',id); nav.setAttribute('aria-labelledby','p-tb-label'); nav.className = 'vector-menu vector-menu-portal portal'; nav.setAttribute('role','navigation'); var tb = document.getElementById('p-tb'); if(tb) { tb.parentNode.insertBefore(nav, tb.nextSibling); } else { return; }

var div = document.createElement('div'); div.className = 'body vector-menu-content'; document.getElementById(id).appendChild(div); }

newContentHeader('p-tv');

$(function($) {	setTimeout(function { $.when( mw.loader.using( ['mediawiki.util']), $.ready ).then(function {		var portletlink = mw.util.addPortletLink('p-tb', '#', 'Table colors');			$(portletlink).click( function(e) { e.preventDefault; // Default parameters and determine if we need to go to the editing page var loc = window.location.href; var wpTextbox1 = document.getElementById('wpTextbox1'); if (loc.indexOf('action=edit') < 0 && loc.indexOf('action=submit') < 0) { alert("Go to the edit page to use this script."); } else { // Gather colours from text of tables var s = wpTextbox1.value.split("\n"); for (var i = 0; i < s.length; i++) { // Regex for any number of parameters that use colours var reg = /\s*\|\s*(bgcolour|bgcolor|headercolour|headercolor|colour|color|fgcolour|fgcolor|background)[\s\=\#]*([0-9A-Za-z]*)/g; var t = reg.exec(s[i]); if (!t) continue; // Add colour to saved colours if its not already added var fontColor = t[2]; // Gather colour, and individually adjust to AAA compliancy var newcolour = colourCompliance(fontColor,false); var reg2 = new RegExp('('+fontColor+')','gi'); // Update if the original colour has been changed to be compliant, check for minus error if (newcolour.indexOf("-") < 0) wpTextbox1.value = wpTextbox1.value.replace(reg2,newcolour); else alert("Error fixing colour code "+fontColor+" - please fix manually."); }					// Done document.getElementById('wpSummary').value += "Adjusted color contrasts via script per WP:COLOR and Template talk:Infobox television season/Archive 3."; }			});		});	},700); });