vis.js is a dynamic, browser-based visualization library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

570 lines
20 KiB

  1. let Hammer = require('../module/hammer');
  2. let hammerUtil = require('../hammerUtil');
  3. let util = require('../util');
  4. /**
  5. * @class ColorPicker
  6. */
  7. class ColorPicker {
  8. /**
  9. * @param {Number} [pixelRatio=1]
  10. * @constructor ColorPicker
  11. */
  12. constructor(pixelRatio = 1) {
  13. this.pixelRatio = pixelRatio;
  14. this.generated = false;
  15. this.centerCoordinates = {x:289/2, y:289/2};
  16. this.r = 289 * 0.49;
  17. this.color = {r:255,g:255,b:255,a:1.0};
  18. this.hueCircle = undefined;
  19. this.initialColor = {r:255,g:255,b:255,a:1.0};
  20. this.previousColor= undefined;
  21. this.applied = false;
  22. // bound by
  23. this.updateCallback = () => {};
  24. this.closeCallback = () => {};
  25. // create all DOM elements
  26. this._create();
  27. }
  28. /**
  29. * this inserts the colorPicker into a div from the DOM
  30. * @param {Element} container
  31. */
  32. insertTo(container) {
  33. if (this.hammer !== undefined) {
  34. this.hammer.destroy();
  35. this.hammer = undefined;
  36. }
  37. this.container = container;
  38. this.container.appendChild(this.frame);
  39. this._bindHammer();
  40. this._setSize();
  41. }
  42. /**
  43. * the callback is executed on apply and save. Bind it to the application
  44. * @param {function} callback
  45. */
  46. setUpdateCallback(callback) {
  47. if (typeof callback === 'function') {
  48. this.updateCallback = callback;
  49. }
  50. else {
  51. throw new Error("Function attempted to set as colorPicker update callback is not a function.");
  52. }
  53. }
  54. /**
  55. * the callback is executed on apply and save. Bind it to the application
  56. * @param {function} callback
  57. */
  58. setCloseCallback(callback) {
  59. if (typeof callback === 'function') {
  60. this.closeCallback = callback;
  61. }
  62. else {
  63. throw new Error("Function attempted to set as colorPicker closing callback is not a function.");
  64. }
  65. }
  66. /**
  67. *
  68. * @param {String} color
  69. * @returns {String}
  70. * @private
  71. */
  72. _isColorString(color) {
  73. var htmlColors = {black: '#000000',navy: '#000080',darkblue: '#00008B',mediumblue: '#0000CD',blue: '#0000FF',darkgreen: '#006400',green: '#008000',teal: '#008080',darkcyan: '#008B8B',deepskyblue: '#00BFFF',darkturquoise: '#00CED1',mediumspringgreen: '#00FA9A',lime: '#00FF00',springgreen: '#00FF7F',aqua: '#00FFFF',cyan: '#00FFFF',midnightblue: '#191970',dodgerblue: '#1E90FF',lightseagreen: '#20B2AA',forestgreen: '#228B22',seagreen: '#2E8B57',darkslategray: '#2F4F4F',limegreen: '#32CD32',mediumseagreen: '#3CB371',turquoise: '#40E0D0',royalblue: '#4169E1',steelblue: '#4682B4',darkslateblue: '#483D8B',mediumturquoise: '#48D1CC',indigo: '#4B0082',darkolivegreen: '#556B2F',cadetblue: '#5F9EA0',cornflowerblue: '#6495ED',mediumaquamarine: '#66CDAA',dimgray: '#696969',slateblue: '#6A5ACD',olivedrab: '#6B8E23',slategray: '#708090',lightslategray: '#778899',mediumslateblue: '#7B68EE',lawngreen: '#7CFC00',chartreuse: '#7FFF00',aquamarine: '#7FFFD4',maroon: '#800000',purple: '#800080',olive: '#808000',gray: '#808080',skyblue: '#87CEEB',lightskyblue: '#87CEFA',blueviolet: '#8A2BE2',darkred: '#8B0000',darkmagenta: '#8B008B',saddlebrown: '#8B4513',darkseagreen: '#8FBC8F',lightgreen: '#90EE90',mediumpurple: '#9370D8',darkviolet: '#9400D3',palegreen: '#98FB98',darkorchid: '#9932CC',yellowgreen: '#9ACD32',sienna: '#A0522D',brown: '#A52A2A',darkgray: '#A9A9A9',lightblue: '#ADD8E6',greenyellow: '#ADFF2F',paleturquoise: '#AFEEEE',lightsteelblue: '#B0C4DE',powderblue: '#B0E0E6',firebrick: '#B22222',darkgoldenrod: '#B8860B',mediumorchid: '#BA55D3',rosybrown: '#BC8F8F',darkkhaki: '#BDB76B',silver: '#C0C0C0',mediumvioletred: '#C71585',indianred: '#CD5C5C',peru: '#CD853F',chocolate: '#D2691E',tan: '#D2B48C',lightgrey: '#D3D3D3',palevioletred: '#D87093',thistle: '#D8BFD8',orchid: '#DA70D6',goldenrod: '#DAA520',crimson: '#DC143C',gainsboro: '#DCDCDC',plum: '#DDA0DD',burlywood: '#DEB887',lightcyan: '#E0FFFF',lavender: '#E6E6FA',darksalmon: '#E9967A',violet: '#EE82EE',palegoldenrod: '#EEE8AA',lightcoral: '#F08080',khaki: '#F0E68C',aliceblue: '#F0F8FF',honeydew: '#F0FFF0',azure: '#F0FFFF',sandybrown: '#F4A460',wheat: '#F5DEB3',beige: '#F5F5DC',whitesmoke: '#F5F5F5',mintcream: '#F5FFFA',ghostwhite: '#F8F8FF',salmon: '#FA8072',antiquewhite: '#FAEBD7',linen: '#FAF0E6',lightgoldenrodyellow: '#FAFAD2',oldlace: '#FDF5E6',red: '#FF0000',fuchsia: '#FF00FF',magenta: '#FF00FF',deeppink: '#FF1493',orangered: '#FF4500',tomato: '#FF6347',hotpink: '#FF69B4',coral: '#FF7F50',darkorange: '#FF8C00',lightsalmon: '#FFA07A',orange: '#FFA500',lightpink: '#FFB6C1',pink: '#FFC0CB',gold: '#FFD700',peachpuff: '#FFDAB9',navajowhite: '#FFDEAD',moccasin: '#FFE4B5',bisque: '#FFE4C4',mistyrose: '#FFE4E1',blanchedalmond: '#FFEBCD',papayawhip: '#FFEFD5',lavenderblush: '#FFF0F5',seashell: '#FFF5EE',cornsilk: '#FFF8DC',lemonchiffon: '#FFFACD',floralwhite: '#FFFAF0',snow: '#FFFAFA',yellow: '#FFFF00',lightyellow: '#FFFFE0',ivory: '#FFFFF0',white: '#FFFFFF'};
  74. if (typeof color === 'string') {
  75. return htmlColors[color];
  76. }
  77. }
  78. /**
  79. * Set the color of the colorPicker
  80. * Supported formats:
  81. * 'red' --> HTML color string
  82. * '#ffffff' --> hex string
  83. * 'rbg(255,255,255)' --> rgb string
  84. * 'rgba(255,255,255,1.0)' --> rgba string
  85. * {r:255,g:255,b:255} --> rgb object
  86. * {r:255,g:255,b:255,a:1.0} --> rgba object
  87. * @param {String|Object} color
  88. * @param {boolean} [setInitial=true]
  89. */
  90. setColor(color, setInitial = true) {
  91. if (color === 'none') {
  92. return;
  93. }
  94. let rgba;
  95. // if a html color shorthand is used, convert to hex
  96. var htmlColor = this._isColorString(color);
  97. if (htmlColor !== undefined) {
  98. color = htmlColor;
  99. }
  100. // check format
  101. if (util.isString(color) === true) {
  102. if (util.isValidRGB(color) === true) {
  103. let rgbaArray = color.substr(4).substr(0, color.length - 5).split(',');
  104. rgba = {r:rgbaArray[0], g:rgbaArray[1], b:rgbaArray[2], a:1.0};
  105. }
  106. else if (util.isValidRGBA(color) === true) {
  107. let rgbaArray = color.substr(5).substr(0, color.length - 6).split(',');
  108. rgba = {r:rgbaArray[0], g:rgbaArray[1], b:rgbaArray[2], a:rgbaArray[3]};
  109. }
  110. else if (util.isValidHex(color) === true) {
  111. let rgbObj = util.hexToRGB(color);
  112. rgba = {r:rgbObj.r, g:rgbObj.g, b:rgbObj.b, a:1.0};
  113. }
  114. }
  115. else {
  116. if (color instanceof Object) {
  117. if (color.r !== undefined && color.g !== undefined && color.b !== undefined) {
  118. let alpha = color.a !== undefined ? color.a : '1.0';
  119. rgba = {r:color.r, g:color.g, b:color.b, a:alpha};
  120. }
  121. }
  122. }
  123. // set color
  124. if (rgba === undefined) {
  125. throw new Error("Unknown color passed to the colorPicker. Supported are strings: rgb, hex, rgba. Object: rgb ({r:r,g:g,b:b,[a:a]}). Supplied: " + JSON.stringify(color));
  126. }
  127. else {
  128. this._setColor(rgba, setInitial);
  129. }
  130. }
  131. /**
  132. * this shows the color picker.
  133. * The hue circle is constructed once and stored.
  134. */
  135. show() {
  136. if (this.closeCallback !== undefined) {
  137. this.closeCallback();
  138. this.closeCallback = undefined;
  139. }
  140. this.applied = false;
  141. this.frame.style.display = 'block';
  142. this._generateHueCircle();
  143. }
  144. // ------------------------------------------ PRIVATE ----------------------------- //
  145. /**
  146. * Hide the picker. Is called by the cancel button.
  147. * Optional boolean to store the previous color for easy access later on.
  148. * @param {boolean} [storePrevious=true]
  149. * @private
  150. */
  151. _hide(storePrevious = true) {
  152. // store the previous color for next time;
  153. if (storePrevious === true) {
  154. this.previousColor = util.extend({}, this.color);
  155. }
  156. if (this.applied === true) {
  157. this.updateCallback(this.initialColor);
  158. }
  159. this.frame.style.display = 'none';
  160. // call the closing callback, restoring the onclick method.
  161. // this is in a setTimeout because it will trigger the show again before the click is done.
  162. setTimeout(() => {
  163. if (this.closeCallback !== undefined) {
  164. this.closeCallback();
  165. this.closeCallback = undefined;
  166. }
  167. },0);
  168. }
  169. /**
  170. * bound to the save button. Saves and hides.
  171. * @private
  172. */
  173. _save() {
  174. this.updateCallback(this.color);
  175. this.applied = false;
  176. this._hide();
  177. }
  178. /**
  179. * Bound to apply button. Saves but does not close. Is undone by the cancel button.
  180. * @private
  181. */
  182. _apply() {
  183. this.applied = true;
  184. this.updateCallback(this.color);
  185. this._updatePicker(this.color);
  186. }
  187. /**
  188. * load the color from the previous session.
  189. * @private
  190. */
  191. _loadLast() {
  192. if (this.previousColor !== undefined) {
  193. this.setColor(this.previousColor, false);
  194. }
  195. else {
  196. alert("There is no last color to load...");
  197. }
  198. }
  199. /**
  200. * set the color, place the picker
  201. * @param {Object} rgba
  202. * @param {boolean} [setInitial=true]
  203. * @private
  204. */
  205. _setColor(rgba, setInitial = true) {
  206. // store the initial color
  207. if (setInitial === true) {
  208. this.initialColor = util.extend({}, rgba);
  209. }
  210. this.color = rgba;
  211. let hsv = util.RGBToHSV(rgba.r, rgba.g, rgba.b);
  212. let angleConvert = 2 * Math.PI;
  213. let radius = this.r * hsv.s;
  214. let x = this.centerCoordinates.x + radius * Math.sin(angleConvert * hsv.h);
  215. let y = this.centerCoordinates.y + radius * Math.cos(angleConvert * hsv.h);
  216. this.colorPickerSelector.style.left = x - 0.5 * this.colorPickerSelector.clientWidth + 'px';
  217. this.colorPickerSelector.style.top = y - 0.5 * this.colorPickerSelector.clientHeight + 'px';
  218. this._updatePicker(rgba);
  219. }
  220. /**
  221. * bound to opacity control
  222. * @param {number} value
  223. * @private
  224. */
  225. _setOpacity(value) {
  226. this.color.a = value / 100;
  227. this._updatePicker(this.color);
  228. }
  229. /**
  230. * bound to brightness control
  231. * @param {number} value
  232. * @private
  233. */
  234. _setBrightness(value) {
  235. let hsv = util.RGBToHSV(this.color.r, this.color.g, this.color.b);
  236. hsv.v = value / 100;
  237. let rgba = util.HSVToRGB(hsv.h, hsv.s, hsv.v);
  238. rgba['a'] = this.color.a;
  239. this.color = rgba;
  240. this._updatePicker();
  241. }
  242. /**
  243. * update the color picker. A black circle overlays the hue circle to mimic the brightness decreasing.
  244. * @param {Object} rgba
  245. * @private
  246. */
  247. _updatePicker(rgba = this.color) {
  248. let hsv = util.RGBToHSV(rgba.r, rgba.g, rgba.b);
  249. let ctx = this.colorPickerCanvas.getContext('2d');
  250. if (this.pixelRation === undefined) {
  251. this.pixelRatio = (window.devicePixelRatio || 1) / (ctx.webkitBackingStorePixelRatio ||
  252. ctx.mozBackingStorePixelRatio ||
  253. ctx.msBackingStorePixelRatio ||
  254. ctx.oBackingStorePixelRatio ||
  255. ctx.backingStorePixelRatio || 1);
  256. }
  257. ctx.setTransform(this.pixelRatio, 0, 0, this.pixelRatio, 0, 0);
  258. // clear the canvas
  259. let w = this.colorPickerCanvas.clientWidth;
  260. let h = this.colorPickerCanvas.clientHeight;
  261. ctx.clearRect(0, 0, w, h);
  262. ctx.putImageData(this.hueCircle, 0,0);
  263. ctx.fillStyle = 'rgba(0,0,0,' + (1- hsv.v) + ')';
  264. ctx.circle(this.centerCoordinates.x, this.centerCoordinates.y, this.r);
  265. ctx.fill();
  266. this.brightnessRange.value = 100 * hsv.v;
  267. this.opacityRange.value = 100 * rgba.a;
  268. this.initialColorDiv.style.backgroundColor = 'rgba(' + this.initialColor.r + ',' + this.initialColor.g + ',' + this.initialColor.b + ',' + this.initialColor.a + ')';
  269. this.newColorDiv.style.backgroundColor = 'rgba(' + this.color.r + ',' + this.color.g + ',' + this.color.b + ',' + this.color.a + ')';
  270. }
  271. /**
  272. * used by create to set the size of the canvas.
  273. * @private
  274. */
  275. _setSize() {
  276. this.colorPickerCanvas.style.width = '100%';
  277. this.colorPickerCanvas.style.height = '100%';
  278. this.colorPickerCanvas.width = 289 * this.pixelRatio;
  279. this.colorPickerCanvas.height = 289 * this.pixelRatio;
  280. }
  281. /**
  282. * create all dom elements
  283. * TODO: cleanup, lots of similar dom elements
  284. * @private
  285. */
  286. _create() {
  287. this.frame = document.createElement('div');
  288. this.frame.className = 'vis-color-picker';
  289. this.colorPickerDiv = document.createElement('div');
  290. this.colorPickerSelector = document.createElement('div');
  291. this.colorPickerSelector.className = 'vis-selector';
  292. this.colorPickerDiv.appendChild(this.colorPickerSelector);
  293. this.colorPickerCanvas = document.createElement('canvas');
  294. this.colorPickerDiv.appendChild(this.colorPickerCanvas);
  295. if (!this.colorPickerCanvas.getContext) {
  296. let noCanvas = document.createElement( 'DIV' );
  297. noCanvas.style.color = 'red';
  298. noCanvas.style.fontWeight = 'bold' ;
  299. noCanvas.style.padding = '10px';
  300. noCanvas.innerHTML = 'Error: your browser does not support HTML canvas';
  301. this.colorPickerCanvas.appendChild(noCanvas);
  302. }
  303. else {
  304. let ctx = this.colorPickerCanvas.getContext("2d");
  305. this.pixelRatio = (window.devicePixelRatio || 1) / (ctx.webkitBackingStorePixelRatio ||
  306. ctx.mozBackingStorePixelRatio ||
  307. ctx.msBackingStorePixelRatio ||
  308. ctx.oBackingStorePixelRatio ||
  309. ctx.backingStorePixelRatio || 1);
  310. this.colorPickerCanvas.getContext("2d").setTransform(this.pixelRatio, 0, 0, this.pixelRatio, 0, 0);
  311. }
  312. this.colorPickerDiv.className = 'vis-color';
  313. this.opacityDiv = document.createElement('div');
  314. this.opacityDiv.className = 'vis-opacity';
  315. this.brightnessDiv = document.createElement('div');
  316. this.brightnessDiv.className = 'vis-brightness';
  317. this.arrowDiv = document.createElement('div');
  318. this.arrowDiv.className = 'vis-arrow';
  319. this.opacityRange = document.createElement('input');
  320. try {
  321. this.opacityRange.type = 'range'; // Not supported on IE9
  322. this.opacityRange.min = '0';
  323. this.opacityRange.max = '100';
  324. }
  325. // TODO: Add some error handling and remove this lint exception
  326. catch (err) {} // eslint-disable-line no-empty
  327. this.opacityRange.value = '100';
  328. this.opacityRange.className = 'vis-range';
  329. this.brightnessRange = document.createElement('input');
  330. try {
  331. this.brightnessRange.type = 'range'; // Not supported on IE9
  332. this.brightnessRange.min = '0';
  333. this.brightnessRange.max = '100';
  334. }
  335. // TODO: Add some error handling and remove this lint exception
  336. catch (err) {} // eslint-disable-line no-empty
  337. this.brightnessRange.value = '100';
  338. this.brightnessRange.className = 'vis-range';
  339. this.opacityDiv.appendChild(this.opacityRange);
  340. this.brightnessDiv.appendChild(this.brightnessRange);
  341. var me = this;
  342. this.opacityRange.onchange = function () {me._setOpacity(this.value);};
  343. this.opacityRange.oninput = function () {me._setOpacity(this.value);};
  344. this.brightnessRange.onchange = function () {me._setBrightness(this.value);};
  345. this.brightnessRange.oninput = function () {me._setBrightness(this.value);};
  346. this.brightnessLabel = document.createElement("div");
  347. this.brightnessLabel.className = "vis-label vis-brightness";
  348. this.brightnessLabel.innerHTML = 'brightness:';
  349. this.opacityLabel = document.createElement("div");
  350. this.opacityLabel.className = "vis-label vis-opacity";
  351. this.opacityLabel.innerHTML = 'opacity:';
  352. this.newColorDiv = document.createElement("div");
  353. this.newColorDiv.className = "vis-new-color";
  354. this.newColorDiv.innerHTML = 'new';
  355. this.initialColorDiv = document.createElement("div");
  356. this.initialColorDiv.className = "vis-initial-color";
  357. this.initialColorDiv.innerHTML = 'initial';
  358. this.cancelButton = document.createElement("div");
  359. this.cancelButton.className = "vis-button vis-cancel";
  360. this.cancelButton.innerHTML = 'cancel';
  361. this.cancelButton.onclick = this._hide.bind(this, false);
  362. this.applyButton = document.createElement("div");
  363. this.applyButton.className = "vis-button vis-apply";
  364. this.applyButton.innerHTML = 'apply';
  365. this.applyButton.onclick = this._apply.bind(this);
  366. this.saveButton = document.createElement("div");
  367. this.saveButton.className = "vis-button vis-save";
  368. this.saveButton.innerHTML = 'save';
  369. this.saveButton.onclick = this._save.bind(this);
  370. this.loadButton = document.createElement("div");
  371. this.loadButton.className = "vis-button vis-load";
  372. this.loadButton.innerHTML = 'load last';
  373. this.loadButton.onclick = this._loadLast.bind(this);
  374. this.frame.appendChild(this.colorPickerDiv);
  375. this.frame.appendChild(this.arrowDiv);
  376. this.frame.appendChild(this.brightnessLabel);
  377. this.frame.appendChild(this.brightnessDiv);
  378. this.frame.appendChild(this.opacityLabel);
  379. this.frame.appendChild(this.opacityDiv);
  380. this.frame.appendChild(this.newColorDiv);
  381. this.frame.appendChild(this.initialColorDiv);
  382. this.frame.appendChild(this.cancelButton);
  383. this.frame.appendChild(this.applyButton);
  384. this.frame.appendChild(this.saveButton);
  385. this.frame.appendChild(this.loadButton);
  386. }
  387. /**
  388. * bind hammer to the color picker
  389. * @private
  390. */
  391. _bindHammer() {
  392. this.drag = {};
  393. this.pinch = {};
  394. this.hammer = new Hammer(this.colorPickerCanvas);
  395. this.hammer.get('pinch').set({enable: true});
  396. hammerUtil.onTouch(this.hammer, (event) => {this._moveSelector(event)});
  397. this.hammer.on('tap', (event) => {this._moveSelector(event)});
  398. this.hammer.on('panstart', (event) => {this._moveSelector(event)});
  399. this.hammer.on('panmove', (event) => {this._moveSelector(event)});
  400. this.hammer.on('panend', (event) => {this._moveSelector(event)});
  401. }
  402. /**
  403. * generate the hue circle. This is relatively heavy (200ms) and is done only once on the first time it is shown.
  404. * @private
  405. */
  406. _generateHueCircle() {
  407. if (this.generated === false) {
  408. let ctx = this.colorPickerCanvas.getContext('2d');
  409. if (this.pixelRation === undefined) {
  410. this.pixelRatio = (window.devicePixelRatio || 1) / (ctx.webkitBackingStorePixelRatio ||
  411. ctx.mozBackingStorePixelRatio ||
  412. ctx.msBackingStorePixelRatio ||
  413. ctx.oBackingStorePixelRatio ||
  414. ctx.backingStorePixelRatio || 1);
  415. }
  416. ctx.setTransform(this.pixelRatio, 0, 0, this.pixelRatio, 0, 0);
  417. // clear the canvas
  418. let w = this.colorPickerCanvas.clientWidth;
  419. let h = this.colorPickerCanvas.clientHeight;
  420. ctx.clearRect(0, 0, w, h);
  421. // draw hue circle
  422. let x, y, hue, sat;
  423. this.centerCoordinates = {x: w * 0.5, y: h * 0.5};
  424. this.r = 0.49 * w;
  425. let angleConvert = (2 * Math.PI) / 360;
  426. let hfac = 1 / 360;
  427. let sfac = 1 / this.r;
  428. let rgb;
  429. for (hue = 0; hue < 360; hue++) {
  430. for (sat = 0; sat < this.r; sat++) {
  431. x = this.centerCoordinates.x + sat * Math.sin(angleConvert * hue);
  432. y = this.centerCoordinates.y + sat * Math.cos(angleConvert * hue);
  433. rgb = util.HSVToRGB(hue * hfac, sat * sfac, 1);
  434. ctx.fillStyle = 'rgb(' + rgb.r + ',' + rgb.g + ',' + rgb.b + ')';
  435. ctx.fillRect(x - 0.5, y - 0.5, 2, 2);
  436. }
  437. }
  438. ctx.strokeStyle = 'rgba(0,0,0,1)';
  439. ctx.circle(this.centerCoordinates.x, this.centerCoordinates.y, this.r);
  440. ctx.stroke();
  441. this.hueCircle = ctx.getImageData(0,0,w,h);
  442. }
  443. this.generated = true;
  444. }
  445. /**
  446. * move the selector. This is called by hammer functions.
  447. *
  448. * @param {Event} event The event
  449. * @private
  450. */
  451. _moveSelector(event) {
  452. let rect = this.colorPickerDiv.getBoundingClientRect();
  453. let left = event.center.x - rect.left;
  454. let top = event.center.y - rect.top;
  455. let centerY = 0.5 * this.colorPickerDiv.clientHeight;
  456. let centerX = 0.5 * this.colorPickerDiv.clientWidth;
  457. let x = left - centerX;
  458. let y = top - centerY;
  459. let angle = Math.atan2(x,y);
  460. let radius = 0.98 * Math.min(Math.sqrt(x * x + y * y), centerX);
  461. let newTop = Math.cos(angle) * radius + centerY;
  462. let newLeft = Math.sin(angle) * radius + centerX;
  463. this.colorPickerSelector.style.top = newTop - 0.5 * this.colorPickerSelector.clientHeight + 'px';
  464. this.colorPickerSelector.style.left = newLeft - 0.5 * this.colorPickerSelector.clientWidth + 'px';
  465. // set color
  466. let h = angle / (2 * Math.PI);
  467. h = h < 0 ? h + 1 : h;
  468. let s = radius / this.r;
  469. let hsv = util.RGBToHSV(this.color.r, this.color.g, this.color.b);
  470. hsv.h = h;
  471. hsv.s = s;
  472. let rgba = util.HSVToRGB(hsv.h, hsv.s, hsv.v);
  473. rgba['a'] = this.color.a;
  474. this.color = rgba;
  475. // update previews
  476. this.initialColorDiv.style.backgroundColor = 'rgba(' + this.initialColor.r + ',' + this.initialColor.g + ',' + this.initialColor.b + ',' + this.initialColor.a + ')';
  477. this.newColorDiv.style.backgroundColor = 'rgba(' + this.color.r + ',' + this.color.g + ',' + this.color.b + ',' + this.color.a + ')';
  478. }
  479. }
  480. export default ColorPicker;