style

biblioteca script

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.cn-greasyfork.org/scripts/38435/251227/style.js

  1. /**
  2. * jscolor - JavaScript Color Picker
  3. *
  4. * @link http://jscolor.com
  5. * @license For open source use: GPLv3
  6. * For commercial use: JSColor Commercial License
  7. * @author Jan Odvarko
  8. * @version 2.0.4
  9. *
  10. * See usage examples at http://jscolor.com/examples/
  11. */
  12.  
  13.  
  14. "use strict";
  15.  
  16.  
  17. if (!window.jscolor) { window.jscolor = (function () {
  18.  
  19.  
  20. var jsc = {
  21.  
  22.  
  23. register : function () {
  24. jsc.attachDOMReadyEvent(jsc.init);
  25. jsc.attachEvent(document, 'mousedown', jsc.onDocumentMouseDown);
  26. jsc.attachEvent(document, 'touchstart', jsc.onDocumentTouchStart);
  27. jsc.attachEvent(window, 'resize', jsc.onWindowResize);
  28. },
  29.  
  30.  
  31. init : function () {
  32. if (jsc.jscolor.lookupClass) {
  33. jsc.jscolor.installByClassName(jsc.jscolor.lookupClass);
  34. }
  35. },
  36.  
  37.  
  38. tryInstallOnElements : function (elms, className) {
  39. var matchClass = new RegExp('(^|\\s)(' + className + ')(\\s*(\\{[^}]*\\})|\\s|$)', 'i');
  40.  
  41. for (var i = 0; i < elms.length; i += 1) {
  42. if (elms[i].type !== undefined && elms[i].type.toLowerCase() == 'color') {
  43. if (jsc.isColorAttrSupported) {
  44. // skip inputs of type 'color' if supported by the browser
  45. continue;
  46. }
  47. }
  48. var m;
  49. if (!elms[i].jscolor && elms[i].className && (m = elms[i].className.match(matchClass))) {
  50. var targetElm = elms[i];
  51. var optsStr = null;
  52.  
  53. var dataOptions = jsc.getDataAttr(targetElm, 'jscolor');
  54. if (dataOptions !== null) {
  55. optsStr = dataOptions;
  56. } else if (m[4]) {
  57. optsStr = m[4];
  58. }
  59.  
  60. var opts = {};
  61. if (optsStr) {
  62. try {
  63. opts = (new Function ('return (' + optsStr + ')'))();
  64. } catch(eParseError) {
  65. jsc.warn('Error parsing jscolor options: ' + eParseError + ':\n' + optsStr);
  66. }
  67. }
  68. targetElm.jscolor = new jsc.jscolor(targetElm, opts);
  69. }
  70. }
  71. },
  72.  
  73.  
  74. isColorAttrSupported : (function () {
  75. var elm = document.createElement('input');
  76. if (elm.setAttribute) {
  77. elm.setAttribute('type', 'color');
  78. if (elm.type.toLowerCase() == 'color') {
  79. return true;
  80. }
  81. }
  82. return false;
  83. })(),
  84.  
  85.  
  86. isCanvasSupported : (function () {
  87. var elm = document.createElement('canvas');
  88. return !!(elm.getContext && elm.getContext('2d'));
  89. })(),
  90.  
  91.  
  92. fetchElement : function (mixed) {
  93. return typeof mixed === 'string' ? document.getElementById(mixed) : mixed;
  94. },
  95.  
  96.  
  97. isElementType : function (elm, type) {
  98. return elm.nodeName.toLowerCase() === type.toLowerCase();
  99. },
  100.  
  101.  
  102. getDataAttr : function (el, name) {
  103. var attrName = 'data-' + name;
  104. var attrValue = el.getAttribute(attrName);
  105. if (attrValue !== null) {
  106. return attrValue;
  107. }
  108. return null;
  109. },
  110.  
  111.  
  112. attachEvent : function (el, evnt, func) {
  113. if (el.addEventListener) {
  114. el.addEventListener(evnt, func, false);
  115. } else if (el.attachEvent) {
  116. el.attachEvent('on' + evnt, func);
  117. }
  118. },
  119.  
  120.  
  121. detachEvent : function (el, evnt, func) {
  122. if (el.removeEventListener) {
  123. el.removeEventListener(evnt, func, false);
  124. } else if (el.detachEvent) {
  125. el.detachEvent('on' + evnt, func);
  126. }
  127. },
  128.  
  129.  
  130. _attachedGroupEvents : {},
  131.  
  132.  
  133. attachGroupEvent : function (groupName, el, evnt, func) {
  134. if (!jsc._attachedGroupEvents.hasOwnProperty(groupName)) {
  135. jsc._attachedGroupEvents[groupName] = [];
  136. }
  137. jsc._attachedGroupEvents[groupName].push([el, evnt, func]);
  138. jsc.attachEvent(el, evnt, func);
  139. },
  140.  
  141.  
  142. detachGroupEvents : function (groupName) {
  143. if (jsc._attachedGroupEvents.hasOwnProperty(groupName)) {
  144. for (var i = 0; i < jsc._attachedGroupEvents[groupName].length; i += 1) {
  145. var evt = jsc._attachedGroupEvents[groupName][i];
  146. jsc.detachEvent(evt[0], evt[1], evt[2]);
  147. }
  148. delete jsc._attachedGroupEvents[groupName];
  149. }
  150. },
  151.  
  152.  
  153. attachDOMReadyEvent : function (func) {
  154. var fired = false;
  155. var fireOnce = function () {
  156. if (!fired) {
  157. fired = true;
  158. func();
  159. }
  160. };
  161.  
  162. if (document.readyState === 'complete') {
  163. setTimeout(fireOnce, 1); // async
  164. return;
  165. }
  166.  
  167. if (document.addEventListener) {
  168. document.addEventListener('DOMContentLoaded', fireOnce, false);
  169.  
  170. // Fallback
  171. window.addEventListener('load', fireOnce, false);
  172.  
  173. } else if (document.attachEvent) {
  174. // IE
  175. document.attachEvent('onreadystatechange', function () {
  176. if (document.readyState === 'complete') {
  177. document.detachEvent('onreadystatechange', arguments.callee);
  178. fireOnce();
  179. }
  180. })
  181.  
  182. // Fallback
  183. window.attachEvent('onload', fireOnce);
  184.  
  185. // IE7/8
  186. if (document.documentElement.doScroll && window == window.top) {
  187. var tryScroll = function () {
  188. if (!document.body) { return; }
  189. try {
  190. document.documentElement.doScroll('left');
  191. fireOnce();
  192. } catch (e) {
  193. setTimeout(tryScroll, 1);
  194. }
  195. };
  196. tryScroll();
  197. }
  198. }
  199. },
  200.  
  201.  
  202. warn : function (msg) {
  203. if (window.console && window.console.warn) {
  204. window.console.warn(msg);
  205. }
  206. },
  207.  
  208.  
  209. preventDefault : function (e) {
  210. if (e.preventDefault) { e.preventDefault(); }
  211. e.returnValue = false;
  212. },
  213.  
  214.  
  215. captureTarget : function (target) {
  216. // IE
  217. if (target.setCapture) {
  218. jsc._capturedTarget = target;
  219. jsc._capturedTarget.setCapture();
  220. }
  221. },
  222.  
  223.  
  224. releaseTarget : function () {
  225. // IE
  226. if (jsc._capturedTarget) {
  227. jsc._capturedTarget.releaseCapture();
  228. jsc._capturedTarget = null;
  229. }
  230. },
  231.  
  232.  
  233. fireEvent : function (el, evnt) {
  234. if (!el) {
  235. return;
  236. }
  237. if (document.createEvent) {
  238. var ev = document.createEvent('HTMLEvents');
  239. ev.initEvent(evnt, true, true);
  240. el.dispatchEvent(ev);
  241. } else if (document.createEventObject) {
  242. var ev = document.createEventObject();
  243. el.fireEvent('on' + evnt, ev);
  244. } else if (el['on' + evnt]) { // alternatively use the traditional event model
  245. el['on' + evnt]();
  246. }
  247. },
  248.  
  249.  
  250. classNameToList : function (className) {
  251. return className.replace(/^\s+|\s+$/g, '').split(/\s+/);
  252. },
  253.  
  254.  
  255. // The className parameter (str) can only contain a single class name
  256. hasClass : function (elm, className) {
  257. if (!className) {
  258. return false;
  259. }
  260. return -1 != (' ' + elm.className.replace(/\s+/g, ' ') + ' ').indexOf(' ' + className + ' ');
  261. },
  262.  
  263.  
  264. // The className parameter (str) can contain multiple class names separated by whitespace
  265. setClass : function (elm, className) {
  266. var classList = jsc.classNameToList(className);
  267. for (var i = 0; i < classList.length; i += 1) {
  268. if (!jsc.hasClass(elm, classList[i])) {
  269. elm.className += (elm.className ? ' ' : '') + classList[i];
  270. }
  271. }
  272. },
  273.  
  274.  
  275. // The className parameter (str) can contain multiple class names separated by whitespace
  276. unsetClass : function (elm, className) {
  277. var classList = jsc.classNameToList(className);
  278. for (var i = 0; i < classList.length; i += 1) {
  279. var repl = new RegExp(
  280. '^\\s*' + classList[i] + '\\s*|' +
  281. '\\s*' + classList[i] + '\\s*$|' +
  282. '\\s+' + classList[i] + '(\\s+)',
  283. 'g'
  284. );
  285. elm.className = elm.className.replace(repl, '$1');
  286. }
  287. },
  288.  
  289.  
  290. getStyle : function (elm) {
  291. return window.getComputedStyle ? window.getComputedStyle(elm) : elm.currentStyle;
  292. },
  293.  
  294.  
  295. setStyle : (function () {
  296. var helper = document.createElement('div');
  297. var getSupportedProp = function (names) {
  298. for (var i = 0; i < names.length; i += 1) {
  299. if (names[i] in helper.style) {
  300. return names[i];
  301. }
  302. }
  303. };
  304. var props = {
  305. borderRadius: getSupportedProp(['borderRadius', 'MozBorderRadius', 'webkitBorderRadius']),
  306. boxShadow: getSupportedProp(['boxShadow', 'MozBoxShadow', 'webkitBoxShadow'])
  307. };
  308. return function (elm, prop, value) {
  309. switch (prop.toLowerCase()) {
  310. case 'opacity':
  311. var alphaOpacity = Math.round(parseFloat(value) * 100);
  312. elm.style.opacity = value;
  313. elm.style.filter = 'alpha(opacity=' + alphaOpacity + ')';
  314. break;
  315. default:
  316. elm.style[props[prop]] = value;
  317. break;
  318. }
  319. };
  320. })(),
  321.  
  322.  
  323. setBorderRadius : function (elm, value) {
  324. jsc.setStyle(elm, 'borderRadius', value || '0');
  325. },
  326.  
  327.  
  328. setBoxShadow : function (elm, value) {
  329. jsc.setStyle(elm, 'boxShadow', value || 'none');
  330. },
  331.  
  332.  
  333. getElementPos : function (e, relativeToViewport) {
  334. var x=0, y=0;
  335. var rect = e.getBoundingClientRect();
  336. x = rect.left;
  337. y = rect.top;
  338. if (!relativeToViewport) {
  339. var viewPos = jsc.getViewPos();
  340. x += viewPos[0];
  341. y += viewPos[1];
  342. }
  343. return [x, y];
  344. },
  345.  
  346.  
  347. getElementSize : function (e) {
  348. return [e.offsetWidth, e.offsetHeight];
  349. },
  350.  
  351.  
  352. // get pointer's X/Y coordinates relative to viewport
  353. getAbsPointerPos : function (e) {
  354. if (!e) { e = window.event; }
  355. var x = 0, y = 0;
  356. if (typeof e.changedTouches !== 'undefined' && e.changedTouches.length) {
  357. // touch devices
  358. x = e.changedTouches[0].clientX;
  359. y = e.changedTouches[0].clientY;
  360. } else if (typeof e.clientX === 'number') {
  361. x = e.clientX;
  362. y = e.clientY;
  363. }
  364. return { x: x, y: y };
  365. },
  366.  
  367.  
  368. // get pointer's X/Y coordinates relative to target element
  369. getRelPointerPos : function (e) {
  370. if (!e) { e = window.event; }
  371. var target = e.target || e.srcElement;
  372. var targetRect = target.getBoundingClientRect();
  373.  
  374. var x = 0, y = 0;
  375.  
  376. var clientX = 0, clientY = 0;
  377. if (typeof e.changedTouches !== 'undefined' && e.changedTouches.length) {
  378. // touch devices
  379. clientX = e.changedTouches[0].clientX;
  380. clientY = e.changedTouches[0].clientY;
  381. } else if (typeof e.clientX === 'number') {
  382. clientX = e.clientX;
  383. clientY = e.clientY;
  384. }
  385.  
  386. x = clientX - targetRect.left;
  387. y = clientY - targetRect.top;
  388. return { x: x, y: y };
  389. },
  390.  
  391.  
  392. getViewPos : function () {
  393. var doc = document.documentElement;
  394. return [
  395. (window.pageXOffset || doc.scrollLeft) - (doc.clientLeft || 0),
  396. (window.pageYOffset || doc.scrollTop) - (doc.clientTop || 0)
  397. ];
  398. },
  399.  
  400.  
  401. getViewSize : function () {
  402. var doc = document.documentElement;
  403. return [
  404. (window.innerWidth || doc.clientWidth),
  405. (window.innerHeight || doc.clientHeight),
  406. ];
  407. },
  408.  
  409.  
  410. redrawPosition : function () {
  411.  
  412. if (jsc.picker && jsc.picker.owner) {
  413. var thisObj = jsc.picker.owner;
  414.  
  415. var tp, vp;
  416.  
  417. if (thisObj.fixed) {
  418. // Fixed elements are positioned relative to viewport,
  419. // therefore we can ignore the scroll offset
  420. tp = jsc.getElementPos(thisObj.targetElement, true); // target pos
  421. vp = [0, 0]; // view pos
  422. } else {
  423. tp = jsc.getElementPos(thisObj.targetElement); // target pos
  424. vp = jsc.getViewPos(); // view pos
  425. }
  426.  
  427. var ts = jsc.getElementSize(thisObj.targetElement); // target size
  428. var vs = jsc.getViewSize(); // view size
  429. var ps = jsc.getPickerOuterDims(thisObj); // picker size
  430. var a, b, c;
  431. switch (thisObj.position.toLowerCase()) {
  432. case 'left': a=1; b=0; c=-1; break;
  433. case 'right':a=1; b=0; c=1; break;
  434. case 'top': a=0; b=1; c=-1; break;
  435. default: a=0; b=1; c=1; break;
  436. }
  437. var l = (ts[b]+ps[b])/2;
  438.  
  439. // compute picker position
  440. if (!thisObj.smartPosition) {
  441. var pp = [
  442. tp[a],
  443. tp[b]+ts[b]-l+l*c
  444. ];
  445. } else {
  446. var pp = [
  447. -vp[a]+tp[a]+ps[a] > vs[a] ?
  448. (-vp[a]+tp[a]+ts[a]/2 > vs[a]/2 && tp[a]+ts[a]-ps[a] >= 0 ? tp[a]+ts[a]-ps[a] : tp[a]) :
  449. tp[a],
  450. -vp[b]+tp[b]+ts[b]+ps[b]-l+l*c > vs[b] ?
  451. (-vp[b]+tp[b]+ts[b]/2 > vs[b]/2 && tp[b]+ts[b]-l-l*c >= 0 ? tp[b]+ts[b]-l-l*c : tp[b]+ts[b]-l+l*c) :
  452. (tp[b]+ts[b]-l+l*c >= 0 ? tp[b]+ts[b]-l+l*c : tp[b]+ts[b]-l-l*c)
  453. ];
  454. }
  455.  
  456. var x = pp[a];
  457. var y = pp[b];
  458. var positionValue = thisObj.fixed ? 'fixed' : 'absolute';
  459. var contractShadow =
  460. (pp[0] + ps[0] > tp[0] || pp[0] < tp[0] + ts[0]) &&
  461. (pp[1] + ps[1] < tp[1] + ts[1]);
  462.  
  463. jsc._drawPosition(thisObj, x, y, positionValue, contractShadow);
  464. }
  465. },
  466.  
  467.  
  468. _drawPosition : function (thisObj, x, y, positionValue, contractShadow) {
  469. var vShadow = contractShadow ? 0 : thisObj.shadowBlur; // px
  470.  
  471. jsc.picker.wrap.style.position = positionValue;
  472. jsc.picker.wrap.style.left = x + 'px';
  473. jsc.picker.wrap.style.top = y + 'px';
  474.  
  475. jsc.setBoxShadow(
  476. jsc.picker.boxS,
  477. thisObj.shadow ?
  478. new jsc.BoxShadow(0, vShadow, thisObj.shadowBlur, 0, thisObj.shadowColor) :
  479. null);
  480. },
  481.  
  482.  
  483. getPickerDims : function (thisObj) {
  484. var displaySlider = !!jsc.getSliderComponent(thisObj);
  485. var dims = [
  486. 2 * thisObj.insetWidth + 2 * thisObj.padding + thisObj.width +
  487. (displaySlider ? 2 * thisObj.insetWidth + jsc.getPadToSliderPadding(thisObj) + thisObj.sliderSize : 0),
  488. 2 * thisObj.insetWidth + 2 * thisObj.padding + thisObj.height +
  489. (thisObj.closable ? 2 * thisObj.insetWidth + thisObj.padding + thisObj.buttonHeight : 0)
  490. ];
  491. return dims;
  492. },
  493.  
  494.  
  495. getPickerOuterDims : function (thisObj) {
  496. var dims = jsc.getPickerDims(thisObj);
  497. return [
  498. dims[0] + 2 * thisObj.borderWidth,
  499. dims[1] + 2 * thisObj.borderWidth
  500. ];
  501. },
  502.  
  503.  
  504. getPadToSliderPadding : function (thisObj) {
  505. return Math.max(thisObj.padding, 1.5 * (2 * thisObj.pointerBorderWidth + thisObj.pointerThickness));
  506. },
  507.  
  508.  
  509. getPadYComponent : function (thisObj) {
  510. switch (thisObj.mode.charAt(1).toLowerCase()) {
  511. case 'v': return 'v'; break;
  512. }
  513. return 's';
  514. },
  515.  
  516.  
  517. getSliderComponent : function (thisObj) {
  518. if (thisObj.mode.length > 2) {
  519. switch (thisObj.mode.charAt(2).toLowerCase()) {
  520. case 's': return 's'; break;
  521. case 'v': return 'v'; break;
  522. }
  523. }
  524. return null;
  525. },
  526.  
  527.  
  528. onDocumentMouseDown : function (e) {
  529. if (!e) { e = window.event; }
  530. var target = e.target || e.srcElement;
  531.  
  532. if (target._jscLinkedInstance) {
  533. if (target._jscLinkedInstance.showOnClick) {
  534. target._jscLinkedInstance.show();
  535. }
  536. } else if (target._jscControlName) {
  537. jsc.onControlPointerStart(e, target, target._jscControlName, 'mouse');
  538. } else {
  539. // Mouse is outside the picker controls -> hide the color picker!
  540. if (jsc.picker && jsc.picker.owner) {
  541. jsc.picker.owner.hide();
  542. }
  543. }
  544. },
  545.  
  546.  
  547. onDocumentTouchStart : function (e) {
  548. if (!e) { e = window.event; }
  549. var target = e.target || e.srcElement;
  550.  
  551. if (target._jscLinkedInstance) {
  552. if (target._jscLinkedInstance.showOnClick) {
  553. target._jscLinkedInstance.show();
  554. }
  555. } else if (target._jscControlName) {
  556. jsc.onControlPointerStart(e, target, target._jscControlName, 'touch');
  557. } else {
  558. if (jsc.picker && jsc.picker.owner) {
  559. jsc.picker.owner.hide();
  560. }
  561. }
  562. },
  563.  
  564.  
  565. onWindowResize : function (e) {
  566. jsc.redrawPosition();
  567. },
  568.  
  569.  
  570. onParentScroll : function (e) {
  571. // hide the picker when one of the parent elements is scrolled
  572. if (jsc.picker && jsc.picker.owner) {
  573. jsc.picker.owner.hide();
  574. }
  575. },
  576.  
  577.  
  578. _pointerMoveEvent : {
  579. mouse: 'mousemove',
  580. touch: 'touchmove'
  581. },
  582. _pointerEndEvent : {
  583. mouse: 'mouseup',
  584. touch: 'touchend'
  585. },
  586.  
  587.  
  588. _pointerOrigin : null,
  589. _capturedTarget : null,
  590.  
  591.  
  592. onControlPointerStart : function (e, target, controlName, pointerType) {
  593. var thisObj = target._jscInstance;
  594.  
  595. jsc.preventDefault(e);
  596. jsc.captureTarget(target);
  597.  
  598. var registerDragEvents = function (doc, offset) {
  599. jsc.attachGroupEvent('drag', doc, jsc._pointerMoveEvent[pointerType],
  600. jsc.onDocumentPointerMove(e, target, controlName, pointerType, offset));
  601. jsc.attachGroupEvent('drag', doc, jsc._pointerEndEvent[pointerType],
  602. jsc.onDocumentPointerEnd(e, target, controlName, pointerType));
  603. };
  604.  
  605. registerDragEvents(document, [0, 0]);
  606.  
  607. if (window.parent && window.frameElement) {
  608. var rect = window.frameElement.getBoundingClientRect();
  609. var ofs = [-rect.left, -rect.top];
  610. registerDragEvents(window.parent.window.document, ofs);
  611. }
  612.  
  613. var abs = jsc.getAbsPointerPos(e);
  614. var rel = jsc.getRelPointerPos(e);
  615. jsc._pointerOrigin = {
  616. x: abs.x - rel.x,
  617. y: abs.y - rel.y
  618. };
  619.  
  620. switch (controlName) {
  621. case 'pad':
  622. // if the slider is at the bottom, move it up
  623. switch (jsc.getSliderComponent(thisObj)) {
  624. case 's': if (thisObj.hsv[1] === 0) { thisObj.fromHSV(null, 100, null); }; break;
  625. case 'v': if (thisObj.hsv[2] === 0) { thisObj.fromHSV(null, null, 100); }; break;
  626. }
  627. jsc.setPad(thisObj, e, 0, 0);
  628. break;
  629.  
  630. case 'sld':
  631. jsc.setSld(thisObj, e, 0);
  632. break;
  633. }
  634.  
  635. jsc.dispatchFineChange(thisObj);
  636. },
  637.  
  638.  
  639. onDocumentPointerMove : function (e, target, controlName, pointerType, offset) {
  640. return function (e) {
  641. var thisObj = target._jscInstance;
  642. switch (controlName) {
  643. case 'pad':
  644. if (!e) { e = window.event; }
  645. jsc.setPad(thisObj, e, offset[0], offset[1]);
  646. jsc.dispatchFineChange(thisObj);
  647. break;
  648.  
  649. case 'sld':
  650. if (!e) { e = window.event; }
  651. jsc.setSld(thisObj, e, offset[1]);
  652. jsc.dispatchFineChange(thisObj);
  653. break;
  654. }
  655. }
  656. },
  657.  
  658.  
  659. onDocumentPointerEnd : function (e, target, controlName, pointerType) {
  660. return function (e) {
  661. var thisObj = target._jscInstance;
  662. jsc.detachGroupEvents('drag');
  663. jsc.releaseTarget();
  664. // Always dispatch changes after detaching outstanding mouse handlers,
  665. // in case some user interaction will occur in user's onchange callback
  666. // that would intrude with current mouse events
  667. jsc.dispatchChange(thisObj);
  668. jsc.dispatchOnChange(thisObj);
  669. };
  670. },
  671.  
  672.  
  673. dispatchChange : function (thisObj) {
  674. if (thisObj.valueElement) {
  675. if (jsc.isElementType(thisObj.valueElement, 'input')) {
  676. jsc.fireEvent(thisObj.valueElement, 'change');
  677. }
  678. }
  679. },
  680.  
  681. dispatchOnChange : function(thisObj){
  682. if(thisObj.onChange){
  683. var callback;
  684. if (typeof thisObj.onChange === 'string'){
  685. callback = new Function (thisObj.onChange);
  686. } else {
  687. callback = thisObj.onChange;
  688. }
  689. callback.call(thisObj);
  690. }
  691. },
  692.  
  693.  
  694. dispatchFineChange : function (thisObj) {
  695. if (thisObj.onFineChange) {
  696. var callback;
  697. if (typeof thisObj.onFineChange === 'string') {
  698. callback = new Function (thisObj.onFineChange);
  699. } else {
  700. callback = thisObj.onFineChange;
  701. }
  702. callback.call(thisObj);
  703. }
  704. },
  705.  
  706.  
  707. setPad : function (thisObj, e, ofsX, ofsY) {
  708. var pointerAbs = jsc.getAbsPointerPos(e);
  709. var x = ofsX + pointerAbs.x - jsc._pointerOrigin.x - thisObj.padding - thisObj.insetWidth;
  710. var y = ofsY + pointerAbs.y - jsc._pointerOrigin.y - thisObj.padding - thisObj.insetWidth;
  711.  
  712. var xVal = x * (360 / (thisObj.width - 1));
  713. var yVal = 100 - (y * (100 / (thisObj.height - 1)));
  714.  
  715. switch (jsc.getPadYComponent(thisObj)) {
  716. case 's': thisObj.fromHSV(xVal, yVal, null, jsc.leaveSld); break;
  717. case 'v': thisObj.fromHSV(xVal, null, yVal, jsc.leaveSld); break;
  718. }
  719. },
  720.  
  721.  
  722. setSld : function (thisObj, e, ofsY) {
  723. var pointerAbs = jsc.getAbsPointerPos(e);
  724. var y = ofsY + pointerAbs.y - jsc._pointerOrigin.y - thisObj.padding - thisObj.insetWidth;
  725.  
  726. var yVal = 100 - (y * (100 / (thisObj.height - 1)));
  727.  
  728. switch (jsc.getSliderComponent(thisObj)) {
  729. case 's': thisObj.fromHSV(null, yVal, null, jsc.leavePad); break;
  730. case 'v': thisObj.fromHSV(null, null, yVal, jsc.leavePad); break;
  731. }
  732. },
  733.  
  734.  
  735. _vmlNS : 'jsc_vml_',
  736. _vmlCSS : 'jsc_vml_css_',
  737. _vmlReady : false,
  738.  
  739.  
  740. initVML : function () {
  741. if (!jsc._vmlReady) {
  742. // init VML namespace
  743. var doc = document;
  744. if (!doc.namespaces[jsc._vmlNS]) {
  745. doc.namespaces.add(jsc._vmlNS, 'urn:schemas-microsoft-com:vml');
  746. }
  747. if (!doc.styleSheets[jsc._vmlCSS]) {
  748. var tags = ['shape', 'shapetype', 'group', 'background', 'path', 'formulas', 'handles', 'fill', 'stroke', 'shadow', 'textbox', 'textpath', 'imagedata', 'line', 'polyline', 'curve', 'rect', 'roundrect', 'oval', 'arc', 'image'];
  749. var ss = doc.createStyleSheet();
  750. ss.owningElement.id = jsc._vmlCSS;
  751. for (var i = 0; i < tags.length; i += 1) {
  752. ss.addRule(jsc._vmlNS + '\\:' + tags[i], 'behavior:url(#default#VML);');
  753. }
  754. }
  755. jsc._vmlReady = true;
  756. }
  757. },
  758.  
  759.  
  760. createPalette : function () {
  761.  
  762. var paletteObj = {
  763. elm: null,
  764. draw: null
  765. };
  766.  
  767. if (jsc.isCanvasSupported) {
  768. // Canvas implementation for modern browsers
  769.  
  770. var canvas = document.createElement('canvas');
  771. var ctx = canvas.getContext('2d');
  772.  
  773. var drawFunc = function (width, height, type) {
  774. canvas.width = width;
  775. canvas.height = height;
  776.  
  777. ctx.clearRect(0, 0, canvas.width, canvas.height);
  778.  
  779. var hGrad = ctx.createLinearGradient(0, 0, canvas.width, 0);
  780. hGrad.addColorStop(0 / 6, '#F00');
  781. hGrad.addColorStop(1 / 6, '#FF0');
  782. hGrad.addColorStop(2 / 6, '#0F0');
  783. hGrad.addColorStop(3 / 6, '#0FF');
  784. hGrad.addColorStop(4 / 6, '#00F');
  785. hGrad.addColorStop(5 / 6, '#F0F');
  786. hGrad.addColorStop(6 / 6, '#F00');
  787.  
  788. ctx.fillStyle = hGrad;
  789. ctx.fillRect(0, 0, canvas.width, canvas.height);
  790.  
  791. var vGrad = ctx.createLinearGradient(0, 0, 0, canvas.height);
  792. switch (type.toLowerCase()) {
  793. case 's':
  794. vGrad.addColorStop(0, 'rgba(255,255,255,0)');
  795. vGrad.addColorStop(1, 'rgba(255,255,255,1)');
  796. break;
  797. case 'v':
  798. vGrad.addColorStop(0, 'rgba(0,0,0,0)');
  799. vGrad.addColorStop(1, 'rgba(0,0,0,1)');
  800. break;
  801. }
  802. ctx.fillStyle = vGrad;
  803. ctx.fillRect(0, 0, canvas.width, canvas.height);
  804. };
  805.  
  806. paletteObj.elm = canvas;
  807. paletteObj.draw = drawFunc;
  808.  
  809. } else {
  810. // VML fallback for IE 7 and 8
  811.  
  812. jsc.initVML();
  813.  
  814. var vmlContainer = document.createElement('div');
  815. vmlContainer.style.position = 'relative';
  816. vmlContainer.style.overflow = 'hidden';
  817.  
  818. var hGrad = document.createElement(jsc._vmlNS + ':fill');
  819. hGrad.type = 'gradient';
  820. hGrad.method = 'linear';
  821. hGrad.angle = '90';
  822. hGrad.colors = '16.67% #F0F, 33.33% #00F, 50% #0FF, 66.67% #0F0, 83.33% #FF0'
  823.  
  824. var hRect = document.createElement(jsc._vmlNS + ':rect');
  825. hRect.style.position = 'absolute';
  826. hRect.style.left = -1 + 'px';
  827. hRect.style.top = -1 + 'px';
  828. hRect.stroked = false;
  829. hRect.appendChild(hGrad);
  830. vmlContainer.appendChild(hRect);
  831.  
  832. var vGrad = document.createElement(jsc._vmlNS + ':fill');
  833. vGrad.type = 'gradient';
  834. vGrad.method = 'linear';
  835. vGrad.angle = '180';
  836. vGrad.opacity = '0';
  837.  
  838. var vRect = document.createElement(jsc._vmlNS + ':rect');
  839. vRect.style.position = 'absolute';
  840. vRect.style.left = -1 + 'px';
  841. vRect.style.top = -1 + 'px';
  842. vRect.stroked = false;
  843. vRect.appendChild(vGrad);
  844. vmlContainer.appendChild(vRect);
  845.  
  846. var drawFunc = function (width, height, type) {
  847. vmlContainer.style.width = width + 'px';
  848. vmlContainer.style.height = height + 'px';
  849.  
  850. hRect.style.width =
  851. vRect.style.width =
  852. (width + 1) + 'px';
  853. hRect.style.height =
  854. vRect.style.height =
  855. (height + 1) + 'px';
  856.  
  857. // Colors must be specified during every redraw, otherwise IE won't display
  858. // a full gradient during a subsequential redraw
  859. hGrad.color = '#F00';
  860. hGrad.color2 = '#F00';
  861.  
  862. switch (type.toLowerCase()) {
  863. case 's':
  864. vGrad.color = vGrad.color2 = '#FFF';
  865. break;
  866. case 'v':
  867. vGrad.color = vGrad.color2 = '#000';
  868. break;
  869. }
  870. };
  871. paletteObj.elm = vmlContainer;
  872. paletteObj.draw = drawFunc;
  873. }
  874.  
  875. return paletteObj;
  876. },
  877.  
  878.  
  879. createSliderGradient : function () {
  880.  
  881. var sliderObj = {
  882. elm: null,
  883. draw: null
  884. };
  885.  
  886. if (jsc.isCanvasSupported) {
  887. // Canvas implementation for modern browsers
  888.  
  889. var canvas = document.createElement('canvas');
  890. var ctx = canvas.getContext('2d');
  891.  
  892. var drawFunc = function (width, height, color1, color2) {
  893. canvas.width = width;
  894. canvas.height = height;
  895.  
  896. ctx.clearRect(0, 0, canvas.width, canvas.height);
  897.  
  898. var grad = ctx.createLinearGradient(0, 0, 0, canvas.height);
  899. grad.addColorStop(0, color1);
  900. grad.addColorStop(1, color2);
  901.  
  902. ctx.fillStyle = grad;
  903. ctx.fillRect(0, 0, canvas.width, canvas.height);
  904. };
  905.  
  906. sliderObj.elm = canvas;
  907. sliderObj.draw = drawFunc;
  908.  
  909. } else {
  910. // VML fallback for IE 7 and 8
  911.  
  912. jsc.initVML();
  913.  
  914. var vmlContainer = document.createElement('div');
  915. vmlContainer.style.position = 'relative';
  916. vmlContainer.style.overflow = 'hidden';
  917.  
  918. var grad = document.createElement(jsc._vmlNS + ':fill');
  919. grad.type = 'gradient';
  920. grad.method = 'linear';
  921. grad.angle = '180';
  922.  
  923. var rect = document.createElement(jsc._vmlNS + ':rect');
  924. rect.style.position = 'absolute';
  925. rect.style.left = -1 + 'px';
  926. rect.style.top = -1 + 'px';
  927. rect.stroked = false;
  928. rect.appendChild(grad);
  929. vmlContainer.appendChild(rect);
  930.  
  931. var drawFunc = function (width, height, color1, color2) {
  932. vmlContainer.style.width = width + 'px';
  933. vmlContainer.style.height = height + 'px';
  934.  
  935. rect.style.width = (width + 1) + 'px';
  936. rect.style.height = (height + 1) + 'px';
  937.  
  938. grad.color = color1;
  939. grad.color2 = color2;
  940. };
  941. sliderObj.elm = vmlContainer;
  942. sliderObj.draw = drawFunc;
  943. }
  944.  
  945. return sliderObj;
  946. },
  947.  
  948.  
  949. leaveValue : 1<<0,
  950. leaveStyle : 1<<1,
  951. leavePad : 1<<2,
  952. leaveSld : 1<<3,
  953.  
  954.  
  955. BoxShadow : (function () {
  956. var BoxShadow = function (hShadow, vShadow, blur, spread, color, inset) {
  957. this.hShadow = hShadow;
  958. this.vShadow = vShadow;
  959. this.blur = blur;
  960. this.spread = spread;
  961. this.color = color;
  962. this.inset = !!inset;
  963. };
  964.  
  965. BoxShadow.prototype.toString = function () {
  966. var vals = [
  967. Math.round(this.hShadow) + 'px',
  968. Math.round(this.vShadow) + 'px',
  969. Math.round(this.blur) + 'px',
  970. Math.round(this.spread) + 'px',
  971. this.color
  972. ];
  973. if (this.inset) {
  974. vals.push('inset');
  975. }
  976. return vals.join(' ');
  977. };
  978.  
  979. return BoxShadow;
  980. })(),
  981.  
  982.  
  983. //
  984. // Usage:
  985. // var myColor = new jscolor(<targetElement> [, <options>])
  986. //
  987.  
  988. jscolor : function (targetElement, options) {
  989.  
  990. // General options
  991. //
  992. this.value = null; // initial HEX color. To change it later, use methods fromString(), fromHSV() and fromRGB()
  993. this.valueElement = targetElement; // element that will be used to display and input the color code
  994. this.styleElement = targetElement; // element that will preview the picked color using CSS backgroundColor
  995. this.required = true; // whether the associated text <input> can be left empty
  996. this.refine = true; // whether to refine the entered color code (e.g. uppercase it and remove whitespace)
  997. this.hash = false; // whether to prefix the HEX color code with # symbol
  998. this.uppercase = true; // whether to uppercase the color code
  999. this.onFineChange = null; // called instantly every time the color changes (value can be either a function or a string with javascript code)
  1000. this.onChange = null;
  1001. this.activeClass = 'jscolor-active'; // class to be set to the target element when a picker window is open on it
  1002. this.minS = 0; // min allowed saturation (0 - 100)
  1003. this.maxS = 100; // max allowed saturation (0 - 100)
  1004. this.minV = 0; // min allowed value (brightness) (0 - 100)
  1005. this.maxV = 100; // max allowed value (brightness) (0 - 100)
  1006.  
  1007. // Accessing the picked color
  1008. //
  1009. this.hsv = [0, 0, 100]; // read-only [0-360, 0-100, 0-100]
  1010. this.rgb = [255, 255, 255]; // read-only [0-255, 0-255, 0-255]
  1011.  
  1012. // Color Picker options
  1013. //
  1014. this.width = 181; // width of color palette (in px)
  1015. this.height = 101; // height of color palette (in px)
  1016. this.showOnClick = true; // whether to display the color picker when user clicks on its target element
  1017. this.mode = 'HSV'; // HSV | HVS | HS | HV - layout of the color picker controls
  1018. this.position = 'bottom'; // left | right | top | bottom - position relative to the target element
  1019. this.smartPosition = true; // automatically change picker position when there is not enough space for it
  1020. this.sliderSize = 16; // px
  1021. this.crossSize = 8; // px
  1022. this.closable = false; // whether to display the Close button
  1023. this.closeText = 'Close';
  1024. this.buttonColor = '#000000'; // CSS color
  1025. this.buttonHeight = 18; // px
  1026. this.padding = 12; // px
  1027. this.backgroundColor = '#FFFFFF'; // CSS color
  1028. this.borderWidth = 1; // px
  1029. this.borderColor = '#BBBBBB'; // CSS color
  1030. this.borderRadius = 8; // px
  1031. this.insetWidth = 1; // px
  1032. this.insetColor = '#BBBBBB'; // CSS color
  1033. this.shadow = true; // whether to display shadow
  1034. this.shadowBlur = 15; // px
  1035. this.shadowColor = 'rgba(0,0,0,0.2)'; // CSS color
  1036. this.pointerColor = '#4C4C4C'; // px
  1037. this.pointerBorderColor = '#FFFFFF'; // px
  1038. this.pointerBorderWidth = 1; // px
  1039. this.pointerThickness = 2; // px
  1040. this.zIndex = 1000;
  1041. this.container = null; // where to append the color picker (BODY element by default)
  1042.  
  1043.  
  1044. for (var opt in options) {
  1045. if (options.hasOwnProperty(opt)) {
  1046. this[opt] = options[opt];
  1047. }
  1048. }
  1049.  
  1050.  
  1051. this.hide = function () {
  1052. if (isPickerOwner()) {
  1053. detachPicker();
  1054. }
  1055. };
  1056.  
  1057.  
  1058. this.show = function () {
  1059. drawPicker();
  1060. };
  1061.  
  1062.  
  1063. this.redraw = function () {
  1064. if (isPickerOwner()) {
  1065. drawPicker();
  1066. }
  1067. };
  1068.  
  1069.  
  1070. this.importColor = function () {
  1071. if (!this.valueElement) {
  1072. this.exportColor();
  1073. } else {
  1074. if (jsc.isElementType(this.valueElement, 'input')) {
  1075. if (!this.refine) {
  1076. if (!this.fromString(this.valueElement.value, jsc.leaveValue)) {
  1077. if (this.styleElement) {
  1078. this.styleElement.style.backgroundImage = this.styleElement._jscOrigStyle.backgroundImage;
  1079. this.styleElement.style.backgroundColor = this.styleElement._jscOrigStyle.backgroundColor;
  1080. this.styleElement.style.color = this.styleElement._jscOrigStyle.color;
  1081. }
  1082. this.exportColor(jsc.leaveValue | jsc.leaveStyle);
  1083. }
  1084. } else if (!this.required && /^\s*$/.test(this.valueElement.value)) {
  1085. this.valueElement.value = '';
  1086. if (this.styleElement) {
  1087. this.styleElement.style.backgroundImage = this.styleElement._jscOrigStyle.backgroundImage;
  1088. this.styleElement.style.backgroundColor = this.styleElement._jscOrigStyle.backgroundColor;
  1089. this.styleElement.style.color = this.styleElement._jscOrigStyle.color;
  1090. }
  1091. this.exportColor(jsc.leaveValue | jsc.leaveStyle);
  1092.  
  1093. } else if (this.fromString(this.valueElement.value)) {
  1094. // managed to import color successfully from the value -> OK, don't do anything
  1095. } else {
  1096. this.exportColor();
  1097. }
  1098. } else {
  1099. // not an input element -> doesn't have any value
  1100. this.exportColor();
  1101. }
  1102. }
  1103. };
  1104.  
  1105.  
  1106. this.exportColor = function (flags) {
  1107. if (!(flags & jsc.leaveValue) && this.valueElement) {
  1108. var value = this.toString();
  1109. if (this.uppercase) { value = value.toUpperCase(); }
  1110. if (this.hash) { value = '#' + value; }
  1111.  
  1112. if (jsc.isElementType(this.valueElement, 'input')) {
  1113. this.valueElement.value = value;
  1114. } else {
  1115. this.valueElement.innerHTML = value;
  1116. }
  1117. }
  1118. if (!(flags & jsc.leaveStyle)) {
  1119. if (this.styleElement) {
  1120. this.styleElement.style.backgroundImage = 'none';
  1121. this.styleElement.style.backgroundColor = '#' + this.toString();
  1122. this.styleElement.style.color = this.isLight() ? '#000' : '#FFF';
  1123. }
  1124. }
  1125. if (!(flags & jsc.leavePad) && isPickerOwner()) {
  1126. redrawPad();
  1127. }
  1128. if (!(flags & jsc.leaveSld) && isPickerOwner()) {
  1129. redrawSld();
  1130. }
  1131. };
  1132.  
  1133.  
  1134. // h: 0-360
  1135. // s: 0-100
  1136. // v: 0-100
  1137. //
  1138. this.fromHSV = function (h, s, v, flags) { // null = don't change
  1139. if (h !== null) {
  1140. if (isNaN(h)) { return false; }
  1141. h = Math.max(0, Math.min(360, h));
  1142. }
  1143. if (s !== null) {
  1144. if (isNaN(s)) { return false; }
  1145. s = Math.max(0, Math.min(100, this.maxS, s), this.minS);
  1146. }
  1147. if (v !== null) {
  1148. if (isNaN(v)) { return false; }
  1149. v = Math.max(0, Math.min(100, this.maxV, v), this.minV);
  1150. }
  1151.  
  1152. this.rgb = HSV_RGB(
  1153. h===null ? this.hsv[0] : (this.hsv[0]=h),
  1154. s===null ? this.hsv[1] : (this.hsv[1]=s),
  1155. v===null ? this.hsv[2] : (this.hsv[2]=v)
  1156. );
  1157.  
  1158. this.exportColor(flags);
  1159. };
  1160.  
  1161.  
  1162. // r: 0-255
  1163. // g: 0-255
  1164. // b: 0-255
  1165. //
  1166. this.fromRGB = function (r, g, b, flags) { // null = don't change
  1167. if (r !== null) {
  1168. if (isNaN(r)) { return false; }
  1169. r = Math.max(0, Math.min(255, r));
  1170. }
  1171. if (g !== null) {
  1172. if (isNaN(g)) { return false; }
  1173. g = Math.max(0, Math.min(255, g));
  1174. }
  1175. if (b !== null) {
  1176. if (isNaN(b)) { return false; }
  1177. b = Math.max(0, Math.min(255, b));
  1178. }
  1179.  
  1180. var hsv = RGB_HSV(
  1181. r===null ? this.rgb[0] : r,
  1182. g===null ? this.rgb[1] : g,
  1183. b===null ? this.rgb[2] : b
  1184. );
  1185. if (hsv[0] !== null) {
  1186. this.hsv[0] = Math.max(0, Math.min(360, hsv[0]));
  1187. }
  1188. if (hsv[2] !== 0) {
  1189. this.hsv[1] = hsv[1]===null ? null : Math.max(0, this.minS, Math.min(100, this.maxS, hsv[1]));
  1190. }
  1191. this.hsv[2] = hsv[2]===null ? null : Math.max(0, this.minV, Math.min(100, this.maxV, hsv[2]));
  1192.  
  1193. // update RGB according to final HSV, as some values might be trimmed
  1194. var rgb = HSV_RGB(this.hsv[0], this.hsv[1], this.hsv[2]);
  1195. this.rgb[0] = rgb[0];
  1196. this.rgb[1] = rgb[1];
  1197. this.rgb[2] = rgb[2];
  1198.  
  1199. this.exportColor(flags);
  1200. };
  1201.  
  1202.  
  1203. this.fromString = function (str, flags) {
  1204. var m;
  1205. if (m = str.match(/^\W*([0-9A-F]{3}([0-9A-F]{3})?)\W*$/i)) {
  1206. // HEX notation
  1207. //
  1208.  
  1209. if (m[1].length === 6) {
  1210. // 6-char notation
  1211. this.fromRGB(
  1212. parseInt(m[1].substr(0,2),16),
  1213. parseInt(m[1].substr(2,2),16),
  1214. parseInt(m[1].substr(4,2),16),
  1215. flags
  1216. );
  1217. } else {
  1218. // 3-char notation
  1219. this.fromRGB(
  1220. parseInt(m[1].charAt(0) + m[1].charAt(0),16),
  1221. parseInt(m[1].charAt(1) + m[1].charAt(1),16),
  1222. parseInt(m[1].charAt(2) + m[1].charAt(2),16),
  1223. flags
  1224. );
  1225. }
  1226. return true;
  1227.  
  1228. } else if (m = str.match(/^\W*rgba?\(([^)]*)\)\W*$/i)) {
  1229. var params = m[1].split(',');
  1230. var re = /^\s*(\d*)(\.\d+)?\s*$/;
  1231. var mR, mG, mB;
  1232. if (
  1233. params.length >= 3 &&
  1234. (mR = params[0].match(re)) &&
  1235. (mG = params[1].match(re)) &&
  1236. (mB = params[2].match(re))
  1237. ) {
  1238. var r = parseFloat((mR[1] || '0') + (mR[2] || ''));
  1239. var g = parseFloat((mG[1] || '0') + (mG[2] || ''));
  1240. var b = parseFloat((mB[1] || '0') + (mB[2] || ''));
  1241. this.fromRGB(r, g, b, flags);
  1242. return true;
  1243. }
  1244. }
  1245. return false;
  1246. };
  1247.  
  1248.  
  1249. this.toString = function () {
  1250. return (
  1251. (0x100 | Math.round(this.rgb[0])).toString(16).substr(1) +
  1252. (0x100 | Math.round(this.rgb[1])).toString(16).substr(1) +
  1253. (0x100 | Math.round(this.rgb[2])).toString(16).substr(1)
  1254. );
  1255. };
  1256.  
  1257.  
  1258. this.toHEXString = function () {
  1259. return '#' + this.toString().toUpperCase();
  1260. };
  1261.  
  1262.  
  1263. this.toRGBString = function () {
  1264. return ('rgb(' +
  1265. Math.round(this.rgb[0]) + ',' +
  1266. Math.round(this.rgb[1]) + ',' +
  1267. Math.round(this.rgb[2]) + ')'
  1268. );
  1269. };
  1270.  
  1271.  
  1272. this.isLight = function () {
  1273. return (
  1274. 0.213 * this.rgb[0] +
  1275. 0.715 * this.rgb[1] +
  1276. 0.072 * this.rgb[2] >
  1277. 255 / 2
  1278. );
  1279. };
  1280.  
  1281.  
  1282. this._processParentElementsInDOM = function () {
  1283. if (this._linkedElementsProcessed) { return; }
  1284. this._linkedElementsProcessed = true;
  1285.  
  1286. var elm = this.targetElement;
  1287. do {
  1288. // If the target element or one of its parent nodes has fixed position,
  1289. // then use fixed positioning instead
  1290. //
  1291. // Note: In Firefox, getComputedStyle returns null in a hidden iframe,
  1292. // that's why we need to check if the returned style object is non-empty
  1293. var currStyle = jsc.getStyle(elm);
  1294. if (currStyle && currStyle.position.toLowerCase() === 'fixed') {
  1295. this.fixed = true;
  1296. }
  1297.  
  1298. if (elm !== this.targetElement) {
  1299. // Ensure to attach onParentScroll only once to each parent element
  1300. // (multiple targetElements can share the same parent nodes)
  1301. //
  1302. // Note: It's not just offsetParents that can be scrollable,
  1303. // that's why we loop through all parent nodes
  1304. if (!elm._jscEventsAttached) {
  1305. jsc.attachEvent(elm, 'scroll', jsc.onParentScroll);
  1306. elm._jscEventsAttached = true;
  1307. }
  1308. }
  1309. } while ((elm = elm.parentNode) && !jsc.isElementType(elm, 'body'));
  1310. };
  1311.  
  1312.  
  1313. // r: 0-255
  1314. // g: 0-255
  1315. // b: 0-255
  1316. //
  1317. // returns: [ 0-360, 0-100, 0-100 ]
  1318. //
  1319. function RGB_HSV (r, g, b) {
  1320. r /= 255;
  1321. g /= 255;
  1322. b /= 255;
  1323. var n = Math.min(Math.min(r,g),b);
  1324. var v = Math.max(Math.max(r,g),b);
  1325. var m = v - n;
  1326. if (m === 0) { return [ null, 0, 100 * v ]; }
  1327. var h = r===n ? 3+(b-g)/m : (g===n ? 5+(r-b)/m : 1+(g-r)/m);
  1328. return [
  1329. 60 * (h===6?0:h),
  1330. 100 * (m/v),
  1331. 100 * v
  1332. ];
  1333. }
  1334.  
  1335.  
  1336. // h: 0-360
  1337. // s: 0-100
  1338. // v: 0-100
  1339. //
  1340. // returns: [ 0-255, 0-255, 0-255 ]
  1341. //
  1342. function HSV_RGB (h, s, v) {
  1343. var u = 255 * (v / 100);
  1344.  
  1345. if (h === null) {
  1346. return [ u, u, u ];
  1347. }
  1348.  
  1349. h /= 60;
  1350. s /= 100;
  1351.  
  1352. var i = Math.floor(h);
  1353. var f = i%2 ? h-i : 1-(h-i);
  1354. var m = u * (1 - s);
  1355. var n = u * (1 - s * f);
  1356. switch (i) {
  1357. case 6:
  1358. case 0: return [u,n,m];
  1359. case 1: return [n,u,m];
  1360. case 2: return [m,u,n];
  1361. case 3: return [m,n,u];
  1362. case 4: return [n,m,u];
  1363. case 5: return [u,m,n];
  1364. }
  1365. }
  1366.  
  1367.  
  1368. function detachPicker () {
  1369. jsc.unsetClass(THIS.targetElement, THIS.activeClass);
  1370. jsc.picker.wrap.parentNode.removeChild(jsc.picker.wrap);
  1371. delete jsc.picker.owner;
  1372. }
  1373.  
  1374.  
  1375. function drawPicker () {
  1376.  
  1377. // At this point, when drawing the picker, we know what the parent elements are
  1378. // and we can do all related DOM operations, such as registering events on them
  1379. // or checking their positioning
  1380. THIS._processParentElementsInDOM();
  1381.  
  1382. if (!jsc.picker) {
  1383. jsc.picker = {
  1384. owner: null,
  1385. wrap : document.createElement('div'),
  1386. box : document.createElement('div'),
  1387. boxS : document.createElement('div'), // shadow area
  1388. boxB : document.createElement('div'), // border
  1389. pad : document.createElement('div'),
  1390. padB : document.createElement('div'), // border
  1391. padM : document.createElement('div'), // mouse/touch area
  1392. padPal : jsc.createPalette(),
  1393. cross : document.createElement('div'),
  1394. crossBY : document.createElement('div'), // border Y
  1395. crossBX : document.createElement('div'), // border X
  1396. crossLY : document.createElement('div'), // line Y
  1397. crossLX : document.createElement('div'), // line X
  1398. sld : document.createElement('div'),
  1399. sldB : document.createElement('div'), // border
  1400. sldM : document.createElement('div'), // mouse/touch area
  1401. sldGrad : jsc.createSliderGradient(),
  1402. sldPtrS : document.createElement('div'), // slider pointer spacer
  1403. sldPtrIB : document.createElement('div'), // slider pointer inner border
  1404. sldPtrMB : document.createElement('div'), // slider pointer middle border
  1405. sldPtrOB : document.createElement('div'), // slider pointer outer border
  1406. btn : document.createElement('div'),
  1407. btnT : document.createElement('span') // text
  1408. };
  1409.  
  1410. jsc.picker.pad.appendChild(jsc.picker.padPal.elm);
  1411. jsc.picker.padB.appendChild(jsc.picker.pad);
  1412. jsc.picker.cross.appendChild(jsc.picker.crossBY);
  1413. jsc.picker.cross.appendChild(jsc.picker.crossBX);
  1414. jsc.picker.cross.appendChild(jsc.picker.crossLY);
  1415. jsc.picker.cross.appendChild(jsc.picker.crossLX);
  1416. jsc.picker.padB.appendChild(jsc.picker.cross);
  1417. jsc.picker.box.appendChild(jsc.picker.padB);
  1418. jsc.picker.box.appendChild(jsc.picker.padM);
  1419.  
  1420. jsc.picker.sld.appendChild(jsc.picker.sldGrad.elm);
  1421. jsc.picker.sldB.appendChild(jsc.picker.sld);
  1422. jsc.picker.sldB.appendChild(jsc.picker.sldPtrOB);
  1423. jsc.picker.sldPtrOB.appendChild(jsc.picker.sldPtrMB);
  1424. jsc.picker.sldPtrMB.appendChild(jsc.picker.sldPtrIB);
  1425. jsc.picker.sldPtrIB.appendChild(jsc.picker.sldPtrS);
  1426. jsc.picker.box.appendChild(jsc.picker.sldB);
  1427. jsc.picker.box.appendChild(jsc.picker.sldM);
  1428.  
  1429. jsc.picker.btn.appendChild(jsc.picker.btnT);
  1430. jsc.picker.box.appendChild(jsc.picker.btn);
  1431.  
  1432. jsc.picker.boxB.appendChild(jsc.picker.box);
  1433. jsc.picker.wrap.appendChild(jsc.picker.boxS);
  1434. jsc.picker.wrap.appendChild(jsc.picker.boxB);
  1435. }
  1436.  
  1437. var p = jsc.picker;
  1438.  
  1439. var displaySlider = !!jsc.getSliderComponent(THIS);
  1440. var dims = jsc.getPickerDims(THIS);
  1441. var crossOuterSize = (2 * THIS.pointerBorderWidth + THIS.pointerThickness + 2 * THIS.crossSize);
  1442. var padToSliderPadding = jsc.getPadToSliderPadding(THIS);
  1443. var borderRadius = Math.min(
  1444. THIS.borderRadius,
  1445. Math.round(THIS.padding * Math.PI)); // px
  1446. var padCursor = 'crosshair';
  1447.  
  1448. // wrap
  1449. p.wrap.style.clear = 'both';
  1450. p.wrap.style.width = (dims[0] + 2 * THIS.borderWidth) + 'px';
  1451. p.wrap.style.height = (dims[1] + 2 * THIS.borderWidth) + 'px';
  1452. p.wrap.style.zIndex = THIS.zIndex;
  1453.  
  1454. // picker
  1455. p.box.style.width = dims[0] + 'px';
  1456. p.box.style.height = dims[1] + 'px';
  1457.  
  1458. p.boxS.style.position = 'absolute';
  1459. p.boxS.style.left = '0';
  1460. p.boxS.style.top = '0';
  1461. p.boxS.style.width = '100%';
  1462. p.boxS.style.height = '100%';
  1463. jsc.setBorderRadius(p.boxS, borderRadius + 'px');
  1464.  
  1465. // picker border
  1466. p.boxB.style.position = 'relative';
  1467. p.boxB.style.border = THIS.borderWidth + 'px solid';
  1468. p.boxB.style.borderColor = THIS.borderColor;
  1469. p.boxB.style.background = THIS.backgroundColor;
  1470. jsc.setBorderRadius(p.boxB, borderRadius + 'px');
  1471.  
  1472. // IE hack:
  1473. // If the element is transparent, IE will trigger the event on the elements under it,
  1474. // e.g. on Canvas or on elements with border
  1475. p.padM.style.background =
  1476. p.sldM.style.background =
  1477. '#FFF';
  1478. jsc.setStyle(p.padM, 'opacity', '0');
  1479. jsc.setStyle(p.sldM, 'opacity', '0');
  1480.  
  1481. // pad
  1482. p.pad.style.position = 'relative';
  1483. p.pad.style.width = THIS.width + 'px';
  1484. p.pad.style.height = THIS.height + 'px';
  1485.  
  1486. // pad palettes (HSV and HVS)
  1487. p.padPal.draw(THIS.width, THIS.height, jsc.getPadYComponent(THIS));
  1488.  
  1489. // pad border
  1490. p.padB.style.position = 'absolute';
  1491. p.padB.style.left = THIS.padding + 'px';
  1492. p.padB.style.top = THIS.padding + 'px';
  1493. p.padB.style.border = THIS.insetWidth + 'px solid';
  1494. p.padB.style.borderColor = THIS.insetColor;
  1495.  
  1496. // pad mouse area
  1497. p.padM._jscInstance = THIS;
  1498. p.padM._jscControlName = 'pad';
  1499. p.padM.style.position = 'absolute';
  1500. p.padM.style.left = '0';
  1501. p.padM.style.top = '0';
  1502. p.padM.style.width = (THIS.padding + 2 * THIS.insetWidth + THIS.width + padToSliderPadding / 2) + 'px';
  1503. p.padM.style.height = dims[1] + 'px';
  1504. p.padM.style.cursor = padCursor;
  1505.  
  1506. // pad cross
  1507. p.cross.style.position = 'absolute';
  1508. p.cross.style.left =
  1509. p.cross.style.top =
  1510. '0';
  1511. p.cross.style.width =
  1512. p.cross.style.height =
  1513. crossOuterSize + 'px';
  1514.  
  1515. // pad cross border Y and X
  1516. p.crossBY.style.position =
  1517. p.crossBX.style.position =
  1518. 'absolute';
  1519. p.crossBY.style.background =
  1520. p.crossBX.style.background =
  1521. THIS.pointerBorderColor;
  1522. p.crossBY.style.width =
  1523. p.crossBX.style.height =
  1524. (2 * THIS.pointerBorderWidth + THIS.pointerThickness) + 'px';
  1525. p.crossBY.style.height =
  1526. p.crossBX.style.width =
  1527. crossOuterSize + 'px';
  1528. p.crossBY.style.left =
  1529. p.crossBX.style.top =
  1530. (Math.floor(crossOuterSize / 2) - Math.floor(THIS.pointerThickness / 2) - THIS.pointerBorderWidth) + 'px';
  1531. p.crossBY.style.top =
  1532. p.crossBX.style.left =
  1533. '0';
  1534.  
  1535. // pad cross line Y and X
  1536. p.crossLY.style.position =
  1537. p.crossLX.style.position =
  1538. 'absolute';
  1539. p.crossLY.style.background =
  1540. p.crossLX.style.background =
  1541. THIS.pointerColor;
  1542. p.crossLY.style.height =
  1543. p.crossLX.style.width =
  1544. (crossOuterSize - 2 * THIS.pointerBorderWidth) + 'px';
  1545. p.crossLY.style.width =
  1546. p.crossLX.style.height =
  1547. THIS.pointerThickness + 'px';
  1548. p.crossLY.style.left =
  1549. p.crossLX.style.top =
  1550. (Math.floor(crossOuterSize / 2) - Math.floor(THIS.pointerThickness / 2)) + 'px';
  1551. p.crossLY.style.top =
  1552. p.crossLX.style.left =
  1553. THIS.pointerBorderWidth + 'px';
  1554.  
  1555. // slider
  1556. p.sld.style.overflow = 'hidden';
  1557. p.sld.style.width = THIS.sliderSize + 'px';
  1558. p.sld.style.height = THIS.height + 'px';
  1559.  
  1560. // slider gradient
  1561. p.sldGrad.draw(THIS.sliderSize, THIS.height, '#000', '#000');
  1562.  
  1563. // slider border
  1564. p.sldB.style.display = displaySlider ? 'block' : 'none';
  1565. p.sldB.style.position = 'absolute';
  1566. p.sldB.style.right = THIS.padding + 'px';
  1567. p.sldB.style.top = THIS.padding + 'px';
  1568. p.sldB.style.border = THIS.insetWidth + 'px solid';
  1569. p.sldB.style.borderColor = THIS.insetColor;
  1570.  
  1571. // slider mouse area
  1572. p.sldM._jscInstance = THIS;
  1573. p.sldM._jscControlName = 'sld';
  1574. p.sldM.style.display = displaySlider ? 'block' : 'none';
  1575. p.sldM.style.position = 'absolute';
  1576. p.sldM.style.right = '0';
  1577. p.sldM.style.top = '0';
  1578. p.sldM.style.width = (THIS.sliderSize + padToSliderPadding / 2 + THIS.padding + 2 * THIS.insetWidth) + 'px';
  1579. p.sldM.style.height = dims[1] + 'px';
  1580. p.sldM.style.cursor = 'default';
  1581.  
  1582. // slider pointer inner and outer border
  1583. p.sldPtrIB.style.border =
  1584. p.sldPtrOB.style.border =
  1585. THIS.pointerBorderWidth + 'px solid ' + THIS.pointerBorderColor;
  1586.  
  1587. // slider pointer outer border
  1588. p.sldPtrOB.style.position = 'absolute';
  1589. p.sldPtrOB.style.left = -(2 * THIS.pointerBorderWidth + THIS.pointerThickness) + 'px';
  1590. p.sldPtrOB.style.top = '0';
  1591.  
  1592. // slider pointer middle border
  1593. p.sldPtrMB.style.border = THIS.pointerThickness + 'px solid ' + THIS.pointerColor;
  1594.  
  1595. // slider pointer spacer
  1596. p.sldPtrS.style.width = THIS.sliderSize + 'px';
  1597. p.sldPtrS.style.height = sliderPtrSpace + 'px';
  1598.  
  1599. // the Close button
  1600. function setBtnBorder () {
  1601. var insetColors = THIS.insetColor.split(/\s+/);
  1602. var outsetColor = insetColors.length < 2 ? insetColors[0] : insetColors[1] + ' ' + insetColors[0] + ' ' + insetColors[0] + ' ' + insetColors[1];
  1603. p.btn.style.borderColor = outsetColor;
  1604. }
  1605. p.btn.style.display = THIS.closable ? 'block' : 'none';
  1606. p.btn.style.position = 'absolute';
  1607. p.btn.style.left = THIS.padding + 'px';
  1608. p.btn.style.bottom = THIS.padding + 'px';
  1609. p.btn.style.padding = '0 15px';
  1610. p.btn.style.height = THIS.buttonHeight + 'px';
  1611. p.btn.style.border = THIS.insetWidth + 'px solid';
  1612. setBtnBorder();
  1613. p.btn.style.color = THIS.buttonColor;
  1614. p.btn.style.font = '12px sans-serif';
  1615. p.btn.style.textAlign = 'center';
  1616. try {
  1617. p.btn.style.cursor = 'pointer';
  1618. } catch(eOldIE) {
  1619. p.btn.style.cursor = 'hand';
  1620. }
  1621. p.btn.onmousedown = function () {
  1622. THIS.hide();
  1623. };
  1624. p.btnT.style.lineHeight = THIS.buttonHeight + 'px';
  1625. p.btnT.innerHTML = '';
  1626. p.btnT.appendChild(document.createTextNode(THIS.closeText));
  1627.  
  1628. // place pointers
  1629. redrawPad();
  1630. redrawSld();
  1631.  
  1632. // If we are changing the owner without first closing the picker,
  1633. // make sure to first deal with the old owner
  1634. if (jsc.picker.owner && jsc.picker.owner !== THIS) {
  1635. jsc.unsetClass(jsc.picker.owner.targetElement, THIS.activeClass);
  1636. }
  1637.  
  1638. // Set the new picker owner
  1639. jsc.picker.owner = THIS;
  1640.  
  1641. // The redrawPosition() method needs picker.owner to be set, that's why we call it here,
  1642. // after setting the owner
  1643. if (jsc.isElementType(container, 'body')) {
  1644. jsc.redrawPosition();
  1645. } else {
  1646. jsc._drawPosition(THIS, 0, 0, 'relative', false);
  1647. }
  1648.  
  1649. if (p.wrap.parentNode != container) {
  1650. container.appendChild(p.wrap);
  1651. }
  1652.  
  1653. jsc.setClass(THIS.targetElement, THIS.activeClass);
  1654. }
  1655.  
  1656.  
  1657. function redrawPad () {
  1658. // redraw the pad pointer
  1659. switch (jsc.getPadYComponent(THIS)) {
  1660. case 's': var yComponent = 1; break;
  1661. case 'v': var yComponent = 2; break;
  1662. }
  1663. var x = Math.round((THIS.hsv[0] / 360) * (THIS.width - 1));
  1664. var y = Math.round((1 - THIS.hsv[yComponent] / 100) * (THIS.height - 1));
  1665. var crossOuterSize = (2 * THIS.pointerBorderWidth + THIS.pointerThickness + 2 * THIS.crossSize);
  1666. var ofs = -Math.floor(crossOuterSize / 2);
  1667. jsc.picker.cross.style.left = (x + ofs) + 'px';
  1668. jsc.picker.cross.style.top = (y + ofs) + 'px';
  1669.  
  1670. // redraw the slider
  1671. switch (jsc.getSliderComponent(THIS)) {
  1672. case 's':
  1673. var rgb1 = HSV_RGB(THIS.hsv[0], 100, THIS.hsv[2]);
  1674. var rgb2 = HSV_RGB(THIS.hsv[0], 0, THIS.hsv[2]);
  1675. var color1 = 'rgb(' +
  1676. Math.round(rgb1[0]) + ',' +
  1677. Math.round(rgb1[1]) + ',' +
  1678. Math.round(rgb1[2]) + ')';
  1679. var color2 = 'rgb(' +
  1680. Math.round(rgb2[0]) + ',' +
  1681. Math.round(rgb2[1]) + ',' +
  1682. Math.round(rgb2[2]) + ')';
  1683. jsc.picker.sldGrad.draw(THIS.sliderSize, THIS.height, color1, color2);
  1684. break;
  1685. case 'v':
  1686. var rgb = HSV_RGB(THIS.hsv[0], THIS.hsv[1], 100);
  1687. var color1 = 'rgb(' +
  1688. Math.round(rgb[0]) + ',' +
  1689. Math.round(rgb[1]) + ',' +
  1690. Math.round(rgb[2]) + ')';
  1691. var color2 = '#000';
  1692. jsc.picker.sldGrad.draw(THIS.sliderSize, THIS.height, color1, color2);
  1693. break;
  1694. }
  1695. }
  1696.  
  1697.  
  1698. function redrawSld () {
  1699. var sldComponent = jsc.getSliderComponent(THIS);
  1700. if (sldComponent) {
  1701. // redraw the slider pointer
  1702. switch (sldComponent) {
  1703. case 's': var yComponent = 1; break;
  1704. case 'v': var yComponent = 2; break;
  1705. }
  1706. var y = Math.round((1 - THIS.hsv[yComponent] / 100) * (THIS.height - 1));
  1707. jsc.picker.sldPtrOB.style.top = (y - (2 * THIS.pointerBorderWidth + THIS.pointerThickness) - Math.floor(sliderPtrSpace / 2)) + 'px';
  1708. }
  1709. }
  1710.  
  1711.  
  1712. function isPickerOwner () {
  1713. return jsc.picker && jsc.picker.owner === THIS;
  1714. }
  1715.  
  1716.  
  1717. function blurValue () {
  1718. THIS.importColor();
  1719. }
  1720.  
  1721.  
  1722. // Find the target element
  1723. if (typeof targetElement === 'string') {
  1724. var id = targetElement;
  1725. var elm = document.getElementById(id);
  1726. if (elm) {
  1727. this.targetElement = elm;
  1728. } else {
  1729. jsc.warn('Could not find target element with ID \'' + id + '\'');
  1730. }
  1731. } else if (targetElement) {
  1732. this.targetElement = targetElement;
  1733. } else {
  1734. jsc.warn('Invalid target element: \'' + targetElement + '\'');
  1735. }
  1736.  
  1737. if (this.targetElement._jscLinkedInstance) {
  1738. jsc.warn('Cannot link jscolor twice to the same element. Skipping.');
  1739. return;
  1740. }
  1741. this.targetElement._jscLinkedInstance = this;
  1742.  
  1743. // Find the value element
  1744. this.valueElement = jsc.fetchElement(this.valueElement);
  1745. // Find the style element
  1746. this.styleElement = jsc.fetchElement(this.styleElement);
  1747.  
  1748. var THIS = this;
  1749. var container =
  1750. this.container ?
  1751. jsc.fetchElement(this.container) :
  1752. document.getElementsByTagName('body')[0];
  1753. var sliderPtrSpace = 3; // px
  1754.  
  1755. // For BUTTON elements it's important to stop them from sending the form when clicked
  1756. // (e.g. in Safari)
  1757. if (jsc.isElementType(this.targetElement, 'button')) {
  1758. if (this.targetElement.onclick) {
  1759. var origCallback = this.targetElement.onclick;
  1760. this.targetElement.onclick = function (evt) {
  1761. origCallback.call(this, evt);
  1762. return false;
  1763. };
  1764. } else {
  1765. this.targetElement.onclick = function () { return false; };
  1766. }
  1767. }
  1768.  
  1769. /*
  1770. var elm = this.targetElement;
  1771. do {
  1772. // If the target element or one of its offsetParents has fixed position,
  1773. // then use fixed positioning instead
  1774. //
  1775. // Note: In Firefox, getComputedStyle returns null in a hidden iframe,
  1776. // that's why we need to check if the returned style object is non-empty
  1777. var currStyle = jsc.getStyle(elm);
  1778. if (currStyle && currStyle.position.toLowerCase() === 'fixed') {
  1779. this.fixed = true;
  1780. }
  1781.  
  1782. if (elm !== this.targetElement) {
  1783. // attach onParentScroll so that we can recompute the picker position
  1784. // when one of the offsetParents is scrolled
  1785. if (!elm._jscEventsAttached) {
  1786. jsc.attachEvent(elm, 'scroll', jsc.onParentScroll);
  1787. elm._jscEventsAttached = true;
  1788. }
  1789. }
  1790. } while ((elm = elm.offsetParent) && !jsc.isElementType(elm, 'body'));
  1791. */
  1792.  
  1793. // valueElement
  1794. if (this.valueElement) {
  1795. if (jsc.isElementType(this.valueElement, 'input')) {
  1796. var updateField = function () {
  1797. THIS.fromString(THIS.valueElement.value, jsc.leaveValue);
  1798. jsc.dispatchFineChange(THIS);
  1799. };
  1800. jsc.attachEvent(this.valueElement, 'keyup', updateField);
  1801. jsc.attachEvent(this.valueElement, 'input', updateField);
  1802. jsc.attachEvent(this.valueElement, 'blur', blurValue);
  1803. this.valueElement.setAttribute('autocomplete', 'off');
  1804. }
  1805. }
  1806.  
  1807. // styleElement
  1808. if (this.styleElement) {
  1809. this.styleElement._jscOrigStyle = {
  1810. backgroundImage : this.styleElement.style.backgroundImage,
  1811. backgroundColor : this.styleElement.style.backgroundColor,
  1812. color : this.styleElement.style.color
  1813. };
  1814. }
  1815.  
  1816. if (this.value) {
  1817. // Try to set the color from the .value option and if unsuccessful,
  1818. // export the current color
  1819. this.fromString(this.value) || this.exportColor();
  1820. } else {
  1821. this.importColor();
  1822. }
  1823. }
  1824.  
  1825. };
  1826.  
  1827.  
  1828. //================================
  1829. // Public properties and methods
  1830. //================================
  1831.  
  1832.  
  1833. // By default, search for all elements with class="jscolor" and install a color picker on them.
  1834. //
  1835. // You can change what class name will be looked for by setting the property jscolor.lookupClass
  1836. // anywhere in your HTML document. To completely disable the automatic lookup, set it to null.
  1837. //
  1838. jsc.jscolor.lookupClass = 'jscolor';
  1839.  
  1840.  
  1841. jsc.jscolor.installByClassName = function (className) {
  1842. var inputElms = document.getElementsByTagName('input');
  1843. var buttonElms = document.getElementsByTagName('button');
  1844.  
  1845. jsc.tryInstallOnElements(inputElms, className);
  1846. jsc.tryInstallOnElements(buttonElms, className);
  1847. };
  1848.  
  1849.  
  1850. jsc.register();
  1851.  
  1852.  
  1853. return jsc.jscolor;
  1854.  
  1855.  
  1856. })(); }