color_picker.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. tinyMCEPopup.requireLangPack();
  2. var detail = 50, strhex = "0123456789abcdef", i, isMouseDown = false, isMouseOver = false;
  3. var colors = [
  4. "#000000","#000033","#000066","#000099","#0000cc","#0000ff","#330000","#330033",
  5. "#330066","#330099","#3300cc","#3300ff","#660000","#660033","#660066","#660099",
  6. "#6600cc","#6600ff","#990000","#990033","#990066","#990099","#9900cc","#9900ff",
  7. "#cc0000","#cc0033","#cc0066","#cc0099","#cc00cc","#cc00ff","#ff0000","#ff0033",
  8. "#ff0066","#ff0099","#ff00cc","#ff00ff","#003300","#003333","#003366","#003399",
  9. "#0033cc","#0033ff","#333300","#333333","#333366","#333399","#3333cc","#3333ff",
  10. "#663300","#663333","#663366","#663399","#6633cc","#6633ff","#993300","#993333",
  11. "#993366","#993399","#9933cc","#9933ff","#cc3300","#cc3333","#cc3366","#cc3399",
  12. "#cc33cc","#cc33ff","#ff3300","#ff3333","#ff3366","#ff3399","#ff33cc","#ff33ff",
  13. "#006600","#006633","#006666","#006699","#0066cc","#0066ff","#336600","#336633",
  14. "#336666","#336699","#3366cc","#3366ff","#666600","#666633","#666666","#666699",
  15. "#6666cc","#6666ff","#996600","#996633","#996666","#996699","#9966cc","#9966ff",
  16. "#cc6600","#cc6633","#cc6666","#cc6699","#cc66cc","#cc66ff","#ff6600","#ff6633",
  17. "#ff6666","#ff6699","#ff66cc","#ff66ff","#009900","#009933","#009966","#009999",
  18. "#0099cc","#0099ff","#339900","#339933","#339966","#339999","#3399cc","#3399ff",
  19. "#669900","#669933","#669966","#669999","#6699cc","#6699ff","#999900","#999933",
  20. "#999966","#999999","#9999cc","#9999ff","#cc9900","#cc9933","#cc9966","#cc9999",
  21. "#cc99cc","#cc99ff","#ff9900","#ff9933","#ff9966","#ff9999","#ff99cc","#ff99ff",
  22. "#00cc00","#00cc33","#00cc66","#00cc99","#00cccc","#00ccff","#33cc00","#33cc33",
  23. "#33cc66","#33cc99","#33cccc","#33ccff","#66cc00","#66cc33","#66cc66","#66cc99",
  24. "#66cccc","#66ccff","#99cc00","#99cc33","#99cc66","#99cc99","#99cccc","#99ccff",
  25. "#cccc00","#cccc33","#cccc66","#cccc99","#cccccc","#ccccff","#ffcc00","#ffcc33",
  26. "#ffcc66","#ffcc99","#ffcccc","#ffccff","#00ff00","#00ff33","#00ff66","#00ff99",
  27. "#00ffcc","#00ffff","#33ff00","#33ff33","#33ff66","#33ff99","#33ffcc","#33ffff",
  28. "#66ff00","#66ff33","#66ff66","#66ff99","#66ffcc","#66ffff","#99ff00","#99ff33",
  29. "#99ff66","#99ff99","#99ffcc","#99ffff","#ccff00","#ccff33","#ccff66","#ccff99",
  30. "#ccffcc","#ccffff","#ffff00","#ffff33","#ffff66","#ffff99","#ffffcc","#ffffff"
  31. ];
  32. var named = {
  33. '#F0F8FF':'Alice Blue','#FAEBD7':'Antique White','#00FFFF':'Aqua','#7FFFD4':'Aquamarine','#F0FFFF':'Azure','#F5F5DC':'Beige',
  34. '#FFE4C4':'Bisque','#000000':'Black','#FFEBCD':'Blanched Almond','#0000FF':'Blue','#8A2BE2':'Blue Violet','#A52A2A':'Brown',
  35. '#DEB887':'Burly Wood','#5F9EA0':'Cadet Blue','#7FFF00':'Chartreuse','#D2691E':'Chocolate','#FF7F50':'Coral','#6495ED':'Cornflower Blue',
  36. '#FFF8DC':'Cornsilk','#DC143C':'Crimson','#00FFFF':'Cyan','#00008B':'Dark Blue','#008B8B':'Dark Cyan','#B8860B':'Dark Golden Rod',
  37. '#A9A9A9':'Dark Gray','#A9A9A9':'Dark Grey','#006400':'Dark Green','#BDB76B':'Dark Khaki','#8B008B':'Dark Magenta','#556B2F':'Dark Olive Green',
  38. '#FF8C00':'Darkorange','#9932CC':'Dark Orchid','#8B0000':'Dark Red','#E9967A':'Dark Salmon','#8FBC8F':'Dark Sea Green','#483D8B':'Dark Slate Blue',
  39. '#2F4F4F':'Dark Slate Gray','#2F4F4F':'Dark Slate Grey','#00CED1':'Dark Turquoise','#9400D3':'Dark Violet','#FF1493':'Deep Pink','#00BFFF':'Deep Sky Blue',
  40. '#696969':'Dim Gray','#696969':'Dim Grey','#1E90FF':'Dodger Blue','#B22222':'Fire Brick','#FFFAF0':'Floral White','#228B22':'Forest Green',
  41. '#FF00FF':'Fuchsia','#DCDCDC':'Gainsboro','#F8F8FF':'Ghost White','#FFD700':'Gold','#DAA520':'Golden Rod','#808080':'Gray','#808080':'Grey',
  42. '#008000':'Green','#ADFF2F':'Green Yellow','#F0FFF0':'Honey Dew','#FF69B4':'Hot Pink','#CD5C5C':'Indian Red','#4B0082':'Indigo','#FFFFF0':'Ivory',
  43. '#F0E68C':'Khaki','#E6E6FA':'Lavender','#FFF0F5':'Lavender Blush','#7CFC00':'Lawn Green','#FFFACD':'Lemon Chiffon','#ADD8E6':'Light Blue',
  44. '#F08080':'Light Coral','#E0FFFF':'Light Cyan','#FAFAD2':'Light Golden Rod Yellow','#D3D3D3':'Light Gray','#D3D3D3':'Light Grey','#90EE90':'Light Green',
  45. '#FFB6C1':'Light Pink','#FFA07A':'Light Salmon','#20B2AA':'Light Sea Green','#87CEFA':'Light Sky Blue','#778899':'Light Slate Gray','#778899':'Light Slate Grey',
  46. '#B0C4DE':'Light Steel Blue','#FFFFE0':'Light Yellow','#00FF00':'Lime','#32CD32':'Lime Green','#FAF0E6':'Linen','#FF00FF':'Magenta','#800000':'Maroon',
  47. '#66CDAA':'Medium Aqua Marine','#0000CD':'Medium Blue','#BA55D3':'Medium Orchid','#9370D8':'Medium Purple','#3CB371':'Medium Sea Green','#7B68EE':'Medium Slate Blue',
  48. '#00FA9A':'Medium Spring Green','#48D1CC':'Medium Turquoise','#C71585':'Medium Violet Red','#191970':'Midnight Blue','#F5FFFA':'Mint Cream','#FFE4E1':'Misty Rose','#FFE4B5':'Moccasin',
  49. '#FFDEAD':'Navajo White','#000080':'Navy','#FDF5E6':'Old Lace','#808000':'Olive','#6B8E23':'Olive Drab','#FFA500':'Orange','#FF4500':'Orange Red','#DA70D6':'Orchid',
  50. '#EEE8AA':'Pale Golden Rod','#98FB98':'Pale Green','#AFEEEE':'Pale Turquoise','#D87093':'Pale Violet Red','#FFEFD5':'Papaya Whip','#FFDAB9':'Peach Puff',
  51. '#CD853F':'Peru','#FFC0CB':'Pink','#DDA0DD':'Plum','#B0E0E6':'Powder Blue','#800080':'Purple','#FF0000':'Red','#BC8F8F':'Rosy Brown','#4169E1':'Royal Blue',
  52. '#8B4513':'Saddle Brown','#FA8072':'Salmon','#F4A460':'Sandy Brown','#2E8B57':'Sea Green','#FFF5EE':'Sea Shell','#A0522D':'Sienna','#C0C0C0':'Silver',
  53. '#87CEEB':'Sky Blue','#6A5ACD':'Slate Blue','#708090':'Slate Gray','#708090':'Slate Grey','#FFFAFA':'Snow','#00FF7F':'Spring Green',
  54. '#4682B4':'Steel Blue','#D2B48C':'Tan','#008080':'Teal','#D8BFD8':'Thistle','#FF6347':'Tomato','#40E0D0':'Turquoise','#EE82EE':'Violet',
  55. '#F5DEB3':'Wheat','#FFFFFF':'White','#F5F5F5':'White Smoke','#FFFF00':'Yellow','#9ACD32':'Yellow Green'
  56. };
  57. var namedLookup = {};
  58. function init() {
  59. var inputColor = convertRGBToHex(tinyMCEPopup.getWindowArg('input_color')), key, value;
  60. tinyMCEPopup.resizeToInnerSize();
  61. generatePicker();
  62. generateWebColors();
  63. generateNamedColors();
  64. if (inputColor) {
  65. changeFinalColor(inputColor);
  66. col = convertHexToRGB(inputColor);
  67. if (col)
  68. updateLight(col.r, col.g, col.b);
  69. }
  70. for (key in named) {
  71. value = named[key];
  72. namedLookup[value.replace(/\s+/, '').toLowerCase()] = key.replace(/#/, '').toLowerCase();
  73. }
  74. }
  75. function toHexColor(color) {
  76. var matches, red, green, blue, toInt = parseInt;
  77. function hex(value) {
  78. value = parseInt(value).toString(16);
  79. return value.length > 1 ? value : '0' + value; // Padd with leading zero
  80. };
  81. color = tinymce.trim(color);
  82. color = color.replace(/^[#]/, '').toLowerCase(); // remove leading '#'
  83. color = namedLookup[color] || color;
  84. matches = /^rgb\((\d{1,3}),(\d{1,3}),(\d{1,3})\)$/.exec(color);
  85. if (matches) {
  86. red = toInt(matches[1]);
  87. green = toInt(matches[2]);
  88. blue = toInt(matches[3]);
  89. } else {
  90. matches = /^([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/.exec(color);
  91. if (matches) {
  92. red = toInt(matches[1], 16);
  93. green = toInt(matches[2], 16);
  94. blue = toInt(matches[3], 16);
  95. } else {
  96. matches = /^([0-9a-f])([0-9a-f])([0-9a-f])$/.exec(color);
  97. if (matches) {
  98. red = toInt(matches[1] + matches[1], 16);
  99. green = toInt(matches[2] + matches[2], 16);
  100. blue = toInt(matches[3] + matches[3], 16);
  101. } else {
  102. return '';
  103. }
  104. }
  105. }
  106. return '#' + hex(red) + hex(green) + hex(blue);
  107. }
  108. function insertAction() {
  109. var color = document.getElementById("color").value, f = tinyMCEPopup.getWindowArg('func');
  110. var hexColor = toHexColor(color);
  111. if (hexColor === '') {
  112. var text = tinyMCEPopup.editor.getLang('advanced_dlg.invalid_color_value');
  113. tinyMCEPopup.alert(text + ': ' + color);
  114. }
  115. else {
  116. tinyMCEPopup.restoreSelection();
  117. if (f)
  118. f(hexColor);
  119. tinyMCEPopup.close();
  120. }
  121. }
  122. function showColor(color, name) {
  123. if (name)
  124. document.getElementById("colorname").innerHTML = name;
  125. document.getElementById("preview").style.backgroundColor = color;
  126. document.getElementById("color").value = color.toUpperCase();
  127. }
  128. function convertRGBToHex(col) {
  129. var re = new RegExp("rgb\\s*\\(\\s*([0-9]+).*,\\s*([0-9]+).*,\\s*([0-9]+).*\\)", "gi");
  130. if (!col)
  131. return col;
  132. var rgb = col.replace(re, "$1,$2,$3").split(',');
  133. if (rgb.length == 3) {
  134. r = parseInt(rgb[0]).toString(16);
  135. g = parseInt(rgb[1]).toString(16);
  136. b = parseInt(rgb[2]).toString(16);
  137. r = r.length == 1 ? '0' + r : r;
  138. g = g.length == 1 ? '0' + g : g;
  139. b = b.length == 1 ? '0' + b : b;
  140. return "#" + r + g + b;
  141. }
  142. return col;
  143. }
  144. function convertHexToRGB(col) {
  145. if (col.indexOf('#') != -1) {
  146. col = col.replace(new RegExp('[^0-9A-F]', 'gi'), '');
  147. r = parseInt(col.substring(0, 2), 16);
  148. g = parseInt(col.substring(2, 4), 16);
  149. b = parseInt(col.substring(4, 6), 16);
  150. return {r : r, g : g, b : b};
  151. }
  152. return null;
  153. }
  154. function generatePicker() {
  155. var el = document.getElementById('light'), h = '', i;
  156. for (i = 0; i < detail; i++){
  157. h += '<div id="gs'+i+'" style="background-color:#000000; width:15px; height:3px; border-style:none; border-width:0px;"'
  158. + ' onclick="changeFinalColor(this.style.backgroundColor)"'
  159. + ' onmousedown="isMouseDown = true; return false;"'
  160. + ' onmouseup="isMouseDown = false;"'
  161. + ' onmousemove="if (isMouseDown && isMouseOver) changeFinalColor(this.style.backgroundColor); return false;"'
  162. + ' onmouseover="isMouseOver = true;"'
  163. + ' onmouseout="isMouseOver = false;"'
  164. + '></div>';
  165. }
  166. el.innerHTML = h;
  167. }
  168. function generateWebColors() {
  169. var el = document.getElementById('webcolors'), h = '', i;
  170. if (el.className == 'generated')
  171. return;
  172. // TODO: VoiceOver doesn't seem to support legend as a label referenced by labelledby.
  173. h += '<div role="listbox" aria-labelledby="webcolors_title" tabindex="0"><table role="presentation" border="0" cellspacing="1" cellpadding="0">'
  174. + '<tr>';
  175. for (i=0; i<colors.length; i++) {
  176. h += '<td bgcolor="' + colors[i] + '" width="10" height="10">'
  177. + '<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;">';
  178. if (tinyMCEPopup.editor.forcedHighContrastMode) {
  179. h += '<canvas class="mceColorSwatch" height="10" width="10" data-color="' + colors[i] + '"></canvas>';
  180. }
  181. h += '<span class="mceVoiceLabel" style="display:none;" id="web_colors_' + i + '">' + colors[i].toUpperCase() + '</span>';
  182. h += '</a></td>';
  183. if ((i+1) % 18 == 0)
  184. h += '</tr><tr>';
  185. }
  186. h += '</table></div>';
  187. el.innerHTML = h;
  188. el.className = 'generated';
  189. paintCanvas(el);
  190. enableKeyboardNavigation(el.firstChild);
  191. }
  192. function paintCanvas(el) {
  193. tinyMCEPopup.getWin().tinymce.each(tinyMCEPopup.dom.select('canvas.mceColorSwatch', el), function(canvas) {
  194. var context;
  195. if (canvas.getContext && (context = canvas.getContext("2d"))) {
  196. context.fillStyle = canvas.getAttribute('data-color');
  197. context.fillRect(0, 0, 10, 10);
  198. }
  199. });
  200. }
  201. function generateNamedColors() {
  202. var el = document.getElementById('namedcolors'), h = '', n, v, i = 0;
  203. if (el.className == 'generated')
  204. return;
  205. for (n in named) {
  206. v = named[n];
  207. 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 + '">';
  208. if (tinyMCEPopup.editor.forcedHighContrastMode) {
  209. h += '<canvas class="mceColorSwatch" height="10" width="10" data-color="' + colors[i] + '"></canvas>';
  210. }
  211. h += '<span class="mceVoiceLabel" style="display:none;" id="named_colors_' + i + '">' + v + '</span>';
  212. h += '</a>';
  213. i++;
  214. }
  215. el.innerHTML = h;
  216. el.className = 'generated';
  217. paintCanvas(el);
  218. enableKeyboardNavigation(el);
  219. }
  220. function enableKeyboardNavigation(el) {
  221. tinyMCEPopup.editor.windowManager.createInstance('tinymce.ui.KeyboardNavigation', {
  222. root: el,
  223. items: tinyMCEPopup.dom.select('a', el)
  224. }, tinyMCEPopup.dom);
  225. }
  226. function dechex(n) {
  227. return strhex.charAt(Math.floor(n / 16)) + strhex.charAt(n % 16);
  228. }
  229. function computeColor(e) {
  230. var x, y, partWidth, partDetail, imHeight, r, g, b, coef, i, finalCoef, finalR, finalG, finalB, pos = tinyMCEPopup.dom.getPos(e.target);
  231. x = e.offsetX ? e.offsetX : (e.target ? e.clientX - pos.x : 0);
  232. y = e.offsetY ? e.offsetY : (e.target ? e.clientY - pos.y : 0);
  233. partWidth = document.getElementById('colors').width / 6;
  234. partDetail = detail / 2;
  235. imHeight = document.getElementById('colors').height;
  236. r = (x >= 0)*(x < partWidth)*255 + (x >= partWidth)*(x < 2*partWidth)*(2*255 - x * 255 / partWidth) + (x >= 4*partWidth)*(x < 5*partWidth)*(-4*255 + x * 255 / partWidth) + (x >= 5*partWidth)*(x < 6*partWidth)*255;
  237. g = (x >= 0)*(x < partWidth)*(x * 255 / partWidth) + (x >= partWidth)*(x < 3*partWidth)*255 + (x >= 3*partWidth)*(x < 4*partWidth)*(4*255 - x * 255 / partWidth);
  238. b = (x >= 2*partWidth)*(x < 3*partWidth)*(-2*255 + x * 255 / partWidth) + (x >= 3*partWidth)*(x < 5*partWidth)*255 + (x >= 5*partWidth)*(x < 6*partWidth)*(6*255 - x * 255 / partWidth);
  239. coef = (imHeight - y) / imHeight;
  240. r = 128 + (r - 128) * coef;
  241. g = 128 + (g - 128) * coef;
  242. b = 128 + (b - 128) * coef;
  243. changeFinalColor('#' + dechex(r) + dechex(g) + dechex(b));
  244. updateLight(r, g, b);
  245. }
  246. function updateLight(r, g, b) {
  247. var i, partDetail = detail / 2, finalCoef, finalR, finalG, finalB, color;
  248. for (i=0; i<detail; i++) {
  249. if ((i>=0) && (i<partDetail)) {
  250. finalCoef = i / partDetail;
  251. finalR = dechex(255 - (255 - r) * finalCoef);
  252. finalG = dechex(255 - (255 - g) * finalCoef);
  253. finalB = dechex(255 - (255 - b) * finalCoef);
  254. } else {
  255. finalCoef = 2 - i / partDetail;
  256. finalR = dechex(r * finalCoef);
  257. finalG = dechex(g * finalCoef);
  258. finalB = dechex(b * finalCoef);
  259. }
  260. color = finalR + finalG + finalB;
  261. setCol('gs' + i, '#'+color);
  262. }
  263. }
  264. function changeFinalColor(color) {
  265. if (color.indexOf('#') == -1)
  266. color = convertRGBToHex(color);
  267. setCol('preview', color);
  268. document.getElementById('color').value = color;
  269. }
  270. function setCol(e, c) {
  271. try {
  272. document.getElementById(e).style.backgroundColor = c;
  273. } catch (ex) {
  274. // Ignore IE warning
  275. }
  276. }
  277. tinyMCEPopup.onInit.add(init);