Spectrum.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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] = (this.slideHeight - 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. /**
  93. * @param {Function=} onmove
  94. * @param {Function=} onstart
  95. * @param {Function=} onstop
  96. */
  97. WebInspector.Spectrum.draggable = function(element, onmove, onstart, onstop) {
  98. var doc = document;
  99. var dragging;
  100. var offset;
  101. var scrollOffset;
  102. var maxHeight;
  103. var maxWidth;
  104. function consume(e)
  105. {
  106. e.consume(true);
  107. }
  108. function move(e)
  109. {
  110. if (dragging) {
  111. var dragX = Math.max(0, Math.min(e.pageX - offset.left + scrollOffset.left, maxWidth));
  112. var dragY = Math.max(0, Math.min(e.pageY - offset.top + scrollOffset.top, maxHeight));
  113. if (onmove)
  114. onmove(element, dragX, dragY, e);
  115. }
  116. }
  117. function start(e)
  118. {
  119. var rightClick = e.which ? (e.which === 3) : (e.button === 2);
  120. if (!rightClick && !dragging) {
  121. if (onstart)
  122. onstart(element, e)
  123. dragging = true;
  124. maxHeight = element.clientHeight;
  125. maxWidth = element.clientWidth;
  126. scrollOffset = element.scrollOffset();
  127. offset = element.totalOffset();
  128. doc.addEventListener("selectstart", consume, false);
  129. doc.addEventListener("dragstart", consume, false);
  130. doc.addEventListener("mousemove", move, false);
  131. doc.addEventListener("mouseup", stop, false);
  132. move(e);
  133. consume(e);
  134. }
  135. }
  136. function stop(e)
  137. {
  138. if (dragging) {
  139. doc.removeEventListener("selectstart", consume, false);
  140. doc.removeEventListener("dragstart", consume, false);
  141. doc.removeEventListener("mousemove", move, false);
  142. doc.removeEventListener("mouseup", stop, false);
  143. if (onstop)
  144. onstop(element, e);
  145. }
  146. dragging = false;
  147. }
  148. element.addEventListener("mousedown", start, false);
  149. };
  150. WebInspector.Spectrum.prototype = {
  151. /**
  152. * @param {WebInspector.Color} color
  153. */
  154. setColor: function(color)
  155. {
  156. this._hsv = color.hsva();
  157. },
  158. /**
  159. * @return {WebInspector.Color}
  160. */
  161. color: function()
  162. {
  163. return WebInspector.Color.fromHSVA(this._hsv);
  164. },
  165. _colorString: function()
  166. {
  167. var cf = WebInspector.Color.Format;
  168. var format = this._originalFormat;
  169. var color = this.color();
  170. var originalFormatString = color.toString(this._originalFormat);
  171. if (originalFormatString)
  172. return originalFormatString;
  173. if (color.hasAlpha()) {
  174. // Everything except HSL(A) should be returned as RGBA if transparency is involved.
  175. if (format === cf.HSLA || format === cf.HSL)
  176. return color.toString(cf.HSLA);
  177. else
  178. return color.toString(cf.RGBA);
  179. }
  180. if (format === cf.ShortHEX)
  181. return color.toString(cf.HEX);
  182. console.assert(format === cf.Nickname);
  183. return color.toString(cf.RGB);
  184. },
  185. set displayText(text)
  186. {
  187. this._displayElement.textContent = text;
  188. },
  189. _onchange: function()
  190. {
  191. this._updateUI();
  192. this.dispatchEventToListeners(WebInspector.Spectrum.Events.ColorChanged, this._colorString());
  193. },
  194. _updateHelperLocations: function()
  195. {
  196. var h = this._hsv[0];
  197. var s = this._hsv[1];
  198. var v = this._hsv[2];
  199. // Where to show the little circle that displays your current selected color.
  200. var dragX = s * this.dragWidth;
  201. var dragY = this.dragHeight - (v * this.dragHeight);
  202. dragX = Math.max(-this._dragHelperElementHeight,
  203. Math.min(this.dragWidth - this._dragHelperElementHeight, dragX - this._dragHelperElementHeight));
  204. dragY = Math.max(-this._dragHelperElementHeight,
  205. Math.min(this.dragHeight - this._dragHelperElementHeight, dragY - this._dragHelperElementHeight));
  206. this._dragHelperElement.positionAt(dragX, dragY);
  207. // Where to show the bar that displays your current selected hue.
  208. var slideY = this.slideHeight - ((h * this.slideHeight) + this.slideHelperHeight);
  209. this.slideHelper.style.top = slideY + "px";
  210. this._alphaElement.value = this._hsv[3] * 100;
  211. },
  212. _updateUI: function()
  213. {
  214. this._updateHelperLocations();
  215. this._draggerElement.style.backgroundColor = WebInspector.Color.fromHSVA([this._hsv[0], 1, 1, 1]).toString(WebInspector.Color.Format.RGB);
  216. this._swatchInnerElement.style.backgroundColor = this.color().toString(WebInspector.Color.Format.RGBA);
  217. this._alphaElement.value = this._hsv[3] * 100;
  218. },
  219. wasShown: function()
  220. {
  221. this.slideHeight = this._sliderElement.offsetHeight;
  222. this.dragWidth = this._draggerElement.offsetWidth;
  223. this.dragHeight = this._draggerElement.offsetHeight;
  224. this._dragHelperElementHeight = this._dragHelperElement.offsetHeight / 2;
  225. this.slideHelperHeight = this.slideHelper.offsetHeight / 2;
  226. this._updateUI();
  227. },
  228. __proto__: WebInspector.View.prototype
  229. }
  230. /**
  231. * @constructor
  232. * @extends {WebInspector.Object}
  233. */
  234. WebInspector.SpectrumPopupHelper = function()
  235. {
  236. this._spectrum = new WebInspector.Spectrum();
  237. this._spectrum.element.addEventListener("keydown", this._onKeyDown.bind(this), false);
  238. this._popover = new WebInspector.Popover();
  239. this._popover.setCanShrink(false);
  240. this._popover.element.addEventListener("mousedown", consumeEvent, false);
  241. this._hideProxy = this.hide.bind(this, true);
  242. }
  243. WebInspector.SpectrumPopupHelper.Events = {
  244. Hidden: "Hidden"
  245. };
  246. WebInspector.SpectrumPopupHelper.prototype = {
  247. /**
  248. * @return {WebInspector.Spectrum}
  249. */
  250. spectrum: function()
  251. {
  252. return this._spectrum;
  253. },
  254. toggle: function(element, color, format)
  255. {
  256. if (this._popover.isShowing())
  257. this.hide(true);
  258. else
  259. this.show(element, color, format);
  260. return this._popover.isShowing();
  261. },
  262. show: function(element, color, format)
  263. {
  264. if (this._popover.isShowing()) {
  265. if (this._anchorElement === element)
  266. return false;
  267. // Reopen the picker for another anchor element.
  268. this.hide(true);
  269. }
  270. this._anchorElement = element;
  271. this._spectrum.setColor(color);
  272. this._spectrum._originalFormat = format !== WebInspector.Color.Format.Original ? format : color.format();
  273. this.reposition(element);
  274. document.addEventListener("mousedown", this._hideProxy, false);
  275. window.addEventListener("blur", this._hideProxy, false);
  276. return true;
  277. },
  278. reposition: function(element)
  279. {
  280. if (!this._previousFocusElement)
  281. this._previousFocusElement = WebInspector.currentFocusElement();
  282. this._popover.showView(this._spectrum, element);
  283. WebInspector.setCurrentFocusElement(this._spectrum.element);
  284. },
  285. /**
  286. * @param {boolean=} commitEdit
  287. */
  288. hide: function(commitEdit)
  289. {
  290. if (!this._popover.isShowing())
  291. return;
  292. this._popover.hide();
  293. document.removeEventListener("mousedown", this._hideProxy, false);
  294. window.removeEventListener("blur", this._hideProxy, false);
  295. this.dispatchEventToListeners(WebInspector.SpectrumPopupHelper.Events.Hidden, !!commitEdit);
  296. WebInspector.setCurrentFocusElement(this._previousFocusElement);
  297. delete this._previousFocusElement;
  298. delete this._anchorElement;
  299. },
  300. _onKeyDown: function(event)
  301. {
  302. if (event.keyIdentifier === "Enter") {
  303. this.hide(true);
  304. event.consume(true);
  305. return;
  306. }
  307. if (event.keyIdentifier === "U+001B") { // Escape key
  308. this.hide(false);
  309. event.consume(true);
  310. }
  311. },
  312. __proto__: WebInspector.Object.prototype
  313. }
  314. /**
  315. * @constructor
  316. */
  317. WebInspector.ColorSwatch = function()
  318. {
  319. this.element = document.createElement("span");
  320. this._swatchInnerElement = this.element.createChild("span", "swatch-inner");
  321. this.element.title = WebInspector.UIString("Click to open a colorpicker. Shift-click to change color format");
  322. this.element.className = "swatch";
  323. this.element.addEventListener("mousedown", consumeEvent, false);
  324. this.element.addEventListener("dblclick", consumeEvent, false);
  325. }
  326. WebInspector.ColorSwatch.prototype = {
  327. /**
  328. * @param {string} colorString
  329. */
  330. setColorString: function(colorString)
  331. {
  332. this._swatchInnerElement.style.backgroundColor = colorString;
  333. }
  334. }