User:Peaskey/Javascript/rainbow.js

var gradientType = 'oneway'; $(document).ready( function 		{								$("#gradient_1").spectrum( {				showButtons: false, showInput: true, change: function (color) {					previewText; }			});					$("#gradient_2").spectrum( {				showButtons: false, showInput: true, change: function (color) {					previewText; }			});				$(document).on("click", "#gradient_menu a", function {				$("#gradient_menu a").removeClass("active"); $(this).addClass("active"); if (parseInt($(this).attr("href").substr(1)) == 2) {					$("#gradient_options ul li").eq(0).hide; $("#gradient_options ul li").eq(1).hide; }				else {					$("#gradient_options ul li").eq(0).show; $("#gradient_options ul li").eq(1).show; }				previewText; return false; });					$(document).on("click", "#generate", function {				var sfxmode = parseInt($("#gradient_menu a.active").attr("href").substr(1)); var result = MakeSFX( $.trim($("#textInput").val), $("#html_output").prop("checked"), sfxmode); $("#textOutput").val(result); return false; });						$("#gradient_repeat").bind("keyup", function {				previewText; });				$("#textOutput").bind("click", function {				$(this).select; });			previewText;		}); function previewText {			var sfxmode = parseInt($("#gradient_menu a.active").attr("href").substr(1)); $("#preview").html( MakeSFX("This is what your text will look like!", true, sfxmode)); }

// base code by umop ap!sdn // modified for jquery and this page function tohex(decval) {		var l, h;		var str = "";

l = Math.floor(decval % 16); h = Math.floor(decval / 16);

if (h < 10) {			str = "" + h;		} if (h > 9) {			switch(h) {				case 10: str = "A"; break; case 11: str = "B"; break; case 12: str = "C"; break; case 13: str = "D"; break; case 14: str = "E"; break; case 15: str = "F"; break; default: str = "X"; break; }		}

if (l < 10) {			str = str + "" + l;		} if (l > 9) {			switch(l) {				case 10: str += "A"; break; case 11: str += "B"; break; case 12: str += "C"; break; case 13: str += "D"; break; case 14: str += "E"; break; case 15: str += "F"; break; default: str += "X"; break; }		}

return str; }

function todec(hexval) {		var l, h;		hexstr = new String(hexval).toUpperCase;

switch(hexstr.charAt(0)) {			case "A": h = 10; break; case "B": h = 11; break; case "C": h = 12; break; case "D": h = 13; break; case "E": h = 14; break; case "F": h = 15; break; default: h = eval(hexstr.charAt(0)); }

switch(hexstr.charAt(1)) {			case "A": l = 10; break; case "B": l = 11; break; case "C": l = 12; break; case "D": l = 13; break; case "E": l = 14; break; case "F": l = 15; break; default: l = eval(hexstr.charAt(1)); }

return l + 16 * h;	}

function hexToRGB(hexval) {		// Get the input value as a string str = new String(hexval).toUpperCase;

// If there is a hash mark, remove it		if (str.charAt(0) == "#") str = str.substr(1);

// Obtain RGB values from hex code g_r = todec(str.substr(0, 2)); g_g = todec(str.substr(2, 2)); g_b = todec(str.substr(4, 2)); }

function getSFXColor(k) {		var r, g, b, k1, min, max;

// Are we doing rainbow? if (g_cstyle == 0) {			//if (document.colorform.rvsdir.checked == 0) k1 = k;			//else //	k1 = 5.3 - k;

r = 127 + 127 * Math.cos(k1 - .5); g = 127 + 127 * Math.cos(k1 - 2.5); b = 127 + 127 * Math.cos(k1 - 4.5);

/*			if (document.colorform.invcol.checked != 0) {				r = 255 - r;				g = 255 - g;				b = 255 - b;			} */			// Make fully saturated min = r;			if (g < min) min = g;			if (b < min) min = b;

// Subtract the minimum from all values so that the darkest becomes zero r -= min; g -= min; b -= min;

// Now find the highest value max = r;			if (g > max) max = g;			if (b > max) max = b;

// Scale the RGB values up so that the brightest equals the brightness input value. max = 255.0 / max;		// (document.colorform.bright.value - (255 - document.colorform.cont.value)) / max; r *= max; g *= max; b *= max;

// Adjust for brightness and contrast var tekBright = 200; var tekContrast = 255; //max = (document.colorform.bright.value / 255) * (document.colorform.cont.value / 255); max = (tekBright / 255) * (tekContrast / 255); //min = (255 - document.colorform.cont.value) * (document.colorform.bright.value / 255); min = (255 - tekContrast) * (tekBright / 255);

r = r*max + min; g = g*max + min; b = b*max + min;

if (r < 0) r = 0; if (g < 0) g = 0; if (b < 0) b = 0;

if (r > 255) r = 255; if (g > 255) g = 255; if (b > 255) b = 255;

}			// color style == rainbow

// Are we doing gradients? if (g_cstyle == 1) {			k -= Math.floor(k); r = r1 + k * dr; g = g1 + k * dg; b = b1 + k * db; }			// color style == grad1

if (g_cstyle == 2) {			k -= 2 * Math.floor(k/2);

if (k < 1) {				r = r1 + k * dr; g = g1 + k * dg; b = b1 + k * db; }

if (k >= 1) {				k -= 2; r = r1 - k * dr; g = g1 - k * dg; b = b1 - k * db; }

}			// color style == grad2

if (g_cstyle == 3) {			k -= 3 * Math.floor(k/3);

if (k < 1) {				r = r1 + k * dr; g = g1 + k * dg; b = b1 + k * db; }

if (k >= 1 && k < 2) {				k -= 1; r = r2 + k * dr1; g = g2 + k * dg1; b = b2 + k * db1; }

if (k >= 2) {				k -= 2; r = r3 + k * dr2; g = g3 + k * dg2; b = b3 + k * db2; }

}			// color style == grad3

g_r = r;		g_g = g;		g_b = b;	}

function setOutSizeIndicator(divtext) {		document.getElementById("charssub").setAttribute("id", "oldsub"); var newdiv=document.createElement("div"); newdiv.setAttribute("id", "charssub"); var newtext=document.createTextNode(divtext); newdiv.appendChild(newtext); document.getElementById("chars").appendChild(newdiv); document.getElementById("chars").removeChild(document.getElementById("oldsub")); }

function MakeSFX(inputString, outputHTML, sfxmode) {		var r, g, b;		var i, j, k, l;		var x, scale, res; var min, max; var in_tag = 0; var oignumi = 0; temp = new String(""); var numreps = parseInt($("#gradient_repeat").val); if (numreps < 1) numreps = 1; if (numreps > 10 ) numreps = 10;

// Initialize string variables //instr = new String(document.colorform.intxt.value); instr = inputString; outstr = new String(""); tempstr = new String(""); res = 1;

// Get the length and scale. For rainbows, the scale must be such that one cycle comes out to almost 2pi. j = instr.length; if (sfxmode == 2) {			scale = Math.PI * (2 * eval(numreps) - .21) / j;			g_cstyle = 0; }		if (sfxmode == 1) {			scale = numreps / j;			g_cstyle = 1; }		if (sfxmode == 0) {			scale = 2.0 * numreps / j;			g_cstyle = 2; }		if (gradientType == "tricolor") {			scale = 3.0 * numreps / j;			g_cstyle = 3; }

if (sfxmode != 2) {			hexToRGB($("#gradient_1").val);

// Obtain RGB values from hex codes r1 = g_r; g1 = g_g; b1 = g_b;

hexToRGB($("#gradient_2").val);

r2 = g_r; g2 = g_g; b2 = g_b;

// Find delta values (decimal figure used to ensure floating point math) dr = 0.0 + r2 - r1; dg = 0.0 + g2 - g1; db = 0.0 + b2 - b1; }

// Start the loop for (i=0; i<j; i++) {			// First of all, is this the opening bracket of a tag? if (instr.charAt(i) == "<") in_tag = 1;

// If this character is within a tag, do not apply colorization to it.

if (in_tag == 0) {				// Determine the RGB values k = scale * i;

getSFXColor(k); r = g_r; g = g_g; b = g_b;

// Convert to hexadecimal tempstr = tohex(r) + tohex(g) + tohex(b);

// Get the character to colorize temp = instr.charAt(i);

// Is it a &; code? if (instr.charAt(i) == "&") {

// Search forward until either a semicolon, tag, or space is found for (l=i+1; l") break; if (instr.charAt(l) == ";") break; }			// for l

// If it's a semicolon, then we have ourselves a character. if (instr.charAt(l) == ";") {						temp = instr.substr(i, l-i+1); }

}			// temp == "&"

// Are we outputting HTML or BBCode? //if (document.colorform.out_format.value == "html") if (outputHTML) {					if (i % res == 0) { 						outstr = outstr + ""; oignumi = 1; }					outstr = outstr + temp; if ((i+1) % res == 0) { 						outstr = outstr + " "; oignumi = 0; }				}			// out_format = HTML else {					if (i % res == 0) { 						outstr = outstr + "[color=#" + tempstr + "]"; oignumi = 1; }					outstr = outstr + temp; if ((i+1) % res == 0) {						outstr = outstr + "[/color]"; oignumi = 0; }				}			// out_format = BBCode

if (temp.length > 1) i += (temp.length - 1);

}	// in_tag == 0

if (in_tag == 1) outstr = outstr + instr.charAt(i);

// We have to check for the closing bracket of a tag last so it doesn't get colorized. if (instr.charAt(i) == ">") in_tag = 0; }

if (oignumi > 0) {			if (document.colorform.out_format.value == "html") outstr = outstr + " "; if (document.colorform.out_format.value == "bbc") outstr = outstr + "[/color]"; }

return outstr; }

function UpdateRGB(ctl) {		var lum;

// Set the background color to whatever the user has typed in		ctl.style.backgroundColor = ctl.value;

// Get RGB values from the user's selection hexToRGB(ctl.value);

// Obtain luminance measurement lum = .29 * g_r + .57 * g_g + .14 * g_b;

// If it is less than 96, set foreground color to white, otherwise set to black. if (lum < 96) {			ctl.style.color = "#FFFFFF"; }		else {			ctl.style.color = "#000000"; }	}

function flipbkg(ctl) {		if (prevbkc == "#FFFFFF") prevbkc = "#000000"; else prevbkc = "#FFFFFF";

ctl.style.backgroundColor = prevbkc; }