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. 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'};
  5. /**
  6. * @param {number} [pixelRatio=1]
  7. */
  8. class ColorPicker {
  9. /**
  10. * @param {number} [pixelRatio=1]
  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. if (typeof color === 'string') {
  74. return htmlColors[color];
  75. }
  76. }
  77. /**
  78. * Set the color of the colorPicker
  79. * Supported formats:
  80. * 'red' --> HTML color string
  81. * '#ffffff' --> hex string
  82. * 'rgb(255,255,255)' --> rgb string
  83. * 'rgba(255,255,255,1.0)' --> rgba string
  84. * {r:255,g:255,b:255} --> rgb object
  85. * {r:255,g:255,b:255,a:1.0} --> rgba object
  86. * @param {string|Object} color
  87. * @param {boolean} [setInitial=true]
  88. */
  89. setColor(color, setInitial = true) {
  90. if (color === 'none') {
  91. return;
  92. }
  93. let rgba;
  94. // if a html color shorthand is used, convert to hex
  95. var htmlColor = this._isColorString(color);
  96. if (htmlColor !== undefined) {
  97. color = htmlColor;
  98. }
  99. // check format
  100. if (util.isString(color) === true) {
  101. if (util.isValidRGB(color) === true) {
  102. let rgbaArray = color.substr(4).substr(0, color.length - 5).split(',');
  103. rgba = {r:rgbaArray[0], g:rgbaArray[1], b:rgbaArray[2], a:1.0};
  104. }
  105. else if (util.isValidRGBA(color) === true) {
  106. let rgbaArray = color.substr(5).substr(0, color.length - 6).split(',');
  107. rgba = {r:rgbaArray[0], g:rgbaArray[1], b:rgbaArray[2], a:rgbaArray[3]};
  108. }
  109. else if (util.isValidHex(color) === true) {
  110. let rgbObj = util.hexToRGB(color);
  111. rgba = {r:rgbObj.r, g:rgbObj.g, b:rgbObj.b, a:1.0};
  112. }
  113. }
  114. else {
  115. if (color instanceof Object) {
  116. if (color.r !== undefined && color.g !== undefined && color.b !== undefined) {
  117. let alpha = color.a !== undefined ? color.a : '1.0';
  118. rgba = {r:color.r, g:color.g, b:color.b, a:alpha};
  119. }
  120. }
  121. }
  122. // set color
  123. if (rgba === undefined) {
  124. 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));
  125. }
  126. else {
  127. this._setColor(rgba, setInitial);
  128. }
  129. }
  130. /**
  131. * this shows the color picker.
  132. * The hue circle is constructed once and stored.
  133. */
  134. show() {
  135. if (this.closeCallback !== undefined) {
  136. this.closeCallback();
  137. this.closeCallback = undefined;
  138. }
  139. this.applied = false;
  140. this.frame.style.display = 'block';
  141. this._generateHueCircle();
  142. }
  143. // ------------------------------------------ PRIVATE ----------------------------- //
  144. /**
  145. * Hide the picker. Is called by the cancel button.
  146. * Optional boolean to store the previous color for easy access later on.
  147. * @param {boolean} [storePrevious=true]
  148. * @private
  149. */
  150. _hide(storePrevious = true) {
  151. // store the previous color for next time;
  152. if (storePrevious === true) {
  153. this.previousColor = util.extend({}, this.color);
  154. }
  155. if (this.applied === true) {
  156. this.updateCallback(this.initialColor);
  157. }
  158. this.frame.style.display = 'none';
  159. // call the closing callback, restoring the onclick method.
  160. // this is in a setTimeout because it will trigger the show again before the click is done.
  161. setTimeout(() => {
  162. if (this.closeCallback !== undefined) {
  163. this.closeCallback();
  164. this.closeCallback = undefined;
  165. }
  166. },0);
  167. }
  168. /**
  169. * bound to the save button. Saves and hides.
  170. * @private
  171. */
  172. _save() {
  173. this.updateCallback(this.color);
  174. this.applied = false;
  175. this._hide();
  176. }
  177. /**
  178. * Bound to apply button. Saves but does not close. Is undone by the cancel button.
  179. * @private
  180. */
  181. _apply() {
  182. this.applied = true;
  183. this.updateCallback(this.color);
  184. this._updatePicker(this.color);
  185. }
  186. /**
  187. * load the color from the previous session.
  188. * @private
  189. */
  190. _loadLast() {
  191. if (this.previousColor !== undefined) {
  192. this.setColor(this.previousColor, false);
  193. }
  194. else {
  195. alert("There is no last color to load...");
  196. }
  197. }
  198. /**
  199. * set the color, place the picker
  200. * @param {Object} rgba
  201. * @param {boolean} [setInitial=true]
  202. * @private
  203. */
  204. _setColor(rgba, setInitial = true) {
  205. // store the initial color
  206. if (setInitial === true) {
  207. this.initialColor = util.extend({}, rgba);
  208. }
  209. this.color = rgba;
  210. let hsv = util.RGBToHSV(rgba.r, rgba.g, rgba.b);
  211. let angleConvert = 2 * Math.PI;
  212. let radius = this.r * hsv.s;
  213. let x = this.centerCoordinates.x + radius * Math.sin(angleConvert * hsv.h);
  214. let y = this.centerCoordinates.y + radius * Math.cos(angleConvert * hsv.h);
  215. this.colorPickerSelector.style.left = x - 0.5 * this.colorPickerSelector.clientWidth + 'px';
  216. this.colorPickerSelector.style.top = y - 0.5 * this.colorPickerSelector.clientHeight + 'px';
  217. this._updatePicker(rgba);
  218. }
  219. /**
  220. * bound to opacity control
  221. * @param {number} value
  222. * @private
  223. */
  224. _setOpacity(value) {
  225. this.color.a = value / 100;
  226. this._updatePicker(this.color);
  227. }
  228. /**
  229. * bound to brightness control
  230. * @param {number} value
  231. * @private
  232. */
  233. _setBrightness(value) {
  234. let hsv = util.RGBToHSV(this.color.r, this.color.g, this.color.b);
  235. hsv.v = value / 100;
  236. let rgba = util.HSVToRGB(hsv.h, hsv.s, hsv.v);
  237. rgba['a'] = this.color.a;
  238. this.color = rgba;
  239. this._updatePicker();
  240. }
  241. /**
  242. * update the color picker. A black circle overlays the hue circle to mimic the brightness decreasing.
  243. * @param {Object} rgba
  244. * @private
  245. */
  246. _updatePicker(rgba = this.color) {
  247. let hsv = util.RGBToHSV(rgba.r, rgba.g, rgba.b);
  248. let ctx = this.colorPickerCanvas.getContext('2d');
  249. if (this.pixelRation === undefined) {
  250. this.pixelRatio = (window.devicePixelRatio || 1) / (ctx.webkitBackingStorePixelRatio ||
  251. ctx.mozBackingStorePixelRatio ||
  252. ctx.msBackingStorePixelRatio ||
  253. ctx.oBackingStorePixelRatio ||
  254. ctx.backingStorePixelRatio || 1);
  255. }
  256. ctx.setTransform(this.pixelRatio, 0, 0, this.pixelRatio, 0, 0);
  257. // clear the canvas
  258. let w = this.colorPickerCanvas.clientWidth;
  259. let h = this.colorPickerCanvas.clientHeight;
  260. ctx.clearRect(0, 0, w, h);
  261. ctx.putImageData(this.hueCircle, 0,0);
  262. ctx.fillStyle = 'rgba(0,0,0,' + (1- hsv.v) + ')';
  263. ctx.circle(this.centerCoordinates.x, this.centerCoordinates.y, this.r);
  264. ctx.fill();
  265. this.brightnessRange.value = 100 * hsv.v;
  266. this.opacityRange.value = 100 * rgba.a;
  267. this.initialColorDiv.style.backgroundColor = 'rgba(' + this.initialColor.r + ',' + this.initialColor.g + ',' + this.initialColor.b + ',' + this.initialColor.a + ')';
  268. this.newColorDiv.style.backgroundColor = 'rgba(' + this.color.r + ',' + this.color.g + ',' + this.color.b + ',' + this.color.a + ')';
  269. }
  270. /**
  271. * used by create to set the size of the canvas.
  272. * @private
  273. */
  274. _setSize() {
  275. this.colorPickerCanvas.style.width = '100%';
  276. this.colorPickerCanvas.style.height = '100%';
  277. this.colorPickerCanvas.width = 289 * this.pixelRatio;
  278. this.colorPickerCanvas.height = 289 * this.pixelRatio;
  279. }
  280. /**
  281. * create all dom elements
  282. * TODO: cleanup, lots of similar dom elements
  283. * @private
  284. */
  285. _create() {
  286. this.frame = document.createElement('div');
  287. this.frame.className = 'vis-color-picker';
  288. this.colorPickerDiv = document.createElement('div');
  289. this.colorPickerSelector = document.createElement('div');
  290. this.colorPickerSelector.className = 'vis-selector';
  291. this.colorPickerDiv.appendChild(this.colorPickerSelector);
  292. this.colorPickerCanvas = document.createElement('canvas');
  293. this.colorPickerDiv.appendChild(this.colorPickerCanvas);
  294. if (!this.colorPickerCanvas.getContext) {
  295. let noCanvas = document.createElement( 'DIV' );
  296. noCanvas.style.color = 'red';
  297. noCanvas.style.fontWeight = 'bold' ;
  298. noCanvas.style.padding = '10px';
  299. noCanvas.innerHTML = 'Error: your browser does not support HTML canvas';
  300. this.colorPickerCanvas.appendChild(noCanvas);
  301. }
  302. else {
  303. let ctx = this.colorPickerCanvas.getContext("2d");
  304. this.pixelRatio = (window.devicePixelRatio || 1) / (ctx.webkitBackingStorePixelRatio ||
  305. ctx.mozBackingStorePixelRatio ||
  306. ctx.msBackingStorePixelRatio ||
  307. ctx.oBackingStorePixelRatio ||
  308. ctx.backingStorePixelRatio || 1);
  309. this.colorPickerCanvas.getContext("2d").setTransform(this.pixelRatio, 0, 0, this.pixelRatio, 0, 0);
  310. }
  311. this.colorPickerDiv.className = 'vis-color';
  312. this.opacityDiv = document.createElement('div');
  313. this.opacityDiv.className = 'vis-opacity';
  314. this.brightnessDiv = document.createElement('div');
  315. this.brightnessDiv.className = 'vis-brightness';
  316. this.arrowDiv = document.createElement('div');
  317. this.arrowDiv.className = 'vis-arrow';
  318. this.opacityRange = document.createElement('input');
  319. try {
  320. this.opacityRange.type = 'range'; // Not supported on IE9
  321. this.opacityRange.min = '0';
  322. this.opacityRange.max = '100';
  323. }
  324. // TODO: Add some error handling and remove this lint exception
  325. catch (err) {} // eslint-disable-line no-empty
  326. this.opacityRange.value = '100';
  327. this.opacityRange.className = 'vis-range';
  328. this.brightnessRange = document.createElement('input');
  329. try {
  330. this.brightnessRange.type = 'range'; // Not supported on IE9
  331. this.brightnessRange.min = '0';
  332. this.brightnessRange.max = '100';
  333. }
  334. // TODO: Add some error handling and remove this lint exception
  335. catch (err) {} // eslint-disable-line no-empty
  336. this.brightnessRange.value = '100';
  337. this.brightnessRange.className = 'vis-range';
  338. this.opacityDiv.appendChild(this.opacityRange);
  339. this.brightnessDiv.appendChild(this.brightnessRange);
  340. var me = this;
  341. this.opacityRange.onchange = function () {me._setOpacity(this.value);};
  342. this.opacityRange.oninput = function () {me._setOpacity(this.value);};
  343. this.brightnessRange.onchange = function () {me._setBrightness(this.value);};
  344. this.brightnessRange.oninput = function () {me._setBrightness(this.value);};
  345. this.brightnessLabel = document.createElement("div");
  346. this.brightnessLabel.className = "vis-label vis-brightness";
  347. this.brightnessLabel.innerHTML = 'brightness:';
  348. this.opacityLabel = document.createElement("div");
  349. this.opacityLabel.className = "vis-label vis-opacity";
  350. this.opacityLabel.innerHTML = 'opacity:';
  351. this.newColorDiv = document.createElement("div");
  352. this.newColorDiv.className = "vis-new-color";
  353. this.newColorDiv.innerHTML = 'new';
  354. this.initialColorDiv = document.createElement("div");
  355. this.initialColorDiv.className = "vis-initial-color";
  356. this.initialColorDiv.innerHTML = 'initial';
  357. this.cancelButton = document.createElement("div");
  358. this.cancelButton.className = "vis-button vis-cancel";
  359. this.cancelButton.innerHTML = 'cancel';
  360. this.cancelButton.onclick = this._hide.bind(this, false);
  361. this.applyButton = document.createElement("div");
  362. this.applyButton.className = "vis-button vis-apply";
  363. this.applyButton.innerHTML = 'apply';
  364. this.applyButton.onclick = this._apply.bind(this);
  365. this.saveButton = document.createElement("div");
  366. this.saveButton.className = "vis-button vis-save";
  367. this.saveButton.innerHTML = 'save';
  368. this.saveButton.onclick = this._save.bind(this);
  369. this.loadButton = document.createElement("div");
  370. this.loadButton.className = "vis-button vis-load";
  371. this.loadButton.innerHTML = 'load last';
  372. this.loadButton.onclick = this._loadLast.bind(this);
  373. this.frame.appendChild(this.colorPickerDiv);
  374. this.frame.appendChild(this.arrowDiv);
  375. this.frame.appendChild(this.brightnessLabel);
  376. this.frame.appendChild(this.brightnessDiv);
  377. this.frame.appendChild(this.opacityLabel);
  378. this.frame.appendChild(this.opacityDiv);
  379. this.frame.appendChild(this.newColorDiv);
  380. this.frame.appendChild(this.initialColorDiv);
  381. this.frame.appendChild(this.cancelButton);
  382. this.frame.appendChild(this.applyButton);
  383. this.frame.appendChild(this.saveButton);
  384. this.frame.appendChild(this.loadButton);
  385. }
  386. /**
  387. * bind hammer to the color picker
  388. * @private
  389. */
  390. _bindHammer() {
  391. this.drag = {};
  392. this.pinch = {};
  393. this.hammer = new Hammer(this.colorPickerCanvas);
  394. this.hammer.get('pinch').set({enable: true});
  395. hammerUtil.onTouch(this.hammer, (event) => {this._moveSelector(event)});
  396. this.hammer.on('tap', (event) => {this._moveSelector(event)});
  397. this.hammer.on('panstart', (event) => {this._moveSelector(event)});
  398. this.hammer.on('panmove', (event) => {this._moveSelector(event)});
  399. this.hammer.on('panend', (event) => {this._moveSelector(event)});
  400. }
  401. /**
  402. * generate the hue circle. This is relatively heavy (200ms) and is done only once on the first time it is shown.
  403. * @private
  404. */
  405. _generateHueCircle() {
  406. if (this.generated === false) {
  407. let ctx = this.colorPickerCanvas.getContext('2d');
  408. if (this.pixelRation === undefined) {
  409. this.pixelRatio = (window.devicePixelRatio || 1) / (ctx.webkitBackingStorePixelRatio ||
  410. ctx.mozBackingStorePixelRatio ||
  411. ctx.msBackingStorePixelRatio ||
  412. ctx.oBackingStorePixelRatio ||
  413. ctx.backingStorePixelRatio || 1);
  414. }
  415. ctx.setTransform(this.pixelRatio, 0, 0, this.pixelRatio, 0, 0);
  416. // clear the canvas
  417. let w = this.colorPickerCanvas.clientWidth;
  418. let h = this.colorPickerCanvas.clientHeight;
  419. ctx.clearRect(0, 0, w, h);
  420. // draw hue circle
  421. let x, y, hue, sat;
  422. this.centerCoordinates = {x: w * 0.5, y: h * 0.5};
  423. this.r = 0.49 * w;
  424. let angleConvert = (2 * Math.PI) / 360;
  425. let hfac = 1 / 360;
  426. let sfac = 1 / this.r;
  427. let rgb;
  428. for (hue = 0; hue < 360; hue++) {
  429. for (sat = 0; sat < this.r; sat++) {
  430. x = this.centerCoordinates.x + sat * Math.sin(angleConvert * hue);
  431. y = this.centerCoordinates.y + sat * Math.cos(angleConvert * hue);
  432. rgb = util.HSVToRGB(hue * hfac, sat * sfac, 1);
  433. ctx.fillStyle = 'rgb(' + rgb.r + ',' + rgb.g + ',' + rgb.b + ')';
  434. ctx.fillRect(x - 0.5, y - 0.5, 2, 2);
  435. }
  436. }
  437. ctx.strokeStyle = 'rgba(0,0,0,1)';
  438. ctx.circle(this.centerCoordinates.x, this.centerCoordinates.y, this.r);
  439. ctx.stroke();
  440. this.hueCircle = ctx.getImageData(0,0,w,h);
  441. }
  442. this.generated = true;
  443. }
  444. /**
  445. * move the selector. This is called by hammer functions.
  446. *
  447. * @param {Event} event The event
  448. * @private
  449. */
  450. _moveSelector(event) {
  451. let rect = this.colorPickerDiv.getBoundingClientRect();
  452. let left = event.center.x - rect.left;
  453. let top = event.center.y - rect.top;
  454. let centerY = 0.5 * this.colorPickerDiv.clientHeight;
  455. let centerX = 0.5 * this.colorPickerDiv.clientWidth;
  456. let x = left - centerX;
  457. let y = top - centerY;
  458. let angle = Math.atan2(x,y);
  459. let radius = 0.98 * Math.min(Math.sqrt(x * x + y * y), centerX);
  460. let newTop = Math.cos(angle) * radius + centerY;
  461. let newLeft = Math.sin(angle) * radius + centerX;
  462. this.colorPickerSelector.style.top = newTop - 0.5 * this.colorPickerSelector.clientHeight + 'px';
  463. this.colorPickerSelector.style.left = newLeft - 0.5 * this.colorPickerSelector.clientWidth + 'px';
  464. // set color
  465. let h = angle / (2 * Math.PI);
  466. h = h < 0 ? h + 1 : h;
  467. let s = radius / this.r;
  468. let hsv = util.RGBToHSV(this.color.r, this.color.g, this.color.b);
  469. hsv.h = h;
  470. hsv.s = s;
  471. let rgba = util.HSVToRGB(hsv.h, hsv.s, hsv.v);
  472. rgba['a'] = this.color.a;
  473. this.color = rgba;
  474. // update previews
  475. this.initialColorDiv.style.backgroundColor = 'rgba(' + this.initialColor.r + ',' + this.initialColor.g + ',' + this.initialColor.b + ',' + this.initialColor.a + ')';
  476. this.newColorDiv.style.backgroundColor = 'rgba(' + this.color.r + ',' + this.color.g + ',' + this.color.b + ',' + this.color.a + ')';
  477. }
  478. }
  479. export default ColorPicker;