// COLOR BAR * Code Based On PHPBB2 hack by mat < mat100.AT.ifrance.com >
// Reworked/Converted/Improved for use on SMF boards by Karlbenson aka(Smack!) <URL:http://www.youposted.com/>
// V1.1 - Modified for RC3, now uses SMF insert BBcode function, better css support, explanation comments
// V1.0 - Added Greyscale Colors

var cpm = 25;// Colors Per Matrice eg 25 yellow shades etc

function color(hexcolor)
{
	surroundText('[color='+ hexcolor + ']', '[/color]', document.forms.postmodify.message);
}

var base_hexa = "0123456789ABCDEF";

function dec2Hexa(number)
{
   return base_hexa.charAt(Math.floor(number / 16)) + base_hexa.charAt(number % 16);
}

function RGB2Hexa(TR,TG,TB)
{
  return "#" + dec2Hexa(TR) + dec2Hexa(TG) + dec2Hexa(TB);
}

// THE COLOR MATRICES
col = new Array;
col[0] = new Array(255,0,255,0,255,-1);
col[1] = new Array(255,-1,255,0,0,0);
col[2] = new Array(0,0,255,0,0,1);
col[3] = new Array(0,0,255,-1,255,0);
col[4] = new Array(0,1,0,0,255,0);
col[5] = new Array(255,0,0,0,255,-1);
col[6] = new Array(255,-1,0,0,0,0);

// CREATES ALL THE RGB COLORS
function rgb(cpm){
for (j=0;j<6+1;j++){
    for (i=0;i<cpm+1;i++)
	{
	r = Math.floor(col[j][0]+col[j][1]*i*(255)/cpm);
	g = Math.floor(col[j][2]+col[j][3]*i*(255)/cpm);
	b = Math.floor(col[j][4]+col[j][5]*i*(255)/cpm);
  codehex = r + '' + g + '' + b;
  document.write(' <td class="colorcell" bgColor="' + RGB2Hexa(r,g,b) + '" onClick="color(this.bgColor);document.getElementById(\'colorused\').bgColor = this.bgColor;" '
         + 'onmouseover="document.getElementById(\'colorused1\').bgColor = this.bgColor;this.style.cursor=\'pointer\'" title="Insert Color BBCode [color='+ RGB2Hexa(r,g,b) + '][/color]"></td>\n');
	}}}

// CREATE THE GREYSCALE COLORS
function greyscale(cpm) {
    for (i=0;i<cpm;i++) {
		v = Math.floor(i*(255)/cpm);
		codehex = v + '' + v + '' + v;
		document.write(' <td class="colorcell" bgColor="' + RGB2Hexa(v,v,v) + '" onClick="color(this.bgColor);document.getElementById(\'colorused\').bgColor = this.bgColor;" '
         + 'onmouseover="document.getElementById(\'colorused1\').bgColor = this.bgColor;this.style.cursor=\'pointer\'" title="Insert Color BBCode [color='+ RGB2Hexa(v,v,v) + '][/color]"></td>\n');
	}
}

// WRITES THE COLOR BAR TABLE
document.write('<table cellspacing="0" width="450px" cellpadding="0" class="colorbar"><tr>')
document.write('<td id="colorused" onClick="color(this.bgColor);"  bgColor="#FFFFFF" onMouseOver="this.style.cursor=\'pointer\'" class="colorbox">')
document.write('<img src="Themes/default/images/blank.gif" class="colorboxspacer" alt="Insert The Colour Last Clicked"/></td><td width="3px"></td>')
document.write('<td id="colorused1" name="color_bar" bgColor="#000000" onClick="color(this.bgColor);" color(this.bgColor); onMouseOver="this.style.cursor=\'pointer\'" class="colorbox">')
document.write('<img src="Themes/default/images/blank.gif" class="colorboxspacer"  alt="Insert The Colour Last Hovered Over" /></td><td width="3px"></td>')
document.write('<!--' + rgb(cpm) + '// -->') 
document.write('<!--' + greyscale(cpm) + '// -->') 
document.write('</tr></table>')
