jscolor

jscolor is a web color picker component that aims to be super easy both for developers to install and end users to use.

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