jscolor.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935
  1. /**
  2. * jscolor, JavaScript Color Picker
  3. *
  4. * @version 1.3.13
  5. * @license GNU Lesser General Public License, http://www.gnu.org/copyleft/lesser.html
  6. * @author Jan Odvarko, http://odvarko.cz
  7. * @created 2008-06-15
  8. * @updated 2012-01-19
  9. * @link http://jscolor.com
  10. */
  11. var jscolor = {
  12. dir : '', // location of jscolor directory (leave empty to autodetect)
  13. bindClass : 'color', // class name
  14. binding : true, // automatic binding via <input class="...">
  15. preloading : true, // use image preloading?
  16. install : function() {
  17. jscolor.addEvent(window, 'load', jscolor.init);
  18. },
  19. init : function() {
  20. if(jscolor.binding) {
  21. jscolor.bind();
  22. }
  23. if(jscolor.preloading) {
  24. jscolor.preload();
  25. }
  26. },
  27. getDir : function() {
  28. if(!jscolor.dir) {
  29. var detected = jscolor.detectDir();
  30. jscolor.dir = detected!==false ? detected : 'jscolor/';
  31. }
  32. return jscolor.dir;
  33. },
  34. detectDir : function() {
  35. var base = location.href;
  36. var e = document.getElementsByTagName('base');
  37. for(var i=0; i<e.length; i+=1) {
  38. if(e[i].href) { base = e[i].href; }
  39. }
  40. var e = document.getElementsByTagName('script');
  41. for(var i=0; i<e.length; i+=1) {
  42. if(e[i].src && /(^|\/)jscolor\.js([?#].*)?$/i.test(e[i].src)) {
  43. var src = new jscolor.URI(e[i].src);
  44. var srcAbs = src.toAbsolute(base);
  45. srcAbs.path = srcAbs.path.replace(/[^\/]+$/, ''); // remove filename
  46. srcAbs.query = null;
  47. srcAbs.fragment = null;
  48. return srcAbs.toString();
  49. }
  50. }
  51. return false;
  52. },
  53. bind : function() {
  54. var matchClass = new RegExp('(^|\\s)('+jscolor.bindClass+')\\s*(\\{[^}]*\\})?', 'i');
  55. var e = document.getElementsByTagName('input');
  56. for(var i=0; i<e.length; i+=1) {
  57. var m;
  58. if(!e[i].color && e[i].className && (m = e[i].className.match(matchClass))) {
  59. var prop = {};
  60. if(m[3]) {
  61. try {
  62. eval('prop='+m[3]);
  63. } catch(eInvalidProp) {}
  64. }
  65. e[i].color = new jscolor.color(e[i], prop);
  66. }
  67. }
  68. },
  69. preload : function() {
  70. for(var fn in jscolor.imgRequire) {
  71. if(jscolor.imgRequire.hasOwnProperty(fn)) {
  72. jscolor.loadImage(fn);
  73. }
  74. }
  75. },
  76. images : {
  77. pad : [ 181, 101 ],
  78. sld : [ 16, 101 ],
  79. cross : [ 15, 15 ],
  80. arrow : [ 7, 11 ]
  81. },
  82. imgRequire : {},
  83. imgLoaded : {},
  84. requireImage : function(filename) {
  85. jscolor.imgRequire[filename] = true;
  86. },
  87. loadImage : function(filename) {
  88. if(!jscolor.imgLoaded[filename]) {
  89. jscolor.imgLoaded[filename] = new Image();
  90. jscolor.imgLoaded[filename].src = jscolor.getDir()+filename;
  91. }
  92. },
  93. fetchElement : function(mixed) {
  94. return typeof mixed === 'string' ? document.getElementById(mixed) : mixed;
  95. },
  96. addEvent : function(el, evnt, func) {
  97. if(el.addEventListener) {
  98. el.addEventListener(evnt, func, false);
  99. } else if(el.attachEvent) {
  100. el.attachEvent('on'+evnt, func);
  101. }
  102. },
  103. fireEvent : function(el, evnt) {
  104. if(!el) {
  105. return;
  106. }
  107. if(document.createEvent) {
  108. var ev = document.createEvent('HTMLEvents');
  109. ev.initEvent(evnt, true, true);
  110. el.dispatchEvent(ev);
  111. } else if(document.createEventObject) {
  112. var ev = document.createEventObject();
  113. el.fireEvent('on'+evnt, ev);
  114. } else if(el['on'+evnt]) { // alternatively use the traditional event model (IE5)
  115. el['on'+evnt]();
  116. }
  117. },
  118. getElementPos : function(e) {
  119. var e1=e, e2=e;
  120. var x=0, y=0;
  121. if(e1.offsetParent) {
  122. do {
  123. x += e1.offsetLeft;
  124. y += e1.offsetTop;
  125. } while(e1 = e1.offsetParent);
  126. }
  127. while((e2 = e2.parentNode) && e2.nodeName.toUpperCase() !== 'BODY') {
  128. x -= e2.scrollLeft;
  129. y -= e2.scrollTop;
  130. }
  131. return [x, y];
  132. },
  133. getElementSize : function(e) {
  134. return [e.offsetWidth, e.offsetHeight];
  135. },
  136. getRelMousePos : function(e) {
  137. var x = 0, y = 0;
  138. if (!e) { e = window.event; }
  139. if (typeof e.offsetX === 'number') {
  140. x = e.offsetX;
  141. y = e.offsetY;
  142. } else if (typeof e.layerX === 'number') {
  143. x = e.layerX;
  144. y = e.layerY;
  145. }
  146. return { x: x, y: y };
  147. },
  148. getViewPos : function() {
  149. if(typeof window.pageYOffset === 'number') {
  150. return [window.pageXOffset, window.pageYOffset];
  151. } else if(document.body && (document.body.scrollLeft || document.body.scrollTop)) {
  152. return [document.body.scrollLeft, document.body.scrollTop];
  153. } else if(document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
  154. return [document.documentElement.scrollLeft, document.documentElement.scrollTop];
  155. } else {
  156. return [0, 0];
  157. }
  158. },
  159. getViewSize : function() {
  160. if(typeof window.innerWidth === 'number') {
  161. return [window.innerWidth, window.innerHeight];
  162. } else if(document.body && (document.body.clientWidth || document.body.clientHeight)) {
  163. return [document.body.clientWidth, document.body.clientHeight];
  164. } else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
  165. return [document.documentElement.clientWidth, document.documentElement.clientHeight];
  166. } else {
  167. return [0, 0];
  168. }
  169. },
  170. URI : function(uri) { // See RFC3986
  171. this.scheme = null;
  172. this.authority = null;
  173. this.path = '';
  174. this.query = null;
  175. this.fragment = null;
  176. this.parse = function(uri) {
  177. var m = uri.match(/^(([A-Za-z][0-9A-Za-z+.-]*)(:))?((\/\/)([^\/?#]*))?([^?#]*)((\?)([^#]*))?((#)(.*))?/);
  178. this.scheme = m[3] ? m[2] : null;
  179. this.authority = m[5] ? m[6] : null;
  180. this.path = m[7];
  181. this.query = m[9] ? m[10] : null;
  182. this.fragment = m[12] ? m[13] : null;
  183. return this;
  184. };
  185. this.toString = function() {
  186. var result = '';
  187. if(this.scheme !== null) { result = result + this.scheme + ':'; }
  188. if(this.authority !== null) { result = result + '//' + this.authority; }
  189. if(this.path !== null) { result = result + this.path; }
  190. if(this.query !== null) { result = result + '?' + this.query; }
  191. if(this.fragment !== null) { result = result + '#' + this.fragment; }
  192. return result;
  193. };
  194. this.toAbsolute = function(base) {
  195. var base = new jscolor.URI(base);
  196. var r = this;
  197. var t = new jscolor.URI;
  198. if(base.scheme === null) { return false; }
  199. if(r.scheme !== null && r.scheme.toLowerCase() === base.scheme.toLowerCase()) {
  200. r.scheme = null;
  201. }
  202. if(r.scheme !== null) {
  203. t.scheme = r.scheme;
  204. t.authority = r.authority;
  205. t.path = removeDotSegments(r.path);
  206. t.query = r.query;
  207. } else {
  208. if(r.authority !== null) {
  209. t.authority = r.authority;
  210. t.path = removeDotSegments(r.path);
  211. t.query = r.query;
  212. } else {
  213. if(r.path === '') { // TODO: == or === ?
  214. t.path = base.path;
  215. if(r.query !== null) {
  216. t.query = r.query;
  217. } else {
  218. t.query = base.query;
  219. }
  220. } else {
  221. if(r.path.substr(0,1) === '/') {
  222. t.path = removeDotSegments(r.path);
  223. } else {
  224. if(base.authority !== null && base.path === '') { // TODO: == or === ?
  225. t.path = '/'+r.path;
  226. } else {
  227. t.path = base.path.replace(/[^\/]+$/,'')+r.path;
  228. }
  229. t.path = removeDotSegments(t.path);
  230. }
  231. t.query = r.query;
  232. }
  233. t.authority = base.authority;
  234. }
  235. t.scheme = base.scheme;
  236. }
  237. t.fragment = r.fragment;
  238. return t;
  239. };
  240. function removeDotSegments(path) {
  241. var out = '';
  242. while(path) {
  243. if(path.substr(0,3)==='../' || path.substr(0,2)==='./') {
  244. path = path.replace(/^\.+/,'').substr(1);
  245. } else if(path.substr(0,3)==='/./' || path==='/.') {
  246. path = '/'+path.substr(3);
  247. } else if(path.substr(0,4)==='/../' || path==='/..') {
  248. path = '/'+path.substr(4);
  249. out = out.replace(/\/?[^\/]*$/, '');
  250. } else if(path==='.' || path==='..') {
  251. path = '';
  252. } else {
  253. var rm = path.match(/^\/?[^\/]*/)[0];
  254. path = path.substr(rm.length);
  255. out = out + rm;
  256. }
  257. }
  258. return out;
  259. }
  260. if(uri) {
  261. this.parse(uri);
  262. }
  263. },
  264. /*
  265. * Usage example:
  266. * var myColor = new jscolor.color(myInputElement)
  267. */
  268. color : function(target, prop) {
  269. this.required = true; // refuse empty values?
  270. this.adjust = true; // adjust value to uniform notation?
  271. this.hash = false; // prefix color with # symbol?
  272. this.caps = true; // uppercase?
  273. this.slider = true; // show the value/saturation slider?
  274. this.valueElement = target; // value holder
  275. this.styleElement = target; // where to reflect current color
  276. this.onImmediateChange = null; // onchange callback (can be either string or function)
  277. this.hsv = [0, 0, 1]; // read-only 0-6, 0-1, 0-1
  278. this.rgb = [1, 1, 1]; // read-only 0-1, 0-1, 0-1
  279. this.pickerOnfocus = true; // display picker on focus?
  280. this.pickerMode = 'HSV'; // HSV | HVS
  281. this.pickerPosition = 'bottom'; // left | right | top | bottom
  282. this.pickerSmartPosition = true; // automatically adjust picker position when necessary
  283. this.pickerButtonHeight = 20; // px
  284. this.pickerClosable = false;
  285. this.pickerCloseText = 'Close';
  286. this.pickerButtonColor = 'ButtonText'; // px
  287. this.pickerFace = 10; // px
  288. this.pickerFaceColor = 'ThreeDFace'; // CSS color
  289. this.pickerBorder = 1; // px
  290. this.pickerBorderColor = 'ThreeDHighlight ThreeDShadow ThreeDShadow ThreeDHighlight'; // CSS color
  291. this.pickerInset = 1; // px
  292. this.pickerInsetColor = 'ThreeDShadow ThreeDHighlight ThreeDHighlight ThreeDShadow'; // CSS color
  293. this.pickerZIndex = 10000;
  294. for(var p in prop) {
  295. if(prop.hasOwnProperty(p)) {
  296. this[p] = prop[p];
  297. }
  298. }
  299. this.hidePicker = function() {
  300. if(isPickerOwner()) {
  301. removePicker();
  302. }
  303. };
  304. this.showPicker = function() {
  305. if(!isPickerOwner()) {
  306. var tp = jscolor.getElementPos(target); // target pos
  307. var ts = jscolor.getElementSize(target); // target size
  308. var vp = jscolor.getViewPos(); // view pos
  309. var vs = jscolor.getViewSize(); // view size
  310. var ps = getPickerDims(this); // picker size
  311. var a, b, c;
  312. switch(this.pickerPosition.toLowerCase()) {
  313. case 'left': a=1; b=0; c=-1; break;
  314. case 'right':a=1; b=0; c=1; break;
  315. case 'top': a=0; b=1; c=-1; break;
  316. default: a=0; b=1; c=1; break;
  317. }
  318. var l = (ts[b]+ps[b])/2;
  319. // picker pos
  320. if (!this.pickerSmartPosition) {
  321. var pp = [
  322. tp[a],
  323. tp[b]+ts[b]-l+l*c
  324. ];
  325. } else {
  326. var pp = [
  327. -vp[a]+tp[a]+ps[a] > vs[a] ?
  328. (-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]) :
  329. tp[a],
  330. -vp[b]+tp[b]+ts[b]+ps[b]-l+l*c > vs[b] ?
  331. (-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) :
  332. (tp[b]+ts[b]-l+l*c >= 0 ? tp[b]+ts[b]-l+l*c : tp[b]+ts[b]-l-l*c)
  333. ];
  334. }
  335. drawPicker(pp[a], pp[b]);
  336. }
  337. };
  338. this.importColor = function() {
  339. if(!valueElement) {
  340. this.exportColor();
  341. } else {
  342. if(!this.adjust) {
  343. if(!this.fromString(valueElement.value, leaveValue)) {
  344. styleElement.style.backgroundImage = styleElement.jscStyle.backgroundImage;
  345. styleElement.style.backgroundColor = styleElement.jscStyle.backgroundColor;
  346. styleElement.style.color = styleElement.jscStyle.color;
  347. this.exportColor(leaveValue | leaveStyle);
  348. }
  349. } else if(!this.required && /^\s*$/.test(valueElement.value)) {
  350. valueElement.value = '';
  351. styleElement.style.backgroundImage = styleElement.jscStyle.backgroundImage;
  352. styleElement.style.backgroundColor = styleElement.jscStyle.backgroundColor;
  353. styleElement.style.color = styleElement.jscStyle.color;
  354. this.exportColor(leaveValue | leaveStyle);
  355. } else if(this.fromString(valueElement.value)) {
  356. // OK
  357. } else {
  358. this.exportColor();
  359. }
  360. }
  361. };
  362. this.exportColor = function(flags) {
  363. if(!(flags & leaveValue) && valueElement) {
  364. var value = this.toString();
  365. if(this.caps) { value = value.toUpperCase(); }
  366. if(this.hash) { value = '#'+value; }
  367. valueElement.value = value;
  368. }
  369. if(!(flags & leaveStyle) && styleElement) {
  370. styleElement.style.backgroundImage = "none";
  371. styleElement.style.backgroundColor =
  372. '#'+this.toString();
  373. styleElement.style.color =
  374. 0.213 * this.rgb[0] +
  375. 0.715 * this.rgb[1] +
  376. 0.072 * this.rgb[2]
  377. < 0.5 ? '#FFF' : '#000';
  378. }
  379. if(!(flags & leavePad) && isPickerOwner()) {
  380. redrawPad();
  381. }
  382. if(!(flags & leaveSld) && isPickerOwner()) {
  383. redrawSld();
  384. }
  385. };
  386. this.fromHSV = function(h, s, v, flags) { // null = don't change
  387. h<0 && (h=0) || h>6 && (h=6);
  388. s<0 && (s=0) || s>1 && (s=1);
  389. v<0 && (v=0) || v>1 && (v=1);
  390. this.rgb = HSV_RGB(
  391. h===null ? this.hsv[0] : (this.hsv[0]=h),
  392. s===null ? this.hsv[1] : (this.hsv[1]=s),
  393. v===null ? this.hsv[2] : (this.hsv[2]=v)
  394. );
  395. this.exportColor(flags);
  396. };
  397. this.fromRGB = function(r, g, b, flags) { // null = don't change
  398. r<0 && (r=0) || r>1 && (r=1);
  399. g<0 && (g=0) || g>1 && (g=1);
  400. b<0 && (b=0) || b>1 && (b=1);
  401. var hsv = RGB_HSV(
  402. r===null ? this.rgb[0] : (this.rgb[0]=r),
  403. g===null ? this.rgb[1] : (this.rgb[1]=g),
  404. b===null ? this.rgb[2] : (this.rgb[2]=b)
  405. );
  406. if(hsv[0] !== null) {
  407. this.hsv[0] = hsv[0];
  408. }
  409. if(hsv[2] !== 0) {
  410. this.hsv[1] = hsv[1];
  411. }
  412. this.hsv[2] = hsv[2];
  413. this.exportColor(flags);
  414. };
  415. this.fromString = function(hex, flags) {
  416. var m = hex.match(/^\W*([0-9A-F]{3}([0-9A-F]{3})?)\W*$/i);
  417. if(!m) {
  418. return false;
  419. } else {
  420. if(m[1].length === 6) { // 6-char notation
  421. this.fromRGB(
  422. parseInt(m[1].substr(0,2),16) / 255,
  423. parseInt(m[1].substr(2,2),16) / 255,
  424. parseInt(m[1].substr(4,2),16) / 255,
  425. flags
  426. );
  427. } else { // 3-char notation
  428. this.fromRGB(
  429. parseInt(m[1].charAt(0)+m[1].charAt(0),16) / 255,
  430. parseInt(m[1].charAt(1)+m[1].charAt(1),16) / 255,
  431. parseInt(m[1].charAt(2)+m[1].charAt(2),16) / 255,
  432. flags
  433. );
  434. }
  435. return true;
  436. }
  437. };
  438. this.toString = function() {
  439. return (
  440. (0x100 | Math.round(255*this.rgb[0])).toString(16).substr(1) +
  441. (0x100 | Math.round(255*this.rgb[1])).toString(16).substr(1) +
  442. (0x100 | Math.round(255*this.rgb[2])).toString(16).substr(1)
  443. );
  444. };
  445. function RGB_HSV(r, g, b) {
  446. var n = Math.min(Math.min(r,g),b);
  447. var v = Math.max(Math.max(r,g),b);
  448. var m = v - n;
  449. if(m === 0) { return [ null, 0, v ]; }
  450. var h = r===n ? 3+(b-g)/m : (g===n ? 5+(r-b)/m : 1+(g-r)/m);
  451. return [ h===6?0:h, m/v, v ];
  452. }
  453. function HSV_RGB(h, s, v) {
  454. if(h === null) { return [ v, v, v ]; }
  455. var i = Math.floor(h);
  456. var f = i%2 ? h-i : 1-(h-i);
  457. var m = v * (1 - s);
  458. var n = v * (1 - s*f);
  459. switch(i) {
  460. case 6:
  461. case 0: return [v,n,m];
  462. case 1: return [n,v,m];
  463. case 2: return [m,v,n];
  464. case 3: return [m,n,v];
  465. case 4: return [n,m,v];
  466. case 5: return [v,m,n];
  467. }
  468. }
  469. function removePicker() {
  470. delete jscolor.picker.owner;
  471. document.getElementsByTagName('body')[0].removeChild(jscolor.picker.boxB);
  472. }
  473. function drawPicker(x, y) {
  474. if(!jscolor.picker) {
  475. jscolor.picker = {
  476. box : document.createElement('div'),
  477. boxB : document.createElement('div'),
  478. pad : document.createElement('div'),
  479. padB : document.createElement('div'),
  480. padM : document.createElement('div'),
  481. sld : document.createElement('div'),
  482. sldB : document.createElement('div'),
  483. sldM : document.createElement('div'),
  484. btn : document.createElement('div'),
  485. btnS : document.createElement('span'),
  486. btnT : document.createTextNode(THIS.pickerCloseText)
  487. };
  488. for(var i=0,segSize=4; i<jscolor.images.sld[1]; i+=segSize) {
  489. var seg = document.createElement('div');
  490. seg.style.height = segSize+'px';
  491. seg.style.fontSize = '1px';
  492. seg.style.lineHeight = '0';
  493. jscolor.picker.sld.appendChild(seg);
  494. }
  495. jscolor.picker.sldB.appendChild(jscolor.picker.sld);
  496. jscolor.picker.box.appendChild(jscolor.picker.sldB);
  497. jscolor.picker.box.appendChild(jscolor.picker.sldM);
  498. jscolor.picker.padB.appendChild(jscolor.picker.pad);
  499. jscolor.picker.box.appendChild(jscolor.picker.padB);
  500. jscolor.picker.box.appendChild(jscolor.picker.padM);
  501. jscolor.picker.btnS.appendChild(jscolor.picker.btnT);
  502. jscolor.picker.btn.appendChild(jscolor.picker.btnS);
  503. jscolor.picker.box.appendChild(jscolor.picker.btn);
  504. jscolor.picker.boxB.appendChild(jscolor.picker.box);
  505. }
  506. var p = jscolor.picker;
  507. // controls interaction
  508. p.box.onmouseup =
  509. p.box.onmouseout = function() { target.focus(); };
  510. p.box.onmousedown = function() { abortBlur=true; };
  511. p.box.onmousemove = function(e) {
  512. if (holdPad || holdSld) {
  513. holdPad && setPad(e);
  514. holdSld && setSld(e);
  515. if (document.selection) {
  516. document.selection.empty();
  517. } else if (window.getSelection) {
  518. window.getSelection().removeAllRanges();
  519. }
  520. dispatchImmediateChange();
  521. }
  522. };
  523. p.padM.onmouseup =
  524. p.padM.onmouseout = function() { if(holdPad) { holdPad=false; jscolor.fireEvent(valueElement,'change'); } };
  525. p.padM.onmousedown = function(e) {
  526. // if the slider is at the bottom, move it up
  527. switch(modeID) {
  528. case 0: if (THIS.hsv[2] === 0) { THIS.fromHSV(null, null, 1.0); }; break;
  529. case 1: if (THIS.hsv[1] === 0) { THIS.fromHSV(null, 1.0, null); }; break;
  530. }
  531. holdPad=true;
  532. setPad(e);
  533. dispatchImmediateChange();
  534. };
  535. p.sldM.onmouseup =
  536. p.sldM.onmouseout = function() { if(holdSld) { holdSld=false; jscolor.fireEvent(valueElement,'change'); } };
  537. p.sldM.onmousedown = function(e) {
  538. holdSld=true;
  539. setSld(e);
  540. dispatchImmediateChange();
  541. };
  542. // picker
  543. var dims = getPickerDims(THIS);
  544. p.box.style.width = dims[0] + 'px';
  545. p.box.style.height = dims[1] + 'px';
  546. // picker border
  547. p.boxB.style.position = 'absolute';
  548. p.boxB.style.clear = 'both';
  549. p.boxB.style.left = x+'px';
  550. p.boxB.style.top = y+'px';
  551. p.boxB.style.zIndex = THIS.pickerZIndex;
  552. p.boxB.style.border = THIS.pickerBorder+'px solid';
  553. p.boxB.style.borderColor = THIS.pickerBorderColor;
  554. p.boxB.style.background = THIS.pickerFaceColor;
  555. // pad image
  556. p.pad.style.width = jscolor.images.pad[0]+'px';
  557. p.pad.style.height = jscolor.images.pad[1]+'px';
  558. // pad border
  559. p.padB.style.position = 'absolute';
  560. p.padB.style.left = THIS.pickerFace+'px';
  561. p.padB.style.top = THIS.pickerFace+'px';
  562. p.padB.style.border = THIS.pickerInset+'px solid';
  563. p.padB.style.borderColor = THIS.pickerInsetColor;
  564. // pad mouse area
  565. p.padM.style.position = 'absolute';
  566. p.padM.style.left = '0';
  567. p.padM.style.top = '0';
  568. p.padM.style.width = THIS.pickerFace + 2*THIS.pickerInset + jscolor.images.pad[0] + jscolor.images.arrow[0] + 'px';
  569. p.padM.style.height = p.box.style.height;
  570. p.padM.style.cursor = 'crosshair';
  571. // slider image
  572. p.sld.style.overflow = 'hidden';
  573. p.sld.style.width = jscolor.images.sld[0]+'px';
  574. p.sld.style.height = jscolor.images.sld[1]+'px';
  575. // slider border
  576. p.sldB.style.display = THIS.slider ? 'block' : 'none';
  577. p.sldB.style.position = 'absolute';
  578. p.sldB.style.right = THIS.pickerFace+'px';
  579. p.sldB.style.top = THIS.pickerFace+'px';
  580. p.sldB.style.border = THIS.pickerInset+'px solid';
  581. p.sldB.style.borderColor = THIS.pickerInsetColor;
  582. // slider mouse area
  583. p.sldM.style.display = THIS.slider ? 'block' : 'none';
  584. p.sldM.style.position = 'absolute';
  585. p.sldM.style.right = '0';
  586. p.sldM.style.top = '0';
  587. p.sldM.style.width = jscolor.images.sld[0] + jscolor.images.arrow[0] + THIS.pickerFace + 2*THIS.pickerInset + 'px';
  588. p.sldM.style.height = p.box.style.height;
  589. try {
  590. p.sldM.style.cursor = 'pointer';
  591. } catch(eOldIE) {
  592. p.sldM.style.cursor = 'hand';
  593. }
  594. // "close" button
  595. function setBtnBorder() {
  596. var insetColors = THIS.pickerInsetColor.split(/\s+/);
  597. var pickerOutsetColor = insetColors.length < 2 ? insetColors[0] : insetColors[1] + ' ' + insetColors[0] + ' ' + insetColors[0] + ' ' + insetColors[1];
  598. p.btn.style.borderColor = pickerOutsetColor;
  599. }
  600. p.btn.style.display = THIS.pickerClosable ? 'block' : 'none';
  601. p.btn.style.position = 'absolute';
  602. p.btn.style.left = THIS.pickerFace + 'px';
  603. p.btn.style.bottom = THIS.pickerFace + 'px';
  604. p.btn.style.padding = '0 15px';
  605. p.btn.style.height = '18px';
  606. p.btn.style.border = THIS.pickerInset + 'px solid';
  607. setBtnBorder();
  608. p.btn.style.color = THIS.pickerButtonColor;
  609. p.btn.style.font = '12px sans-serif';
  610. p.btn.style.textAlign = 'center';
  611. try {
  612. p.btn.style.cursor = 'pointer';
  613. } catch(eOldIE) {
  614. p.btn.style.cursor = 'hand';
  615. }
  616. p.btn.onmousedown = function () {
  617. THIS.hidePicker();
  618. };
  619. p.btnS.style.lineHeight = p.btn.style.height;
  620. // load images in optimal order
  621. switch(modeID) {
  622. case 0: var padImg = 'hs.png'; break;
  623. case 1: var padImg = 'hv.png'; break;
  624. }
  625. p.padM.style.backgroundImage = "url('"+jscolor.getDir()+"cross.gif')";
  626. p.padM.style.backgroundRepeat = "no-repeat";
  627. p.sldM.style.backgroundImage = "url('"+jscolor.getDir()+"arrow.gif')";
  628. p.sldM.style.backgroundRepeat = "no-repeat";
  629. p.pad.style.backgroundImage = "url('"+jscolor.getDir()+padImg+"')";
  630. p.pad.style.backgroundRepeat = "no-repeat";
  631. p.pad.style.backgroundPosition = "0 0";
  632. // place pointers
  633. redrawPad();
  634. redrawSld();
  635. jscolor.picker.owner = THIS;
  636. document.getElementsByTagName('body')[0].appendChild(p.boxB);
  637. }
  638. function getPickerDims(o) {
  639. var dims = [
  640. 2*o.pickerInset + 2*o.pickerFace + jscolor.images.pad[0] +
  641. (o.slider ? 2*o.pickerInset + 2*jscolor.images.arrow[0] + jscolor.images.sld[0] : 0),
  642. o.pickerClosable ?
  643. 4*o.pickerInset + 3*o.pickerFace + jscolor.images.pad[1] + o.pickerButtonHeight :
  644. 2*o.pickerInset + 2*o.pickerFace + jscolor.images.pad[1]
  645. ];
  646. return dims;
  647. }
  648. function redrawPad() {
  649. // redraw the pad pointer
  650. switch(modeID) {
  651. case 0: var yComponent = 1; break;
  652. case 1: var yComponent = 2; break;
  653. }
  654. var x = Math.round((THIS.hsv[0]/6) * (jscolor.images.pad[0]-1));
  655. var y = Math.round((1-THIS.hsv[yComponent]) * (jscolor.images.pad[1]-1));
  656. jscolor.picker.padM.style.backgroundPosition =
  657. (THIS.pickerFace+THIS.pickerInset+x - Math.floor(jscolor.images.cross[0]/2)) + 'px ' +
  658. (THIS.pickerFace+THIS.pickerInset+y - Math.floor(jscolor.images.cross[1]/2)) + 'px';
  659. // redraw the slider image
  660. var seg = jscolor.picker.sld.childNodes;
  661. switch(modeID) {
  662. case 0:
  663. var rgb = HSV_RGB(THIS.hsv[0], THIS.hsv[1], 1);
  664. for(var i=0; i<seg.length; i+=1) {
  665. seg[i].style.backgroundColor = 'rgb('+
  666. (rgb[0]*(1-i/seg.length)*100)+'%,'+
  667. (rgb[1]*(1-i/seg.length)*100)+'%,'+
  668. (rgb[2]*(1-i/seg.length)*100)+'%)';
  669. }
  670. break;
  671. case 1:
  672. var rgb, s, c = [ THIS.hsv[2], 0, 0 ];
  673. var i = Math.floor(THIS.hsv[0]);
  674. var f = i%2 ? THIS.hsv[0]-i : 1-(THIS.hsv[0]-i);
  675. switch(i) {
  676. case 6:
  677. case 0: rgb=[0,1,2]; break;
  678. case 1: rgb=[1,0,2]; break;
  679. case 2: rgb=[2,0,1]; break;
  680. case 3: rgb=[2,1,0]; break;
  681. case 4: rgb=[1,2,0]; break;
  682. case 5: rgb=[0,2,1]; break;
  683. }
  684. for(var i=0; i<seg.length; i+=1) {
  685. s = 1 - 1/(seg.length-1)*i;
  686. c[1] = c[0] * (1 - s*f);
  687. c[2] = c[0] * (1 - s);
  688. seg[i].style.backgroundColor = 'rgb('+
  689. (c[rgb[0]]*100)+'%,'+
  690. (c[rgb[1]]*100)+'%,'+
  691. (c[rgb[2]]*100)+'%)';
  692. }
  693. break;
  694. }
  695. }
  696. function redrawSld() {
  697. // redraw the slider pointer
  698. switch(modeID) {
  699. case 0: var yComponent = 2; break;
  700. case 1: var yComponent = 1; break;
  701. }
  702. var y = Math.round((1-THIS.hsv[yComponent]) * (jscolor.images.sld[1]-1));
  703. jscolor.picker.sldM.style.backgroundPosition =
  704. '0 ' + (THIS.pickerFace+THIS.pickerInset+y - Math.floor(jscolor.images.arrow[1]/2)) + 'px';
  705. }
  706. function isPickerOwner() {
  707. return jscolor.picker && jscolor.picker.owner === THIS;
  708. }
  709. function blurTarget() {
  710. if(valueElement === target) {
  711. THIS.importColor();
  712. }
  713. if(THIS.pickerOnfocus) {
  714. THIS.hidePicker();
  715. }
  716. }
  717. function blurValue() {
  718. if(valueElement !== target) {
  719. THIS.importColor();
  720. }
  721. }
  722. function setPad(e) {
  723. var mpos = jscolor.getRelMousePos(e);
  724. var x = mpos.x - THIS.pickerFace - THIS.pickerInset;
  725. var y = mpos.y - THIS.pickerFace - THIS.pickerInset;
  726. switch(modeID) {
  727. case 0: THIS.fromHSV(x*(6/(jscolor.images.pad[0]-1)), 1 - y/(jscolor.images.pad[1]-1), null, leaveSld); break;
  728. case 1: THIS.fromHSV(x*(6/(jscolor.images.pad[0]-1)), null, 1 - y/(jscolor.images.pad[1]-1), leaveSld); break;
  729. }
  730. }
  731. function setSld(e) {
  732. var mpos = jscolor.getRelMousePos(e);
  733. var y = mpos.y - THIS.pickerFace - THIS.pickerInset;
  734. switch(modeID) {
  735. case 0: THIS.fromHSV(null, null, 1 - y/(jscolor.images.sld[1]-1), leavePad); break;
  736. case 1: THIS.fromHSV(null, 1 - y/(jscolor.images.sld[1]-1), null, leavePad); break;
  737. }
  738. }
  739. function dispatchImmediateChange() {
  740. if (THIS.onImmediateChange) {
  741. if (typeof THIS.onImmediateChange === 'string') {
  742. eval(THIS.onImmediateChange);
  743. } else {
  744. THIS.onImmediateChange(THIS);
  745. }
  746. }
  747. }
  748. var THIS = this;
  749. var modeID = this.pickerMode.toLowerCase()==='hvs' ? 1 : 0;
  750. var abortBlur = false;
  751. var
  752. valueElement = jscolor.fetchElement(this.valueElement),
  753. styleElement = jscolor.fetchElement(this.styleElement);
  754. var
  755. holdPad = false,
  756. holdSld = false;
  757. var
  758. leaveValue = 1<<0,
  759. leaveStyle = 1<<1,
  760. leavePad = 1<<2,
  761. leaveSld = 1<<3;
  762. // target
  763. jscolor.addEvent(target, 'focus', function() {
  764. if(THIS.pickerOnfocus) { THIS.showPicker(); }
  765. });
  766. jscolor.addEvent(target, 'blur', function() {
  767. if(!abortBlur) {
  768. window.setTimeout(function(){ abortBlur || blurTarget(); abortBlur=false; }, 0);
  769. } else {
  770. abortBlur = false;
  771. }
  772. });
  773. // valueElement
  774. if(valueElement) {
  775. var updateField = function() {
  776. THIS.fromString(valueElement.value, leaveValue);
  777. dispatchImmediateChange();
  778. };
  779. jscolor.addEvent(valueElement, 'keyup', updateField);
  780. jscolor.addEvent(valueElement, 'input', updateField);
  781. jscolor.addEvent(valueElement, 'blur', blurValue);
  782. valueElement.setAttribute('autocomplete', 'off');
  783. }
  784. // styleElement
  785. if(styleElement) {
  786. styleElement.jscStyle = {
  787. backgroundImage : styleElement.style.backgroundImage,
  788. backgroundColor : styleElement.style.backgroundColor,
  789. color : styleElement.style.color
  790. };
  791. }
  792. // require images
  793. switch(modeID) {
  794. case 0: jscolor.requireImage('hs.png'); break;
  795. case 1: jscolor.requireImage('hv.png'); break;
  796. }
  797. jscolor.requireImage('cross.gif');
  798. jscolor.requireImage('arrow.gif');
  799. this.importColor();
  800. }
  801. };
  802. jscolor.install();