///////////////////////////////////////////////////////////////////////////////////////////////////
// File: colors.js
// Creator: Monjay Settro, Devbleue Inc.
// Purpose: color functions, entry point being pastel()
//
// pastel() could not have been acheived without these slayeroffice
// "Cross Browser Gradient Backgrounds" code functions which have been 
// modified to localize the variables (grrr!):
//    createColorPath()
//    setColorHue()
//    longHexToDec()
//    toDec()
// See http://slayeroffice.com/code/gradient/ for more examples.
///////////////////////////////////////////////////////////////////////////////////////////////////
function createColorPath(color1,color2) {
	var colorPath = new Array();
	    colorPercent = 1.0;
	do {
		colorPath[colorPath.length]=setColorHue(longHexToDec(color1),colorPercent,longHexToDec(color2));
		colorPercent-=.01;
	} while(colorPercent>0);
	return colorPath;
}
function setColorHue(originColor,opacityPercent,maskRGB) {
	var returnColor=new Array();
	for(var w=0;w<originColor.length;w++) returnColor[w] = Math.round(originColor[w]*opacityPercent) + Math.round(maskRGB[w]*(1.0-opacityPercent));
	return returnColor;
}
function longHexToDec(longHex) {
	return new Array(toDec(longHex.substring(0,2)),toDec(longHex.substring(2,4)),toDec(longHex.substring(4,6)));	
}
function toDec(hex) {	
	return parseInt(hex,16);
}
///////////////////////////////////////////////////////////////////////////////////////////////////
function map_named_color(name)
{
   var map = new Array();
       map['AliceBlue'] = '#F0F8FF'
       map['AntiqueWhite'] = '#FAEBD7'
       map['Aqua'] = '#00FFFF'
       map['Aquamarine'] = '#7FFFD4'
       map['Azure'] = '#F0FFFF'
       map['Beige'] = '#F5F5DC'
       map['Bisque'] = '#FFE4C4'
       map['Black'] = '#000000'
       map['BlanchedAlmond'] = '#FFEBCD'
       map['Blue'] = '#0000FF'
       map['BlueViolet'] = '#8A2BE2'
       map['Brown'] = '#A52A2A'
       map['BurlyWood'] = '#DEB887'
       map['CadetBlue'] = '#5F9EA0'
       map['Chartreuse'] = '#7FFF00'
       map['Chocolate'] = '#D2691E'
       map['Coral'] = '#FF7F50'
       map['CornflowerBlue'] = '#6495ED'
       map['Cornsilk'] = '#FFF8DC'
       map['Crimson'] = '#DC143C'
       map['Cyan'] = '#00FFFF'
       map['DarkBlue'] = '#00008B'
       map['DarkCyan'] = '#008B8B'
       map['DarkGoldenRod'] = '#B8860B'
       map['DarkGray'] = '#A9A9A9'
       map['DarkGrey'] = '#A9A9A9'
       map['DarkGreen'] = '#006400'
       map['DarkKhaki'] = '#BDB76B'
       map['DarkMagenta'] = '#8B008B'
       map['DarkOliveGreen'] = '#556B2F'
       map['Darkorange'] = '#FF8C00'
       map['DarkOrchid'] = '#9932CC'
       map['DarkRed'] = '#8B0000'
       map['DarkSalmon'] = '#E9967A'
       map['DarkSeaGreen'] = '#8FBC8F'
       map['DarkSlateBlue'] = '#483D8B'
       map['DarkSlateGray'] = '#2F4F4F'
       map['DarkSlateGrey'] = '#2F4F4F'
       map['DarkTurquoise'] = '#00CED1'
       map['DarkViolet'] = '#9400D3'
       map['DeepPink'] = '#FF1493'
       map['DeepSkyBlue'] = '#00BFFF'
       map['DimGray'] = '#696969'
       map['DimGrey'] = '#696969'
       map['DodgerBlue'] = '#1E90FF'
       map['FireBrick'] = '#B22222'
       map['FloralWhite'] = '#FFFAF0'
       map['ForestGreen'] = '#228B22'
       map['Fuchsia'] = '#FF00FF'
       map['Gainsboro'] = '#DCDCDC'
       map['GhostWhite'] = '#F8F8FF'
       map['Gold'] = '#FFD700'
       map['GoldenRod'] = '#DAA520'
       map['Gray'] = '#808080'
       map['Grey'] = '#808080'
       map['Green'] = '#008000'
       map['GreenYellow'] = '#ADFF2F'
       map['HoneyDew'] = '#F0FFF0'
       map['HotPink'] = '#FF69B4'
       map['IndianRed '] = '#CD5C5C'
       map['Indigo '] = '#4B0082'
       map['Ivory'] = '#FFFFF0'
       map['Khaki'] = '#F0E68C'
       map['Lavender'] = '#E6E6FA'
       map['LavenderBlush'] = '#FFF0F5'
       map['LawnGreen'] = '#7CFC00'
       map['LemonChiffon'] = '#FFFACD'
       map['LightBlue'] = '#ADD8E6'
       map['LightCoral'] = '#F08080'
       map['LightCyan'] = '#E0FFFF'
       map['LightGoldenRodYellow'] = '#FAFAD2'
       map['LightGray'] = '#D3D3D3'
       map['LightGrey'] = '#D3D3D3'
       map['LightGreen'] = '#90EE90'
       map['LightPink'] = '#FFB6C1'
       map['LightSalmon'] = '#FFA07A'
       map['LightSeaGreen'] = '#20B2AA'
       map['LightSkyBlue'] = '#87CEFA'
       map['LightSlateGray'] = '#778899'
       map['LightSlateGrey'] = '#778899'
       map['LightSteelBlue'] = '#B0C4DE'
       map['LightYellow'] = '#FFFFE0'
       map['Lime'] = '#00FF00'
       map['LimeGreen'] = '#32CD32'
       map['Linen'] = '#FAF0E6'
       map['Magenta'] = '#FF00FF'
       map['Maroon'] = '#800000'
       map['MediumAquaMarine'] = '#66CDAA'
       map['MediumBlue'] = '#0000CD'
       map['MediumOrchid'] = '#BA55D3'
       map['MediumPurple'] = '#9370D8'
       map['MediumSeaGreen'] = '#3CB371'
       map['MediumSlateBlue'] = '#7B68EE'
       map['MediumSpringGreen'] = '#00FA9A'
       map['MediumTurquoise'] = '#48D1CC'
       map['MediumVioletRed'] = '#C71585'
       map['MidnightBlue'] = '#191970'
       map['MintCream'] = '#F5FFFA'
       map['MistyRose'] = '#FFE4E1'
       map['Moccasin'] = '#FFE4B5'
       map['NavajoWhite'] = '#FFDEAD'
       map['Navy'] = '#000080'
       map['OldLace'] = '#FDF5E6'
       map['Olive'] = '#808000'
       map['OliveDrab'] = '#6B8E23'
       map['Orange'] = '#FFA500'
       map['OrangeRed'] = '#FF4500'
       map['Orchid'] = '#DA70D6'
       map['PaleGoldenRod'] = '#EEE8AA'
       map['PaleGreen'] = '#98FB98'
       map['PaleTurquoise'] = '#AFEEEE'
       map['PaleVioletRed'] = '#D87093'
       map['PapayaWhip'] = '#FFEFD5'
       map['PeachPuff'] = '#FFDAB9'
       map['Peru'] = '#CD853F'
       map['Pink'] = '#FFC0CB'
       map['Plum'] = '#DDA0DD'
       map['PowderBlue'] = '#B0E0E6'
       map['Purple'] = '#800080'
       map['Red'] = '#FF0000'
       map['RosyBrown'] = '#BC8F8F'
       map['RoyalBlue'] = '#4169E1'
       map['SaddleBrown'] = '#8B4513'
       map['Salmon'] = '#FA8072'
       map['SandyBrown'] = '#F4A460'
       map['SeaGreen'] = '#2E8B57'
       map['SeaShell'] = '#FFF5EE'
       map['Sienna'] = '#A0522D'
       map['Silver'] = '#C0C0C0'
       map['SkyBlue'] = '#87CEEB'
       map['SlateBlue'] = '#6A5ACD'
       map['SlateGray'] = '#708090'
       map['SlateGrey'] = '#708090'
       map['Snow'] = '#FFFAFA'
       map['SpringGreen'] = '#00FF7F'
       map['SteelBlue'] = '#4682B4'
       map['Tan'] = '#D2B48C'
       map['Teal'] = '#008080'
       map['Thistle'] = '#D8BFD8'
       map['Tomato'] = '#FF6347'
       map['Turquoise'] = '#40E0D0'
       map['Violet'] = '#EE82EE'
       map['Wheat'] = '#F5DEB3'
       map['White'] = '#FFFFFF'
       map['WhiteSmoke'] = '#F5F5F5'
       map['Yellow'] = '#FFFF00'
       map['YellowGreen'] = '#9ACD32'

   for(var i in map)
   {
      if (i.toLowerCase() == name.toLowerCase())
      {
         return map[i];
      }
   }
   return '';
} // map_named_color
function toHexMap(dec)
{
   if (dec  < 10) { return dec; }
   if (dec == 10) { return 'A'; }
   if (dec == 11) { return 'B'; }
   if (dec == 12) { return 'C'; }
   if (dec == 13) { return 'D'; }
   if (dec == 14) { return 'E'; }
   if (dec == 15) { return 'F'; }
   return ''
}
function toHex(dec)
{
   var Max = 256 -1
   if (dec > Max) { return -1 }
   if (dec < 17)  { return toHexMap(dec); }
   var whole = parseInt('' + (dec / 16) + '')
   var remainder = dec - whole * 16
   return toHexMap(whole) + '' + toHex(remainder)
}
function rgb_to_hex(rbg_string)
{
   // rbg(125,43,7) ==> #7D2B07
   var RRGGBB = rbg_string.replace(/(rgb\(|\))/g, '');   // replace 'rgb('  or ')' with empty string

   var R = '' + toHex(RRGGBB.split(',')[0]) + ''
       R = ((R.length < 2) ? '0' : '') + R
   
   var G = '' + toHex(RRGGBB.split(',')[1]) + ''
       G = ((G.length < 2) ? '0' : '') + G
   
   var B = '' + toHex(RRGGBB.split(',')[2]) + ''
       B = ((B.length < 2) ? '0' : '') + B
   
   return '#' + R + G + B
}
function normalize_color(color)
{
   //color can be: a named color, rgb(1-255, 1-255, 1-255), RRGGBB, #RRGGBB, RGB, #RGB
   //output should always be a #RRGGBB format color, if not then pastel() will break
   var RRGGBB = map_named_color(color)
   if (!RRGGBB)
   {
       RRGGBB = color;
   }
   if (RRGGBB.indexOf('rgb(') > -1)
   {
       RRGGBB = rgb_to_hex(RRGGBB)
       if (RRGGBB.indexOf('-') > -1)
       {
          //we have a bad rgb() number - i.e. one of the components is greater than 255
          return false;
       }
   }
   if (RRGGBB.substring(0,1) != '#')
   {
      RRGGBB = '#' + RRGGBB
   }
   if (RRGGBB.length == 4)  // change #RGB to #RRGGBB
   {
      RRGGBB = '#' + RRGGBB.substring(1,2) + RRGGBB.substring(1,2)
                   + RRGGBB.substring(2,3) + RRGGBB.substring(2,3)
                   + RRGGBB.substring(3,4) + RRGGBB.substring(3,4)
   }
   if (RRGGBB.length != 7)
   {
      return false;
   }
   return RRGGBB;
}
function pastel(color, opacity)
{
   // opacity 0   = 100% color
   // opacity 100 = color towards white
   var RRGGBB = normalize_color(color);
   if (RRGGBB)
   {
      RRGGBB = RRGGBB.replace(/\#/g, '');

      if (opacity == 100) { opacity = 99 }
      if ((!opacity) || (opacity < 1) || (opacity > 100))
      {
         opacity = 83;
      }

      var path = createColorPath(RRGGBB,'FFFFFF')
      if (path.length)
      {
         return rgb_to_hex('rgb(' + path[opacity][0] + ',' + path[opacity][1] + ',' + path[opacity][2] + ')')
      }
      else
      {
         return ''
      }
   }
}