colorpicker.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. //颜色空间 陈裕豪 赖杰辉
  2. Namespace.register("U.Control.Colorpicker.calen"); //颜色拾取PC命名空间
  3. //返回参数 :divs
  4. //调用函数 :U.Control.Colorpicker.calen.A
  5. U.Control.Colorpicker.calen.A = function () { //创建div 等相关内容
  6. var yuanshi = $('<div id="U_Colorpicker_calen_yuanshi"></div>')[0];
  7. var divs = $$('div', { 'class': 'U_Colorpicker_calen' }, yuanshi);
  8. var baowei = $$('div', { 'class': 'U_Colorpicker-rgb-dadivs-baowei' }, divs);
  9. var canvas = $$('canvas', { 'class': 'U_Colorpicker-canvass', 'width': '250', 'height': '250' }, baowei);
  10. var canvass = $$('canvas', { 'class': 'U_Colorpicker-canvass-two', 'width': '250', 'height': '250', 'style': { 'display': 'none'} }, baowei);
  11. var yuanquan = $$('div', { 'class': 'U_Colorpicker-rgb-dadivs-yuanquan' }, baowei);
  12. var changtiaokuang = $$('div', { 'class': 'U_Colorpicker-changtiao-divss-kuang' }, divs);
  13. var changtiao = $$('div', { 'class': 'U_Colorpicker-changtiao-divss' }, changtiaokuang);
  14. var changtiaocanvas = $$('canvas', { 'id': 'U_Colorpicker-changtiaocanvas', 'width': '5', 'height': '250', 'style': { 'display': 'none'} }, changtiaokuang);
  15. var sjx = $$('div', { 'class': 'U_Colorpicker-changtiao-divss-sjx' }, changtiaokuang);
  16. var block = $$('div', { 'class': 'U_Colorpicker-divss' }, divs); //创建加载点击颜色的时候 方块显示的颜色
  17. var divrgb = $$('div', { 'class': 'U_Colorpicker-rgb-dadivs' }, divs);
  18. var divrgsb = $$('div', { 'class': 'U_Colorpicker-rgbkk-dadivs' }, divrgb);
  19. var daq = $$('div', { 'class': 'U_Colorpicker-rgbkk-dadivs_1', 'innerHTML': 'R:' }, divrgsb);
  20. var input = $$('input', { 'class': 'U_Colorpicker-input', 'id': 'U_Colorpicker_1', 'value': '248' }, divrgsb);
  21. var divrgsb = $$('div', { 'class': 'U_Colorpicker-rgbkk-dadivs' }, divrgb);
  22. var daq = $$('div', { 'class': 'U_Colorpicker-rgbkk-dadivs_1', 'innerHTML': 'G:' }, divrgsb);
  23. var input = $$('input', { 'class': 'U_Colorpicker-input', 'id': 'U_Colorpicker_2', 'value': '0' }, divrgsb);
  24. var divrgsb = $$('div', { 'class': 'U_Colorpicker-rgbkk-dadivs' }, divrgb);
  25. var daq = $$('div', { 'class': 'U_Colorpicker-rgbkk-dadivs_1', 'innerHTML': 'B:' }, divrgsb);
  26. var input = $$('input', { 'class': 'U_Colorpicker-input', 'id': 'U_Colorpicker_3', 'value': '42' }, divrgsb);
  27. var divrgsb = $$('div', { 'class': 'U_Colorpicker-rgbkk-dadivs-1' }, divrgb);
  28. var daq = $$('div', { 'class': 'U_Colorpicker-rgbkk-dadivs_1', 'innerHTML': '#' }, divrgsb);
  29. var input = $$('input', { 'class': 'U_Colorpicker-input-2', 'id': 'U_Colorpicker_4', 'value': 'F8002A' }, divrgsb);
  30. var divssp = $$('div', { 'class': 'U_Colorpicker-s-dadivs' }, divs);
  31. var ctxs = canvass.getContext("2d");
  32. var imgs = new Image();
  33. var globalhexcode = '';
  34. imgs.src = "images/hb.png";
  35. imgs.id = "bbbb";
  36. imgs.onload = function () //确保图片已经加载完毕
  37. {
  38. ctxs.drawImage(imgs, 0, 0, 250, 250); // drawImage canvas放置图片参数
  39. }
  40. var ctx = canvas.getContext("2d"); // 创建canvas
  41. /*var img2 = new Image();
  42. img2.id = "aaaa";
  43. img2.src = "images/hb.png";
  44. img2.onload = function () //确保图片已经加载完毕
  45. {
  46. ctx.drawImage(img2,0, 0, 250, 250); // drawImage canvas放置图片参数
  47. }*/
  48. var img = new Image();
  49. img.src = "images/tu.png";
  50. img.id = "bbbb";
  51. img.onload = function () //确保图片已经加载完毕
  52. {
  53. ctx.drawImage(img, 0, 0, 250, 250); // drawImage canvas放置图片参数
  54. }
  55. baowei.onmousedown = function (e) { //获取canvas里面图片点击当前位置的像素数据
  56. GG(e);
  57. }
  58. baowei.onmousemove = function (e) { //获取canvas里面图片点击当前位置的像素数据
  59. GG(e);
  60. }
  61. changtiaokuang.onmousedown = function (e) { HH(e); }
  62. changtiaokuang.onmousemove = function (e) { HH(e); }
  63. var colord = ["000000", "FFFFFF", "FF0000", "00FF00", "0000FF", "FFFF00", "00FFFF", "FF00FF"]; //添加颜色数组
  64. var dd;
  65. for (x = 0; x < 8; x++) {
  66. dd = $$("div", { "style": { "cssText": "float:left;width:15px;height:15px;margin-right:4px;cursor:pointer;" }, "id": x }, divssp); //FOR循环颜色数组
  67. dd.style.background = "#" + colord[x];
  68. dd.onclick = function () { colorpicker_div(this.id) } //当这个数组点击的时候
  69. }
  70. colorpicker_div = function (a) { //左边的小正方形点击的时候执行的函数
  71. $("#U_Colorpicker_4")[0].value = colord[a]; //让input显示值
  72. block.style.background = "#" + colord[a]; //让div显示背景颜色
  73. b = new Array();
  74. for (x = 0; x < 3; x++) { //for循环 数组 吧这个变成color值函数
  75. b[0] = colord[a].substr(x * 2, 2)
  76. b[3] = "0123456789abcdef";
  77. b[1] = b[0].substr(0, 1)
  78. b[2] = b[0].substr(1, 1)
  79. b[20 + x] = b[3].indexOf(b[1]) * 16 + b[3].indexOf(b[2])
  80. }
  81. //$("#bb")[0].value = "rgb(" + b[20] + "," + b[21] + "," + b[22] + ")"; //让div显示color值
  82. $("#U_Colorpicker_1")[0].value = b[20]; //让input 显示颜色值
  83. $("#U_Colorpicker_2")[0].value = b[21]; //让input 显示颜色值
  84. $("#U_Colorpicker_3")[0].value = b[22]; //让input 显示颜色值
  85. }
  86. HH = function (e) {
  87. if (event.buttons == 1) {
  88. var yy = event.clientY - 6;
  89. if (yy < 6 || yy > 253) {
  90. return;
  91. }
  92. var a = $('#U_Colorpicker-changtiaocanvas')[0].getContext("2d");
  93. a.clearRect(0, 0, 5, 250);
  94. var gradient = a.createLinearGradient(0, 0, 0, 250);
  95. gradient.addColorStop(0, '#' + globalhexcode);
  96. gradient.addColorStop(1, '#fff');
  97. a.fillStyle = gradient;
  98. a.rect(0, 0, 5, 250);
  99. a.fill();
  100. var height = $('.U_Colorpicker-changtiao-divss-sjx')[0].style.top.slice(0, length - 2);
  101. if (height == 0) {
  102. height = 1;
  103. var img = a.getImageData(0, height - 1, 1, height).data;
  104. } else {
  105. var img = a.getImageData(0, height - 1, 1, height).data;
  106. }
  107. $("#U_Colorpicker_1")[0].value = img[0];
  108. $("#U_Colorpicker_2")[0].value = img[1];
  109. $("#U_Colorpicker_3")[0].value = img[2];
  110. hexcode = '';
  111. for (var x = 0; x < 3; x++) {
  112. nn = img[x];
  113. var cc = "0123456789ABCDEF",
  114. bb = "",
  115. aa = nn % 16;
  116. bb = cc.substr(aa, 1);
  117. aa = (nn - a) / 16;
  118. hexcode += cc.substr(aa, 1) + bb;
  119. }
  120. $("#U_Colorpicker_4")[0].value = hexcode;
  121. $(".U_Colorpicker-divss")[0].style.cssText = "background-color: rgb(" + img[0] + ", " + img[1] + ", " + img[2] + ");";
  122. //console.log(img[0] + ' ' + img[1] + ' ' + img[2]);
  123. var bfb = e.offsetY / 250;
  124. $(".U_Colorpicker-canvass-two")[0].style.opacity = bfb;
  125. sjx.style.cssText += "top:" + yy + "px";
  126. }
  127. }
  128. GG = function (e) {
  129. if (event.buttons == 1) {
  130. var xx = event.clientX - 6;
  131. var yy = event.clientY - 6;
  132. if (event.clientX > 252 || event.clientY > 252 || event.clientX < 10 || event.clientY < 10) {
  133. return;
  134. }
  135. yuanquan.style.cssText += "left:" + xx + "px;top:" + yy + "px";
  136. // yuanquan.style.cssText += "left:" + event.clientX + "px;top:" + event.clientY + "px";
  137. var canvasOffset = ctx.canvas; //canvasOffset 获取canvas
  138. var x = Math.floor(e.pageX - canvasOffset.offsetLeft); //获取当前点击的时候的在X上的位置
  139. var y = Math.floor(e.pageY - canvasOffset.offsetTop); //获取当前点击的时候的在y上的位置
  140. var imgData = ctx.getImageData(x, y, 1, 1); //imgData 获取图片的像素信息
  141. var r = imgData.data[0]; var g = imgData.data[1]; var b = imgData.data[2]; //r是rbg第一个参数 g第二个参数 b第三个参数
  142. var color = [r, g, b]; //加入数组
  143. var hexcode = "";
  144. block.style.backgroundColor = "rgb(" + r + "," + g + "," + b + ")"; //让div显示颜色
  145. //$("#bb")[0].value = "rgb(" + r + "," + g + "," + b + ")"; //让input 显示颜色值
  146. $("#U_Colorpicker_1")[0].value = r; //让input 显示颜色值
  147. $("#U_Colorpicker_2")[0].value = g; //让input 显示颜色值
  148. $("#U_Colorpicker_3")[0].value = b; //让input 显示颜色值
  149. var n;
  150. var c;
  151. for (x = 0; x < 3; x++) { //for循环color
  152. n = color[x];
  153. c = "0123456789ABCDEF", b = "", a = n % 16;
  154. b = c.substr(a, 1); a = (n - a) / 16;
  155. hexcode += c.substr(a, 1) + b
  156. }
  157. globalhexcode = hexcode;
  158. $("#U_Colorpicker_4")[0].value = hexcode;
  159. var hexcodes = "#" + hexcode
  160. $(".U_Colorpicker-changtiao-divss")[0].style.background = " -webkit-linear-gradient(top, " + hexcodes + " , white)";
  161. }
  162. }
  163. return yuanshi; //返回div参数
  164. }