alecpl
2011-03-27 94dfd8ab9d61a969453fdd3b9be14b58e1576816
program/js/tiny_mce/themes/advanced/js/color_picker.js
@@ -1,6 +1,6 @@
tinyMCEPopup.requireLangPack();
var detail = 50, strhex = "0123456789abcdef", i, isMouseDown = false, isMouseOver = false;
var detail = 50, strhex = "0123456789ABCDEF", i, isMouseDown = false, isMouseOver = false;
var colors = [
   "#000000","#000033","#000066","#000099","#0000cc","#0000ff","#330000","#330033",
@@ -58,12 +58,16 @@
   '#F5DEB3':'Wheat','#FFFFFF':'White','#F5F5F5':'WhiteSmoke','#FFFF00':'Yellow','#9ACD32':'YellowGreen'
};
var namedLookup = {};
function init() {
   var inputColor = convertRGBToHex(tinyMCEPopup.getWindowArg('input_color'));
   var inputColor = convertRGBToHex(tinyMCEPopup.getWindowArg('input_color')), key, value;
   tinyMCEPopup.resizeToInnerSize();
   generatePicker();
   generateWebColors();
   generateNamedColors();
   if (inputColor) {
      changeFinalColor(inputColor);
@@ -73,6 +77,45 @@
      if (col)
         updateLight(col.r, col.g, col.b);
   }
   for (key in named) {
      value = named[key];
      namedLookup[value.replace(/\s+/, '').toLowerCase()] = key.replace(/#/, '').toLowerCase();
   }
}
function toHexColor(color) {
   var matches, red, green, blue, toInt = parseInt;
   function hex(value) {
      value = parseInt(value).toString(16);
      return value.length > 1 ? value : '0' + value; // Padd with leading zero
   };
   color = color.replace(/[\s#]+/g, '').toLowerCase();
   color = namedLookup[color] || color;
   matches = /^rgb\((\d{1,3}),(\d{1,3}),(\d{1,3})\)|([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{2})|([a-f0-9])([a-f0-9])([a-f0-9])$/.exec(color);
   if (matches) {
      if (matches[1]) {
         red = toInt(matches[1]);
         green = toInt(matches[2]);
         blue = toInt(matches[3]);
      } else if (matches[4]) {
         red = toInt(matches[4], 16);
         green = toInt(matches[5], 16);
         blue = toInt(matches[6], 16);
      } else if (matches[7]) {
         red = toInt(matches[7] + matches[7], 16);
         green = toInt(matches[8] + matches[8], 16);
         blue = toInt(matches[9] + matches[9], 16);
      }
      return '#' + hex(red) + hex(green) + hex(blue);
   }
   return '';
}
function insertAction() {
@@ -81,7 +124,7 @@
   tinyMCEPopup.restoreSelection();
   if (f)
      f(color);
      f(toHexColor(color));
   tinyMCEPopup.close();
}
@@ -91,7 +134,7 @@
      document.getElementById("colorname").innerHTML = name;
   document.getElementById("preview").style.backgroundColor = color;
   document.getElementById("color").value = color.toLowerCase();
   document.getElementById("color").value = color.toUpperCase();
}
function convertRGBToHex(col) {
@@ -153,23 +196,40 @@
   if (el.className == 'generated')
      return;
   h += '<table border="0" cellspacing="1" cellpadding="0">'
   // TODO: VoiceOver doesn't seem to support legend as a label referenced by labelledby.
   h += '<div role="listbox" aria-labelledby="webcolors_title" tabindex="0"><table role="presentation" border="0" cellspacing="1" cellpadding="0">'
      + '<tr>';
   for (i=0; i<colors.length; i++) {
      h += '<td bgcolor="' + colors[i] + '" width="10" height="10">'
         + '<a href="javascript:insertAction();" onfocus="showColor(\'' + colors[i] +  '\');" onmouseover="showColor(\'' + colors[i] +  '\');" style="display:block;width:10px;height:10px;overflow:hidden;">'
         + '</a></td>';
         + '<a href="javascript:insertAction();" role="option" tabindex="-1" aria-labelledby="web_colors_' + i + '" onfocus="showColor(\'' + colors[i] + '\');" onmouseover="showColor(\'' + colors[i] + '\');" style="display:block;width:10px;height:10px;overflow:hidden;">';
      if (tinyMCEPopup.editor.forcedHighContrastMode) {
         h += '<canvas class="mceColorSwatch" height="10" width="10" data-color="' + colors[i] + '"></canvas>';
      }
      h += '<span class="mceVoiceLabel" style="display:none;" id="web_colors_' + i + '">' + colors[i].toUpperCase() + '</span>';
      h += '</a></td>';
      if ((i+1) % 18 == 0)
         h += '</tr><tr>';
   }
   h += '</table>';
   h += '</table></div>';
   el.innerHTML = h;
   el.className = 'generated';
   paintCanvas(el);
   enableKeyboardNavigation(el.firstChild);
}
function paintCanvas(el) {
   tinyMCEPopup.getWin().tinymce.each(tinyMCEPopup.dom.select('canvas.mceColorSwatch', el), function(canvas) {
      var context;
      if (canvas.getContext && (context = canvas.getContext("2d"))) {
         context.fillStyle = canvas.getAttribute('data-color');
         context.fillRect(0, 0, 10, 10);
      }
   });
}
function generateNamedColors() {
   var el = document.getElementById('namedcolors'), h = '', n, v, i = 0;
@@ -178,11 +238,27 @@
   for (n in named) {
      v = named[n];
      h += '<a href="javascript:insertAction();" onmouseover="showColor(\'' + n +  '\',\'' + v + '\');" style="background-color: ' + n + '"><!-- IE --></a>'
      h += '<a href="javascript:insertAction();" role="option" tabindex="-1" aria-labelledby="named_colors_' + i + '" onfocus="showColor(\'' + n + '\',\'' + v + '\');" onmouseover="showColor(\'' + n + '\',\'' + v + '\');" style="background-color: ' + n + '">';
      if (tinyMCEPopup.editor.forcedHighContrastMode) {
         h += '<canvas class="mceColorSwatch" height="10" width="10" data-color="' + colors[i] + '"></canvas>';
      }
      h += '<span class="mceVoiceLabel" style="display:none;" id="named_colors_' + i + '">' + v + '</span>';
      h += '</a>';
      i++;
   }
   el.innerHTML = h;
   el.className = 'generated';
   paintCanvas(el);
   enableKeyboardNavigation(el);
}
function enableKeyboardNavigation(el) {
   tinyMCEPopup.editor.windowManager.createInstance('tinymce.ui.KeyboardNavigation', {
      root: el,
      items: tinyMCEPopup.dom.select('a', el)
   }, tinyMCEPopup.dom);
}
function dechex(n) {