Spectrum.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. /*
  2. * Copyright (C) 2011 Brian Grinstead All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
  14. * its contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
  18. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
  21. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. /**
  29. * @constructor
  30. * @extends {WebInspector.View}
  31. */
  32. WebInspector.Spectrum = function()
  33. {
  34. WebInspector.View.call(this);
  35. this.registerRequiredCSS("spectrum.css");
  36. this.element.className = "spectrum-container";
  37. this.element.tabIndex = 0;
  38. var topElement = this.element.createChild("div", "spectrum-top");
  39. topElement.createChild("div", "spectrum-fill");
  40. var topInnerElement = topElement.createChild("div", "spectrum-top-inner fill");
  41. this._draggerElement = topInnerElement.createChild("div", "spectrum-color");
  42. this._dragHelperElement = this._draggerElement.createChild("div", "spectrum-sat fill").createChild("div", "spectrum-val fill").createChild("div", "spectrum-dragger");
  43. this._sliderElement = topInnerElement.createChild("div", "spectrum-hue");
  44. this.slideHelper = this._sliderElement.createChild("div", "spectrum-slider");
  45. var rangeContainer = this.element.createChild("div", "spectrum-range-container");
  46. var alphaLabel = rangeContainer.createChild("label");
  47. alphaLabel.textContent = WebInspector.UIString("\u03B1:");
  48. this._alphaElement = rangeContainer.createChild("input", "spectrum-range");
  49. this._alphaElement.setAttribute("type", "range");
  50. this._alphaElement.setAttribute("min", "0");
  51. this._alphaElement.setAttribute("max", "100");
  52. this._alphaElement.addEventListener("change", alphaDrag.bind(this), false);
  53. var swatchElement = document.createElement("span");
  54. swatchElement.className = "swatch";
  55. this._swatchInnerElement = swatchElement.createChild("span", "swatch-inner");
  56. var displayContainer = this.element.createChild("div");
  57. displayContainer.appendChild(swatchElement);
  58. this._displayElement = displayContainer.createChild("span", "source-code spectrum-display-value");
  59. WebInspector.Spectrum.draggable(this._sliderElement, hueDrag.bind(this));
  60. WebInspector.Spectrum.draggable(this._draggerElement, colorDrag.bind(this), colorDragStart.bind(this));
  61. function hueDrag(element, dragX, dragY)
  62. {
  63. this.hsv[0] = (dragY / this.slideHeight);
  64. this._onchange();
  65. }
  66. var initialHelperOffset;
  67. function colorDragStart(element, dragX, dragY)
  68. {
  69. initialHelperOffset = { x: this._dragHelperElement.offsetLeft, y: this._dragHelperElement.offsetTop };
  70. }
  71. function colorDrag(element, dragX, dragY, event)
  72. {
  73. if (event.shiftKey) {
  74. if (Math.abs(dragX - initialHelperOffset.x) >= Math.abs(dragY - initialHelperOffset.y))
  75. dragY = initialHelperOffset.y;
  76. else
  77. dragX = initialHelperOffset.x;
  78. }
  79. this.hsv[1] = dragX / this.dragWidth;
  80. this.hsv[2] = (this.dragHeight - dragY) / this.dragHeight;
  81. this._onchange();
  82. }
  83. function alphaDrag()
  84. {
  85. this.hsv[3] = this._alphaElement.value / 100;
  86. this._onchange();
  87. }
  88. };
  89. WebInspector.Spectrum.Events = {
  90. ColorChanged: "ColorChanged"
  91. };
  92. WebInspector.Spectrum.hsvaToRGBA = function(h, s, v, a)
  93. {
  94. var r, g, b;
  95. var i = Math.floor(h * 6);
  96. var f = h * 6 - i;
  97. var p = v * (1 - s);
  98. var q = v * (1 - f * s);
  99. var t = v * (1 - (1 - f) * s);
  100. switch(i % 6) {
  101. case 0:
  102. r = v, g = t, b = p;
  103. break;
  104. case 1:
  105. r = q, g = v, b = p;
  106. break;
  107. case 2:
  108. r = p, g = v, b = t;
  109. break;
  110. case 3:
  111. r = p, g = q, b = v;
  112. break;
  113. case 4:
  114. r = t, g = p, b = v;
  115. break;
  116. case 5:
  117. r = v, g = p, b = q;
  118. break;
  119. }
  120. return [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255), a];
  121. };
  122. WebInspector.Spectrum.rgbaToHSVA = function(r, g, b, a)
  123. {
  124. r = r / 255;
  125. g = g / 255;
  126. b = b / 255;
  127. var max = Math.max(r, g, b);
  128. var min = Math.min(r, g, b);
  129. var h;
  130. var s;
  131. var v = max;
  132. var d = max - min;
  133. s = max ? d / max : 0;
  134. if(max === min) {
  135. // Achromatic.
  136. h = 0;
  137. } else {
  138. switch(max) {
  139. case r:
  140. h = (g - b) / d + (g < b ? 6 : 0);
  141. break;
  142. case g:
  143. h = (b - r) / d + 2;
  144. break;
  145. case b:
  146. h = (r - g) / d + 4;
  147. break;
  148. }
  149. h /= 6;
  150. }
  151. return [h, s, v, a];
  152. };
  153. //FIXME: migrate to WebInspector.installDragHandle
  154. /**
  155. * @param {Function=} onmove
  156. * @param {Function=} onstart
  157. * @param {Function=} onstop
  158. */
  159. WebInspector.Spectrum.draggable = function(element, onmove, onstart, onstop) {
  160. var doc = document;
  161. var dragging;
  162. var offset;
  163. var scrollOffset;
  164. var maxHeight;
  165. var maxWidth;
  166. function consume(e)
  167. {
  168. e.consume(true);
  169. }
  170. function move(e)
  171. {
  172. if (dragging) {
  173. var dragX = Math.max(0, Math.min(e.pageX - offset.left + scrollOffset.left, maxWidth));
  174. var dragY = Math.max(0, Math.min(e.pageY - offset.top + scrollOffset.top, maxHeight));
  175. if (onmove)
  176. onmove(element, dragX, dragY, e);
  177. }
  178. }
  179. function start(e)
  180. {
  181. var rightClick = e.which ? (e.which === 3) : (e.button === 2);
  182. if (!rightClick && !dragging) {
  183. if (onstart)
  184. onstart(element, e)
  185. dragging = true;
  186. maxHeight = element.clientHeight;
  187. maxWidth = element.clientWidth;
  188. scrollOffset = element.scrollOffset();
  189. offset = element.totalOffset();
  190. doc.addEventListener("selectstart", consume, false);
  191. doc.addEventListener("dragstart", consume, false);
  192. doc.addEventListener("mousemove", move, false);
  193. doc.addEventListener("mouseup", stop, false);
  194. move(e);
  195. consume(e);
  196. }
  197. }
  198. function stop(e)
  199. {
  200. if (dragging) {
  201. doc.removeEventListener("selectstart", consume, false);
  202. doc.removeEventListener("dragstart", consume, false);
  203. doc.removeEventListener("mousemove", move, false);
  204. doc.removeEventListener("mouseup", stop, false);
  205. if (onstop)
  206. onstop(element, e);
  207. }
  208. dragging = false;
  209. }
  210. element.addEventListener("mousedown", start, false);
  211. };
  212. WebInspector.Spectrum.prototype = {
  213. /**
  214. * @type {WebInspector.Color}
  215. */
  216. set color(color)
  217. {
  218. var rgba = (color.rgba || color.rgb).slice(0);
  219. if (rgba.length === 3)
  220. rgba[3] = 1;
  221. this.hsv = WebInspector.Spectrum.rgbaToHSVA(rgba[0], rgba[1], rgba[2], rgba[3]);
  222. },
  223. get color()
  224. {
  225. var rgba = WebInspector.Spectrum.hsvaToRGBA(this.hsv[0], this.hsv[1], this.hsv[2], this.hsv[3]);
  226. var color;
  227. if (rgba[3] === 1)
  228. color = WebInspector.Color.fromRGB(rgba[0], rgba[1], rgba[2]);
  229. else
  230. color = WebInspector.Color.fromRGBA(rgba[0], rgba[1], rgba[2], rgba[3]);
  231. var colorValue = color.toString(this.outputColorFormat);
  232. if (!colorValue)
  233. colorValue = color.toString(); // this.outputColorFormat can be invalid for current color (e.g. "nickname").
  234. return WebInspector.Color.parse(colorValue);
  235. },
  236. get outputColorFormat()
  237. {
  238. var cf = WebInspector.Color.Format;
  239. var format = this._originalFormat;
  240. if (this.hsv[3] === 1) {
  241. // Simplify transparent formats.
  242. if (format === cf.RGBA)
  243. format = cf.RGB;
  244. else if (format === cf.HSLA)
  245. format = cf.HSL;
  246. } else {
  247. // Everything except HSL(A) should be returned as RGBA if transparency is involved.
  248. if (format === cf.HSL || format === cf.HSLA)
  249. format = cf.HSLA;
  250. else
  251. format = cf.RGBA;
  252. }
  253. return format;
  254. },
  255. get colorHueOnly()
  256. {
  257. var rgba = WebInspector.Spectrum.hsvaToRGBA(this.hsv[0], 1, 1, 1);
  258. return WebInspector.Color.fromRGBA(rgba[0], rgba[1], rgba[2], rgba[3]);
  259. },
  260. set displayText(text)
  261. {
  262. this._displayElement.textContent = text;
  263. },
  264. _onchange: function()
  265. {
  266. this._updateUI();
  267. this.dispatchEventToListeners(WebInspector.Spectrum.Events.ColorChanged, this.color);
  268. },
  269. _updateHelperLocations: function()
  270. {
  271. var h = this.hsv[0];
  272. var s = this.hsv[1];
  273. var v = this.hsv[2];
  274. // Where to show the little circle that displays your current selected color.
  275. var dragX = s * this.dragWidth;
  276. var dragY = this.dragHeight - (v * this.dragHeight);
  277. dragX = Math.max(-this._dragHelperElementHeight,
  278. Math.min(this.dragWidth - this._dragHelperElementHeight, dragX - this._dragHelperElementHeight));
  279. dragY = Math.max(-this._dragHelperElementHeight,
  280. Math.min(this.dragHeight - this._dragHelperElementHeight, dragY - this._dragHelperElementHeight));
  281. this._dragHelperElement.positionAt(dragX, dragY);
  282. // Where to show the bar that displays your current selected hue.
  283. var slideY = (h * this.slideHeight) - this.slideHelperHeight;
  284. this.slideHelper.style.top = slideY + "px";
  285. this._alphaElement.value = this.hsv[3] * 100;
  286. },
  287. _updateUI: function()
  288. {
  289. this._updateHelperLocations();
  290. var rgb = (this.color.rgba || this.color.rgb).slice(0);
  291. if (rgb.length === 3)
  292. rgb[3] = 1;
  293. var rgbHueOnly = this.colorHueOnly.rgb;
  294. var flatColor = "rgb(" + rgbHueOnly[0] + ", " + rgbHueOnly[1] + ", " + rgbHueOnly[2] + ")";
  295. var fullColor = "rgba(" + rgb[0] + ", " + rgb[1] + ", " + rgb[2] + ", " + rgb[3] + ")";
  296. this._draggerElement.style.backgroundColor = flatColor;
  297. this._swatchInnerElement.style.backgroundColor = fullColor;
  298. this._alphaElement.value = this.hsv[3] * 100;
  299. },
  300. wasShown: function()
  301. {
  302. this.slideHeight = this._sliderElement.offsetHeight;
  303. this.dragWidth = this._draggerElement.offsetWidth;
  304. this.dragHeight = this._draggerElement.offsetHeight;
  305. this._dragHelperElementHeight = this._dragHelperElement.offsetHeight / 2;
  306. this.slideHelperHeight = this.slideHelper.offsetHeight / 2;
  307. this._updateUI();
  308. },
  309. __proto__: WebInspector.View.prototype
  310. }
  311. /**
  312. * @constructor
  313. * @extends {WebInspector.Object}
  314. */
  315. WebInspector.SpectrumPopupHelper = function()
  316. {
  317. this._spectrum = new WebInspector.Spectrum();
  318. this._spectrum.element.addEventListener("keydown", this._onKeyDown.bind(this), false);
  319. this._popover = new WebInspector.Popover();
  320. this._popover.setCanShrink(false);
  321. this._popover.element.addEventListener("mousedown", consumeEvent, false);
  322. this._hideProxy = this.hide.bind(this, true);
  323. }
  324. WebInspector.SpectrumPopupHelper.Events = {
  325. Hidden: "Hidden"
  326. };
  327. WebInspector.SpectrumPopupHelper.prototype = {
  328. /**
  329. * @return {WebInspector.Spectrum}
  330. */
  331. spectrum: function()
  332. {
  333. return this._spectrum;
  334. },
  335. toggle: function(element, color, format)
  336. {
  337. if (this._popover.isShowing())
  338. this.hide(true);
  339. else
  340. this.show(element, color, format);
  341. return this._popover.isShowing();
  342. },
  343. show: function(element, color, format)
  344. {
  345. if (this._popover.isShowing()) {
  346. if (this._anchorElement === element)
  347. return false;
  348. // Reopen the picker for another anchor element.
  349. this.hide(true);
  350. }
  351. this._anchorElement = element;
  352. this._spectrum.color = color;
  353. this._spectrum._originalFormat = format || color.format;
  354. this.reposition(element);
  355. document.addEventListener("mousedown", this._hideProxy, false);
  356. window.addEventListener("blur", this._hideProxy, false);
  357. return true;
  358. },
  359. reposition: function(element)
  360. {
  361. if (!this._previousFocusElement)
  362. this._previousFocusElement = WebInspector.currentFocusElement();
  363. this._popover.showView(this._spectrum, element);
  364. WebInspector.setCurrentFocusElement(this._spectrum.element);
  365. },
  366. /**
  367. * @param {boolean=} commitEdit
  368. */
  369. hide: function(commitEdit)
  370. {
  371. if (!this._popover.isShowing())
  372. return;
  373. this._popover.hide();
  374. document.removeEventListener("mousedown", this._hideProxy, false);
  375. window.removeEventListener("blur", this._hideProxy, false);
  376. this.dispatchEventToListeners(WebInspector.SpectrumPopupHelper.Events.Hidden, !!commitEdit);
  377. WebInspector.setCurrentFocusElement(this._previousFocusElement);
  378. delete this._previousFocusElement;
  379. delete this._anchorElement;
  380. },
  381. _onKeyDown: function(event)
  382. {
  383. if (event.keyIdentifier === "Enter") {
  384. this.hide(true);
  385. event.consume(true);
  386. return;
  387. }
  388. if (event.keyIdentifier === "U+001B") { // Escape key
  389. this.hide(false);
  390. event.consume(true);
  391. }
  392. },
  393. __proto__: WebInspector.Object.prototype
  394. }
  395. /**
  396. * @constructor
  397. */
  398. WebInspector.ColorSwatch = function()
  399. {
  400. this.element = document.createElement("span");
  401. this._swatchInnerElement = this.element.createChild("span", "swatch-inner");
  402. this.element.title = WebInspector.UIString("Click to open a colorpicker. Shift-click to change color format");
  403. this.element.className = "swatch";
  404. this.element.addEventListener("mousedown", consumeEvent, false);
  405. this.element.addEventListener("dblclick", consumeEvent, false);
  406. }
  407. WebInspector.ColorSwatch.prototype = {
  408. /**
  409. * @param {string} colorString
  410. */
  411. setColorString: function(colorString)
  412. {
  413. this._swatchInnerElement.style.backgroundColor = colorString;
  414. }
  415. }