colorpicker.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. (function () {
  2. var util = {
  3. css: function (elem, obj) {
  4. for (var i in obj) {
  5. elem.style[i] = obj[i];
  6. }
  7. },
  8. hasClass: function (elem, classN) {
  9. var className = elem.getAttribute("class");
  10. return className.indexOf(classN) != -1;
  11. }
  12. };
  13. function Colorpicker(opt) {
  14. if (this === window) throw `Colorpicker: Can't call a function directly`;
  15. this.init(opt);
  16. };
  17. Colorpicker.prototype = {
  18. init(opt) {
  19. let { el, initColor = "rgb(255,0,0)", allMode = ['hex', 'rgb'], color = '' } = opt;
  20. var elem = document.getElementById(el);
  21. if (!(elem && elem.nodeType && elem.nodeType === 1)) {
  22. throw `Colorpicker: not found ID:${el} HTMLElement,not ${{}.toString.call(el)}`;
  23. }
  24. this.Opt = {
  25. ...opt,
  26. el,
  27. initColor,
  28. allMode,
  29. color
  30. }
  31. this.bindElem = elem; // 缁戝畾鐨勫厓绱�
  32. this.elem_wrap = null; // 鏈€澶栧眰瀹瑰櫒
  33. this.fixedBg = null; // 鎷捐壊鍣ㄥ悗闈㈠浐瀹氬畾浣嶇殑閫忔槑div 鐢ㄤ簬鐐瑰嚮闅愯棌鎷捐壊鍣�
  34. this.elem_colorPancel = null; // 鑹插僵闈㈡澘
  35. this.elem_picker = null; // 鎷捐壊鍣ㄨ壊鍧楁寜閽�
  36. this.elem_barPicker1 = null; // 棰滆壊鏉�
  37. this.elem_hexInput = null; // 鏄剧ずhex鐨勮〃鍗�
  38. this.elem_showColor = null; // 鏄剧ず褰撳墠棰滆壊
  39. this.elem_showModeBtn = null; // 鍒囨崲杈撳叆妗嗘ā寮忔寜閽�
  40. this.elem_inputWrap = null; // 杈撳叆妗嗗灞傚鍣�
  41. this.pancelLeft = 0;
  42. this.pancelTop = 0;
  43. this.downX = 0;
  44. this.downY = 0;
  45. this.moveX = 0;
  46. this.moveY = 0;
  47. this.pointLeft = 0;
  48. this.pointTop = 0;
  49. this.current_mode = 'hex'; // input妗嗗綋鍓嶇殑妯″紡
  50. this.rgba = { r: 0, g: 0, b: 0, a: 1 };
  51. this.hsb = { h: 0, s: 100, b: 100 };
  52. var _this = this, rgb = initColor.slice(4, -1).split(",");
  53. this.rgba.r = parseInt(rgb[0]);
  54. this.rgba.g = parseInt(rgb[1]);
  55. this.rgba.b = parseInt(rgb[2]);
  56. var body = document.getElementsByTagName("body")[0],
  57. div = document.createElement("div");
  58. div.innerHTML = this.render();
  59. body.appendChild(div);
  60. this.elem_wrap = div;
  61. this.fixedBg = div.children[0];
  62. this.elem_colorPancel = div.getElementsByClassName("color-pancel")[0];
  63. this.pancel_width = this.elem_colorPancel.offsetWidth;
  64. this.pancel_height = this.elem_colorPancel.offsetHeight;
  65. this.elem_picker = div.getElementsByClassName("pickerBtn")[0];
  66. this.elem_colorPalette = div.getElementsByClassName("color-palette")[0];
  67. this.elem_showColor = div.getElementsByClassName("colorpicker-showColor")[0];
  68. this.elem_barPicker1 = div.getElementsByClassName("colorBar-color-picker")[0];
  69. /* this.elem_barPicker2 = div.getElementsByClassName("colorBar-opacity-picker")[0]; */
  70. this.elem_hexInput = div.getElementsByClassName("colorpicker-hexInput")[0];
  71. this.elem_showModeBtn = div.getElementsByClassName("colorpicker-showModeBtn")[0];
  72. this.elem_inputWrap = div.getElementsByClassName("colorpicker-inputWrap")[0];
  73. /* this.elem_opacityPancel = this.elem_barPicker2.parentNode.parentNode.children[1]; */
  74. // var rect = this.bindElem.getBoundingClientRect();
  75. var elem = this.bindElem;
  76. var top = elem.offsetTop;
  77. var left = elem.offsetLeft;
  78. while (elem.offsetParent) {
  79. top += elem.offsetParent.offsetTop;
  80. left += elem.offsetParent.offsetLeft;
  81. elem = elem.offsetParent;
  82. }
  83. this.pancelLeft = left + this.elem_colorPalette.clientWidth;
  84. this.pancelTop = top + this.bindElem.offsetHeight;
  85. util.css(div, {
  86. "position": "absolute",
  87. "z-index": 999999999999999,
  88. "display": 'none',
  89. "left": left + "px",
  90. "top": top + this.bindElem.offsetHeight + "px"
  91. });
  92. this.bindMove(this.elem_colorPancel, this.setPosition, true);
  93. this.bindMove(this.elem_barPicker1.parentNode, this.setBar, false);
  94. /* this.bindMove(this.elem_barPicker2.parentNode,this.setBar,false); */
  95. this.bindElem.addEventListener("click", function () {
  96. _this.show();
  97. }, false);
  98. this.fixedBg.addEventListener("click", function (e) {
  99. _this.hide();
  100. }, false)
  101. this.elem_showModeBtn.addEventListener("click", function () {
  102. _this.switch_current_mode();
  103. }, false)
  104. this.elem_wrap.addEventListener("input", function (e) {
  105. var target = e.target, value = target.value;
  106. _this.setColorByInput(value);
  107. }, false);
  108. this.elem_colorPalette.addEventListener("click", function (e) {
  109. if (e.target.tagName.toLocaleLowerCase() == "p") {
  110. let colorStr = e.target.style.background;
  111. let rgb = colorStr.slice(4, -1).split(",");
  112. let rgba = {
  113. r: parseInt(rgb[0]),
  114. g: parseInt(rgb[1]),
  115. b: parseInt(rgb[2])
  116. }
  117. switch (_this.current_mode) {
  118. case "hex":
  119. _this.setColorByInput("#" + _this.rgbToHex(rgba))
  120. break;
  121. case 'rgb':
  122. let inputs = _this.elem_wrap.getElementsByTagName("input")
  123. inputs[0].value = rgba.r;
  124. inputs[1].value = rgba.g;
  125. inputs[2].value = rgba.b;
  126. _this.setColorByInput(colorStr)
  127. /* _this.hsb = _this.rgbToHsb(rgba); */
  128. break;
  129. }
  130. }
  131. }, false);
  132. (color != '' && this.setColorByInput(color));
  133. },
  134. render: function () {
  135. var tpl =
  136. `<div style="position: fixed; top: 0px; right: 0px; bottom: 0px; left: 0px;"></div>
  137. <div style="position: inherit;z-index: 99999999999999999;display: flex;box-shadow: rgba(0, 0, 0, 0.3) 0px 0px 2px, rgba(0, 0, 0, 0.3) 0px 4px 8px;">
  138. <div style='width:180px;padding:10px;background: #f9f9f9;display: flex;flex-flow: row wrap;align-content: space-around;justify-content: space-around;' class='color-palette'>
  139. ${this.getPaletteColorsItem()}
  140. </div>
  141. <div class="colorpicker-pancel" style="background: rgb(255, 255, 255);box-sizing: initial; width: 225px; font-family: Menlo;">
  142. <div style="width: 100%; padding-bottom: 55%; position: relative; border-radius: 2px 2px 0px 0px; overflow: hidden;">
  143. <div class="color-pancel" style="position: absolute; top: 0px; right: 0px; bottom: 0px; left: 0px; background: rgb(${this.rgba.r},${this.rgba.g},${this.rgba.b})">
  144. <style>
  145. .saturation-white {background: -webkit-linear-gradient(to right, #fff, rgba(255,255,255,0));background: linear-gradient(to right, #fff, rgba(255,255,255,0));}
  146. .saturation-black {background: -webkit-linear-gradient(to top, #000, rgba(0,0,0,0));background: linear-gradient(to top, #000, rgba(0,0,0,0));}
  147. </style>
  148. <div class="saturation-white" style="position: absolute; top: 0px; right: 0px; bottom: 0px; left: 0px;">
  149. <div class="saturation-black" style="position: absolute; top: 0px; right: 0px; bottom: 0px; left: 0px;">
  150. </div>
  151. <div class="pickerBtn" style="position: absolute; top: 0%; left: 100%; cursor: default;">
  152. <div style="width: 12px; height: 12px; border-radius: 6px; box-shadow: rgb(255, 255, 255) 0px 0px 0px 1px inset; transform: translate(-6px, -6px);">
  153. </div>
  154. </div>
  155. </div>
  156. </div>
  157. </div>
  158. <div style="padding: 0 16px 20px;">
  159. <div class="flexbox-fix" style="display: flex;align-items: center;height: 40px;">
  160. <div style="width: 32px;">
  161. <div style="width: 16px; height: 16px; border-radius: 8px; position: relative; overflow: hidden;">
  162. <div class="colorpicker-showColor" style="position: absolute; top: 0px; right: 0px; bottom: 0px; left: 0px; border-radius: 8px; box-shadow: rgba(0, 0, 0, 0.1) 0px 0px 0px 1px inset; background:rgb(${this.rgba.r},${this.rgba.g},${this.rgba.b}); z-index: 999999999999999;"></div>
  163. <div class="" style="position: absolute; top: 0px; right: 0px; bottom: 0px; left: 0px; background: url(&quot;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAMUlEQVQ4T2NkYGAQYcAP3uCTZhw1gGGYhAGBZIA/nYDCgBDAm9BGDWAAJyRCgLaBCAAgXwixzAS0pgAAAABJRU5ErkJggg==&quot;) left center;"></div>
  164. </div>
  165. </div>
  166. <div style="-webkit-box-flex: 1; flex: 1 1 0%;"><div style="height: 10px; position: relative;">
  167. <div style="position: absolute; top: 0px;right: 0px; bottom: 0px; left: 0px;">
  168. <div class="hue-horizontal" style="padding: 0px 2px; position: relative; height: 100%;">
  169. <style>
  170. .hue-horizontal {background: linear-gradient(to right, #f00 0%, #ff0 17%, #0f0 33%, #0ff 50%, #00f 67%, #f0f 83%, #f00 100%);background: -webkit-linear-gradient(to right, #f00 0%, #ff0 17%, #0f0 33%, #0ff 50%, #00f 67%, #f0f 83%, #f00 100%);}
  171. .hue-vertical {background: linear-gradient(to top, #f00 0%, #ff0 17%, #0f0 33%,#0ff 50%, #00f 67%, #f0f 83%, #f00 100%);background: -webkit-linear-gradient(to top, #f00 0%, #ff0 17%,#0f0 33%, #0ff 50%, #00f 67%, #f0f 83%, #f00 100%);}
  172. </style>
  173. <div class="colorBar-color-picker" style="position: absolute; left: 0%;">
  174. <div style="width: 12px; height: 12px; border-radius: 6px; transform: translate(-6px, -1px); background-color: rgb(248, 248, 248); box-shadow: rgba(0, 0, 0, 0.37) 0px 1px 4px 0px;">
  175. </div>
  176. </div>
  177. </div>
  178. </div>
  179. </div>
  180. </div>
  181. </div>
  182. <div class="flexbox-fix" style="display: flex;">
  183. <div class="flexbox-fix colorpicker-inputWrap" style="-webkit-box-flex: 1; flex: 1 1 0%; display: flex; margin-left: -6px;">
  184. ${this.getInputTpl()}
  185. </div>
  186. <div class="colorpicker-showModeBtn" style="width: 32px; text-align: right; position: relative;">
  187. <div style="margin-right: -4px; cursor: pointer; position: relative;">
  188. <svg viewBox="0 0 24 24" style="width: 24px; height: 24px; border: 1px solid transparent; border-radius: 5px;"><path fill="#333" d="M12,5.83L15.17,9L16.58,7.59L12,3L7.41,7.59L8.83,9L12,5.83Z"></path><path fill="#333" d="M12,18.17L8.83,15L7.42,16.41L12,21L16.59,16.41L15.17,15Z"></path></svg>
  189. </div>
  190. </div>
  191. </div>
  192. </div>
  193. </div>
  194. </div>`;
  195. return tpl;
  196. },
  197. getInputTpl: function () {
  198. var current_mode_html = "";
  199. switch (this.current_mode) {
  200. case 'hex':
  201. var hex = "#" + this.rgbToHex(this.HSBToRGB(this.hsb));
  202. current_mode_html += `
  203. <div style="padding-left: 6px; width: 100%;">
  204. <div style="position: relative;">
  205. <input class="colorpicker-hexInput" value="${hex}" spellcheck="false" style="font-size: 11px; color: rgb(51, 51, 51); width: 100%; border-radius: 2px; border: none; box-shadow: rgb(218, 218, 218) 0px 0px 0px 1px inset; height: 21px; text-align: center;">
  206. <span style="text-transform: uppercase; font-size: 11px; line-height: 11px; color: rgb(150, 150, 150); text-align: center; display: block; margin-top: 12px;">hex</span>
  207. </div>
  208. </div>`;
  209. break;
  210. case 'rgb':
  211. for (var i = 0; i < 3; i++) {
  212. current_mode_html +=
  213. `<div style="padding-left: 6px; width: 100%;">
  214. <div style="position: relative;">
  215. <input class="colorpicker-hexInput" value="${this.rgba['rgb'[i]]}" spellcheck="false" style="font-size: 11px; color: rgb(51, 51, 51); width: 100%; border-radius: 2px; border: none; box-shadow: rgb(218, 218, 218) 0px 0px 0px 1px inset; height: 21px; text-align: center;">
  216. <span style="text-transform: uppercase; font-size: 11px; line-height: 11px; color: rgb(150, 150, 150); text-align: center; display: block; margin-top: 12px;">${'rgb'[i]}</span>
  217. </div>
  218. </div>`;
  219. }
  220. default:
  221. }
  222. return current_mode_html;
  223. },
  224. getPaletteColorsItem: function () {
  225. let str = '';
  226. let palette = ["rgb(0, 0, 0)", "rgb(67, 67, 67)", "rgb(102, 102, 102)", "rgb(204, 204, 204)", "rgb(217, 217, 217)", "rgb(255, 255, 255)",
  227. "rgb(152, 0, 0)", "rgb(255, 0, 0)", "rgb(255, 153, 0)", "rgb(255, 255, 0)", "rgb(0, 255, 0)", "rgb(0, 255, 255)",
  228. "rgb(74, 134, 232)", "rgb(0, 0, 255)", "rgb(153, 0, 255)", "rgb(255, 0, 255)", "rgb(230, 184, 175)", "rgb(244, 204, 204)",
  229. "rgb(252, 229, 205)", "rgb(255, 242, 204)", "rgb(217, 234, 211)", "rgb(208, 224, 227)", "rgb(201, 218, 248)", "rgb(207, 226, 243)",
  230. "rgb(217, 210, 233)", "rgb(234, 209, 220)", "rgb(221, 126, 107)", "rgb(234, 153, 153)", "rgb(249, 203, 156)", "rgb(255, 229, 153)",
  231. "rgb(182, 215, 168)", "rgb(162, 196, 201)", "rgb(164, 194, 244)", "rgb(159, 197, 232)", "rgb(180, 167, 214)"]
  232. palette.forEach(item => str += `<p style='width:20px;height:20px;background:${item};margin:0 5px;border: solid 1px #d0d0d0;'></p>`)
  233. return str;
  234. },
  235. setPosition(x, y) {
  236. var LEFT = parseInt(x - this.pancelLeft),
  237. TOP = parseInt(y - this.pancelTop);
  238. this.pointLeft = Math.max(0, Math.min(LEFT, this.pancel_width));
  239. this.pointTop = Math.max(0, Math.min(TOP, this.pancel_height));
  240. util.css(this.elem_picker, {
  241. left: this.pointLeft + "px",
  242. top: this.pointTop + "px"
  243. })
  244. this.hsb.s = parseInt(100 * this.pointLeft / this.pancel_width);
  245. this.hsb.b = parseInt(100 * (this.pancel_height - this.pointTop) / this.pancel_height);
  246. this.setShowColor();
  247. this.setValue(this.rgba);
  248. },
  249. setBar: function (elem, x) {
  250. var elem_bar = elem.getElementsByTagName("div")[0],
  251. rect = elem.getBoundingClientRect(),
  252. elem_width = elem.offsetWidth,
  253. X = Math.max(0, Math.min(x - rect.x, elem_width));
  254. if (elem_bar === this.elem_barPicker1) {
  255. util.css(elem_bar, {
  256. left: X + "px"
  257. });
  258. this.hsb.h = parseInt(360 * X / elem_width);
  259. } else {
  260. util.css(elem_bar, {
  261. left: X + "px"
  262. });
  263. this.rgba.a = X / elem_width;
  264. }
  265. this.setPancelColor(this.hsb.h);
  266. this.setShowColor();
  267. this.setValue(this.rgba);
  268. },
  269. setPancelColor: function (h) {
  270. var rgb = this.HSBToRGB({ h: h, s: 100, b: 100 });
  271. util.css(this.elem_colorPancel, {
  272. background: 'rgba(' + rgb.r + ',' + rgb.g + ',' + rgb.b + ',' + this.rgba.a + ')'
  273. });
  274. },
  275. setShowColor: function () {
  276. var rgb = this.HSBToRGB(this.hsb);
  277. this.rgba.r = rgb.r;
  278. this.rgba.g = rgb.g;
  279. this.rgba.b = rgb.b;
  280. util.css(this.elem_showColor, {
  281. background: 'rgba(' + rgb.r + ',' + rgb.g + ',' + rgb.b + ',' + this.rgba.a + ')'
  282. });
  283. },
  284. setValue: function (rgb) {
  285. var hex = "#" + this.rgbToHex(rgb);
  286. this.elem_inputWrap.innerHTML = this.getInputTpl();
  287. console.log("1")
  288. this.Opt.change(this.bindElem, hex);
  289. },
  290. setColorByInput: function (value) {
  291. var _this = this;
  292. switch (this.current_mode) {
  293. case "hex":
  294. value = value.slice(1);
  295. if (value.length == 3) {
  296. value = '#' + value[0] + value[0] + value[1] + value[1] + value[2] + value[2];
  297. this.hsb = this.hexToHsb(value);
  298. } else if (value.length == 6) {
  299. this.hsb = this.hexToHsb(value);
  300. }
  301. break;
  302. case 'rgb':
  303. var inputs = this.elem_wrap.getElementsByTagName("input"),
  304. rgb = {
  305. r: inputs[0].value ? parseInt(inputs[0].value) : 0,
  306. g: inputs[1].value ? parseInt(inputs[1].value) : 0,
  307. b: inputs[2].value ? parseInt(inputs[2].value) : 0
  308. };
  309. this.hsb = this.rgbToHsb(rgb);
  310. }
  311. this.changeViewByHsb();
  312. },
  313. changeViewByHsb: function () {
  314. this.pointLeft = parseInt(this.hsb.s * this.pancel_width / 100);
  315. this.pointTop = parseInt((100 - this.hsb.b) * this.pancel_height / 100);
  316. util.css(this.elem_picker, {
  317. left: this.pointLeft + "px",
  318. top: this.pointTop + "px"
  319. });
  320. this.setPancelColor(this.hsb.h);
  321. this.setShowColor();
  322. util.css(this.elem_barPicker1, {
  323. left: this.hsb.h / 360 * (this.elem_barPicker1.parentNode.offsetWidth) + "px"
  324. });
  325. var hex = '#' + this.rgbToHex(this.HSBToRGB(this.hsb));
  326. console.log("2")
  327. this.Opt.change(this.bindElem, hex);
  328. },
  329. switch_current_mode: function () {
  330. this.current_mode = this.current_mode == 'hex' ? 'rgb' : 'hex';
  331. this.elem_inputWrap.innerHTML = this.getInputTpl();
  332. },
  333. bindMove: function (elem, fn, bool) {
  334. var _this = this;
  335. elem.addEventListener("mousedown", function (e) {
  336. _this.downX = e.pageX;
  337. _this.downY = e.pageY;
  338. bool ? fn.call(_this, _this.downX, _this.downY) : fn.call(_this, elem, _this.downX, _this.downY);
  339. document.addEventListener("mousemove", mousemove, false);
  340. function mousemove(e) {
  341. _this.moveX = e.pageX;
  342. _this.moveY = e.pageY;
  343. bool ? fn.call(_this, _this.moveX, _this.moveY) : fn.call(_this, elem, _this.moveX, _this.moveY);
  344. e.preventDefault();
  345. }
  346. document.addEventListener("mouseup", mouseup, false);
  347. function mouseup(e) {
  348. document.removeEventListener("mousemove", mousemove, false)
  349. document.removeEventListener("mouseup", mouseup, false)
  350. }
  351. }, false);
  352. },
  353. show: function () {
  354. util.css(this.elem_wrap, {
  355. "display": "block"
  356. })
  357. },
  358. hide: function () {
  359. util.css(this.elem_wrap, {
  360. "display": "none"
  361. })
  362. },
  363. HSBToRGB: function (hsb) {
  364. var rgb = {};
  365. var h = Math.round(hsb.h);
  366. var s = Math.round(hsb.s * 255 / 100);
  367. var v = Math.round(hsb.b * 255 / 100);
  368. if (s == 0) {
  369. rgb.r = rgb.g = rgb.b = v;
  370. } else {
  371. var t1 = v;
  372. var t2 = (255 - s) * v / 255;
  373. var t3 = (t1 - t2) * (h % 60) / 60;
  374. if (h == 360) h = 0;
  375. if (h < 60) { rgb.r = t1; rgb.b = t2; rgb.g = t2 + t3 }
  376. else if (h < 120) { rgb.g = t1; rgb.b = t2; rgb.r = t1 - t3 }
  377. else if (h < 180) { rgb.g = t1; rgb.r = t2; rgb.b = t2 + t3 }
  378. else if (h < 240) { rgb.b = t1; rgb.r = t2; rgb.g = t1 - t3 }
  379. else if (h < 300) { rgb.b = t1; rgb.g = t2; rgb.r = t2 + t3 }
  380. else if (h < 360) { rgb.r = t1; rgb.g = t2; rgb.b = t1 - t3 }
  381. else { rgb.r = 0; rgb.g = 0; rgb.b = 0 }
  382. }
  383. return { r: Math.round(rgb.r), g: Math.round(rgb.g), b: Math.round(rgb.b) };
  384. },
  385. rgbToHex: function (rgb) {
  386. var hex = [
  387. rgb.r.toString(16),
  388. rgb.g.toString(16),
  389. rgb.b.toString(16)
  390. ];
  391. hex.map(function (str, i) {
  392. if (str.length == 1) {
  393. hex[i] = '0' + str;
  394. }
  395. });
  396. return hex.join('');
  397. },
  398. hexToRgb: function (hex) {
  399. var hex = parseInt(((hex.indexOf('#') > -1) ? hex.substring(1) : hex), 16);
  400. return { r: hex >> 16, g: (hex & 0x00FF00) >> 8, b: (hex & 0x0000FF) };
  401. },
  402. hexToHsb: function (hex) {
  403. return this.rgbToHsb(this.hexToRgb(hex));
  404. },
  405. rgbToHsb: function (rgb) {
  406. var hsb = { h: 0, s: 0, b: 0 };
  407. var min = Math.min(rgb.r, rgb.g, rgb.b);
  408. var max = Math.max(rgb.r, rgb.g, rgb.b);
  409. var delta = max - min;
  410. hsb.b = max;
  411. hsb.s = max != 0 ? 255 * delta / max : 0;
  412. if (hsb.s != 0) {
  413. if (rgb.r == max) hsb.h = (rgb.g - rgb.b) / delta;
  414. else if (rgb.g == max) hsb.h = 2 + (rgb.b - rgb.r) / delta;
  415. else hsb.h = 4 + (rgb.r - rgb.g) / delta;
  416. } else hsb.h = -1;
  417. hsb.h *= 60;
  418. if (hsb.h < 0) hsb.h += 360;
  419. hsb.s *= 100 / 255;
  420. hsb.b *= 100 / 255;
  421. return hsb;
  422. }
  423. }
  424. Colorpicker.create = function (opt) {
  425. return new Colorpicker(opt)
  426. }
  427. window.Colorpicker = Colorpicker;
  428. })()