bootstrap-colorpicker.js 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  1. /*!
  2. * Bootstrap Colorpicker
  3. * http://mjolnic.github.io/bootstrap-colorpicker/
  4. *
  5. * Originally written by (c) 2012 Stefan Petre
  6. * Licensed under the Apache License v2.0
  7. * http://www.apache.org/licenses/LICENSE-2.0.txt
  8. *
  9. * @todo Update DOCS
  10. */
  11. (function(factory) {
  12. "use strict";
  13. if (typeof exports === 'object') {
  14. module.exports = factory(window.jQuery);
  15. } else if (typeof define === 'function' && define.amd) {
  16. define(['jquery'], factory);
  17. } else if (window.jQuery && !window.jQuery.fn.colorpicker) {
  18. factory(window.jQuery);
  19. }
  20. }
  21. (function($) {
  22. 'use strict';
  23. // Color object
  24. var Color = function(val, customColors) {
  25. this.value = {
  26. h: 0,
  27. s: 0,
  28. b: 0,
  29. a: 1
  30. };
  31. this.origFormat = null; // original string format
  32. if (customColors) {
  33. $.extend(this.colors, customColors);
  34. }
  35. if (val) {
  36. if (val.toLowerCase !== undefined) {
  37. // cast to string
  38. val = val + '';
  39. this.setColor(val);
  40. } else if (val.h !== undefined) {
  41. this.value = val;
  42. }
  43. }
  44. };
  45. Color.prototype = {
  46. constructor: Color,
  47. // 140 predefined colors from the HTML Colors spec
  48. colors: {
  49. "aliceblue": "#f0f8ff",
  50. "antiquewhite": "#faebd7",
  51. "aqua": "#00ffff",
  52. "aquamarine": "#7fffd4",
  53. "azure": "#f0ffff",
  54. "beige": "#f5f5dc",
  55. "bisque": "#ffe4c4",
  56. "black": "#000000",
  57. "blanchedalmond": "#ffebcd",
  58. "blue": "#0000ff",
  59. "blueviolet": "#8a2be2",
  60. "brown": "#a52a2a",
  61. "burlywood": "#deb887",
  62. "cadetblue": "#5f9ea0",
  63. "chartreuse": "#7fff00",
  64. "chocolate": "#d2691e",
  65. "coral": "#ff7f50",
  66. "cornflowerblue": "#6495ed",
  67. "cornsilk": "#fff8dc",
  68. "crimson": "#dc143c",
  69. "cyan": "#00ffff",
  70. "darkblue": "#00008b",
  71. "darkcyan": "#008b8b",
  72. "darkgoldenrod": "#b8860b",
  73. "darkgray": "#a9a9a9",
  74. "darkgreen": "#006400",
  75. "darkkhaki": "#bdb76b",
  76. "darkmagenta": "#8b008b",
  77. "darkolivegreen": "#556b2f",
  78. "darkorange": "#ff8c00",
  79. "darkorchid": "#9932cc",
  80. "darkred": "#8b0000",
  81. "darksalmon": "#e9967a",
  82. "darkseagreen": "#8fbc8f",
  83. "darkslateblue": "#483d8b",
  84. "darkslategray": "#2f4f4f",
  85. "darkturquoise": "#00ced1",
  86. "darkviolet": "#9400d3",
  87. "deeppink": "#ff1493",
  88. "deepskyblue": "#00bfff",
  89. "dimgray": "#696969",
  90. "dodgerblue": "#1e90ff",
  91. "firebrick": "#b22222",
  92. "floralwhite": "#fffaf0",
  93. "forestgreen": "#228b22",
  94. "fuchsia": "#ff00ff",
  95. "gainsboro": "#dcdcdc",
  96. "ghostwhite": "#f8f8ff",
  97. "gold": "#ffd700",
  98. "goldenrod": "#daa520",
  99. "gray": "#808080",
  100. "green": "#008000",
  101. "greenyellow": "#adff2f",
  102. "honeydew": "#f0fff0",
  103. "hotpink": "#ff69b4",
  104. "indianred": "#cd5c5c",
  105. "indigo": "#4b0082",
  106. "ivory": "#fffff0",
  107. "khaki": "#f0e68c",
  108. "lavender": "#e6e6fa",
  109. "lavenderblush": "#fff0f5",
  110. "lawngreen": "#7cfc00",
  111. "lemonchiffon": "#fffacd",
  112. "lightblue": "#add8e6",
  113. "lightcoral": "#f08080",
  114. "lightcyan": "#e0ffff",
  115. "lightgoldenrodyellow": "#fafad2",
  116. "lightgrey": "#d3d3d3",
  117. "lightgreen": "#90ee90",
  118. "lightpink": "#ffb6c1",
  119. "lightsalmon": "#ffa07a",
  120. "lightseagreen": "#20b2aa",
  121. "lightskyblue": "#87cefa",
  122. "lightslategray": "#778899",
  123. "lightsteelblue": "#b0c4de",
  124. "lightyellow": "#ffffe0",
  125. "lime": "#00ff00",
  126. "limegreen": "#32cd32",
  127. "linen": "#faf0e6",
  128. "magenta": "#ff00ff",
  129. "maroon": "#800000",
  130. "mediumaquamarine": "#66cdaa",
  131. "mediumblue": "#0000cd",
  132. "mediumorchid": "#ba55d3",
  133. "mediumpurple": "#9370d8",
  134. "mediumseagreen": "#3cb371",
  135. "mediumslateblue": "#7b68ee",
  136. "mediumspringgreen": "#00fa9a",
  137. "mediumturquoise": "#48d1cc",
  138. "mediumvioletred": "#c71585",
  139. "midnightblue": "#191970",
  140. "mintcream": "#f5fffa",
  141. "mistyrose": "#ffe4e1",
  142. "moccasin": "#ffe4b5",
  143. "navajowhite": "#ffdead",
  144. "navy": "#000080",
  145. "oldlace": "#fdf5e6",
  146. "olive": "#808000",
  147. "olivedrab": "#6b8e23",
  148. "orange": "#ffa500",
  149. "orangered": "#ff4500",
  150. "orchid": "#da70d6",
  151. "palegoldenrod": "#eee8aa",
  152. "palegreen": "#98fb98",
  153. "paleturquoise": "#afeeee",
  154. "palevioletred": "#d87093",
  155. "papayawhip": "#ffefd5",
  156. "peachpuff": "#ffdab9",
  157. "peru": "#cd853f",
  158. "pink": "#ffc0cb",
  159. "plum": "#dda0dd",
  160. "powderblue": "#b0e0e6",
  161. "purple": "#800080",
  162. "red": "#ff0000",
  163. "rosybrown": "#bc8f8f",
  164. "royalblue": "#4169e1",
  165. "saddlebrown": "#8b4513",
  166. "salmon": "#fa8072",
  167. "sandybrown": "#f4a460",
  168. "seagreen": "#2e8b57",
  169. "seashell": "#fff5ee",
  170. "sienna": "#a0522d",
  171. "silver": "#c0c0c0",
  172. "skyblue": "#87ceeb",
  173. "slateblue": "#6a5acd",
  174. "slategray": "#708090",
  175. "snow": "#fffafa",
  176. "springgreen": "#00ff7f",
  177. "steelblue": "#4682b4",
  178. "tan": "#d2b48c",
  179. "teal": "#008080",
  180. "thistle": "#d8bfd8",
  181. "tomato": "#ff6347",
  182. "turquoise": "#40e0d0",
  183. "violet": "#ee82ee",
  184. "wheat": "#f5deb3",
  185. "white": "#ffffff",
  186. "whitesmoke": "#f5f5f5",
  187. "yellow": "#ffff00",
  188. "yellowgreen": "#9acd32",
  189. "transparent": "transparent"
  190. },
  191. _sanitizeNumber: function(val) {
  192. if (typeof val === 'number') {
  193. return val;
  194. }
  195. if (isNaN(val) || (val === null) || (val === '') || (val === undefined)) {
  196. return 1;
  197. }
  198. if (val.toLowerCase !== undefined) {
  199. return parseFloat(val);
  200. }
  201. return 1;
  202. },
  203. isTransparent: function(strVal) {
  204. if (!strVal) {
  205. return false;
  206. }
  207. strVal = strVal.toLowerCase().trim();
  208. return (strVal === 'transparent') || (strVal.match(/#?00000000/)) || (strVal.match(/(rgba|hsla)\(0,0,0,0?\.?0\)/));
  209. },
  210. rgbaIsTransparent: function(rgba) {
  211. return ((rgba.r === 0) && (rgba.g === 0) && (rgba.b === 0) && (rgba.a === 0));
  212. },
  213. //parse a string to HSB
  214. setColor: function(strVal) {
  215. strVal = strVal.toLowerCase().trim();
  216. if (strVal) {
  217. if (this.isTransparent(strVal)) {
  218. this.value = {
  219. h: 0,
  220. s: 0,
  221. b: 0,
  222. a: 0
  223. };
  224. } else {
  225. this.value = this.stringToHSB(strVal) || {
  226. h: 0,
  227. s: 0,
  228. b: 0,
  229. a: 1
  230. }; // if parser fails, defaults to black
  231. }
  232. }
  233. },
  234. stringToHSB: function(strVal) {
  235. strVal = strVal.toLowerCase();
  236. var alias;
  237. if (typeof this.colors[strVal] !== 'undefined') {
  238. strVal = this.colors[strVal];
  239. alias = 'alias';
  240. }
  241. var that = this,
  242. result = false;
  243. $.each(this.stringParsers, function(i, parser) {
  244. var match = parser.re.exec(strVal),
  245. values = match && parser.parse.apply(that, [match]),
  246. format = alias || parser.format || 'rgba';
  247. if (values) {
  248. if (format.match(/hsla?/)) {
  249. result = that.RGBtoHSB.apply(that, that.HSLtoRGB.apply(that, values));
  250. } else {
  251. result = that.RGBtoHSB.apply(that, values);
  252. }
  253. that.origFormat = format;
  254. return false;
  255. }
  256. return true;
  257. });
  258. return result;
  259. },
  260. setHue: function(h) {
  261. this.value.h = 1 - h;
  262. },
  263. setSaturation: function(s) {
  264. this.value.s = s;
  265. },
  266. setBrightness: function(b) {
  267. this.value.b = 1 - b;
  268. },
  269. setAlpha: function(a) {
  270. this.value.a = parseInt((1 - a) * 100, 10) / 100;
  271. },
  272. toRGB: function(h, s, b, a) {
  273. if (!h) {
  274. h = this.value.h;
  275. s = this.value.s;
  276. b = this.value.b;
  277. }
  278. h *= 360;
  279. var R, G, B, X, C;
  280. h = (h % 360) / 60;
  281. C = b * s;
  282. X = C * (1 - Math.abs(h % 2 - 1));
  283. R = G = B = b - C;
  284. h = ~~h;
  285. R += [C, X, 0, 0, X, C][h];
  286. G += [X, C, C, X, 0, 0][h];
  287. B += [0, 0, X, C, C, X][h];
  288. return {
  289. r: Math.round(R * 255),
  290. g: Math.round(G * 255),
  291. b: Math.round(B * 255),
  292. a: a || this.value.a
  293. };
  294. },
  295. toHex: function(h, s, b, a) {
  296. var rgb = this.toRGB(h, s, b, a);
  297. if (this.rgbaIsTransparent(rgb)) {
  298. return 'transparent';
  299. }
  300. return '#' + ((1 << 24) | (parseInt(rgb.r) << 16) | (parseInt(rgb.g) << 8) | parseInt(rgb.b)).toString(16).substr(1);
  301. },
  302. toHSL: function(h, s, b, a) {
  303. h = h || this.value.h;
  304. s = s || this.value.s;
  305. b = b || this.value.b;
  306. a = a || this.value.a;
  307. var H = h,
  308. L = (2 - s) * b,
  309. S = s * b;
  310. if (L > 0 && L <= 1) {
  311. S /= L;
  312. } else {
  313. S /= 2 - L;
  314. }
  315. L /= 2;
  316. if (S > 1) {
  317. S = 1;
  318. }
  319. return {
  320. h: isNaN(H) ? 0 : H,
  321. s: isNaN(S) ? 0 : S,
  322. l: isNaN(L) ? 0 : L,
  323. a: isNaN(a) ? 0 : a
  324. };
  325. },
  326. toAlias: function(r, g, b, a) {
  327. var rgb = this.toHex(r, g, b, a);
  328. for (var alias in this.colors) {
  329. if (this.colors[alias] === rgb) {
  330. return alias;
  331. }
  332. }
  333. return false;
  334. },
  335. RGBtoHSB: function(r, g, b, a) {
  336. r /= 255;
  337. g /= 255;
  338. b /= 255;
  339. var H, S, V, C;
  340. V = Math.max(r, g, b);
  341. C = V - Math.min(r, g, b);
  342. H = (C === 0 ? null :
  343. V === r ? (g - b) / C :
  344. V === g ? (b - r) / C + 2 :
  345. (r - g) / C + 4
  346. );
  347. H = ((H + 360) % 6) * 60 / 360;
  348. S = C === 0 ? 0 : C / V;
  349. return {
  350. h: this._sanitizeNumber(H),
  351. s: S,
  352. b: V,
  353. a: this._sanitizeNumber(a)
  354. };
  355. },
  356. HueToRGB: function(p, q, h) {
  357. if (h < 0) {
  358. h += 1;
  359. } else if (h > 1) {
  360. h -= 1;
  361. }
  362. if ((h * 6) < 1) {
  363. return p + (q - p) * h * 6;
  364. } else if ((h * 2) < 1) {
  365. return q;
  366. } else if ((h * 3) < 2) {
  367. return p + (q - p) * ((2 / 3) - h) * 6;
  368. } else {
  369. return p;
  370. }
  371. },
  372. HSLtoRGB: function(h, s, l, a) {
  373. if (s < 0) {
  374. s = 0;
  375. }
  376. var q;
  377. if (l <= 0.5) {
  378. q = l * (1 + s);
  379. } else {
  380. q = l + s - (l * s);
  381. }
  382. var p = 2 * l - q;
  383. var tr = h + (1 / 3);
  384. var tg = h;
  385. var tb = h - (1 / 3);
  386. var r = Math.round(this.HueToRGB(p, q, tr) * 255);
  387. var g = Math.round(this.HueToRGB(p, q, tg) * 255);
  388. var b = Math.round(this.HueToRGB(p, q, tb) * 255);
  389. return [r, g, b, this._sanitizeNumber(a)];
  390. },
  391. toString: function(format) {
  392. format = format || 'rgba';
  393. var c = false;
  394. switch (format) {
  395. case 'rgb':
  396. {
  397. c = this.toRGB();
  398. if (this.rgbaIsTransparent(c)) {
  399. return 'transparent';
  400. }
  401. return 'rgb(' + c.r + ',' + c.g + ',' + c.b + ')';
  402. }
  403. break;
  404. case 'rgba':
  405. {
  406. c = this.toRGB();
  407. return 'rgba(' + c.r + ',' + c.g + ',' + c.b + ',' + c.a + ')';
  408. }
  409. break;
  410. case 'hsl':
  411. {
  412. c = this.toHSL();
  413. return 'hsl(' + Math.round(c.h * 360) + ',' + Math.round(c.s * 100) + '%,' + Math.round(c.l * 100) + '%)';
  414. }
  415. break;
  416. case 'hsla':
  417. {
  418. c = this.toHSL();
  419. return 'hsla(' + Math.round(c.h * 360) + ',' + Math.round(c.s * 100) + '%,' + Math.round(c.l * 100) + '%,' + c.a + ')';
  420. }
  421. break;
  422. case 'hex':
  423. {
  424. return this.toHex();
  425. }
  426. break;
  427. case 'alias':
  428. return this.toAlias() || this.toHex();
  429. default:
  430. {
  431. return c;
  432. }
  433. break;
  434. }
  435. },
  436. // a set of RE's that can match strings and generate color tuples.
  437. // from John Resig color plugin
  438. // https://github.com/jquery/jquery-color/
  439. stringParsers: [{
  440. re: /rgb\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*?\)/,
  441. format: 'rgb',
  442. parse: function(execResult) {
  443. return [
  444. execResult[1],
  445. execResult[2],
  446. execResult[3],
  447. 1
  448. ];
  449. }
  450. }, {
  451. re: /rgb\(\s*(\d+(?:\.\d+)?)\%\s*,\s*(\d+(?:\.\d+)?)\%\s*,\s*(\d+(?:\.\d+)?)\%\s*?\)/,
  452. format: 'rgb',
  453. parse: function(execResult) {
  454. return [
  455. 2.55 * execResult[1],
  456. 2.55 * execResult[2],
  457. 2.55 * execResult[3],
  458. 1
  459. ];
  460. }
  461. }, {
  462. re: /rgba\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*(?:,\s*(\d+(?:\.\d+)?)\s*)?\)/,
  463. format: 'rgba',
  464. parse: function(execResult) {
  465. return [
  466. execResult[1],
  467. execResult[2],
  468. execResult[3],
  469. execResult[4]
  470. ];
  471. }
  472. }, {
  473. re: /rgba\(\s*(\d+(?:\.\d+)?)\%\s*,\s*(\d+(?:\.\d+)?)\%\s*,\s*(\d+(?:\.\d+)?)\%\s*(?:,\s*(\d+(?:\.\d+)?)\s*)?\)/,
  474. format: 'rgba',
  475. parse: function(execResult) {
  476. return [
  477. 2.55 * execResult[1],
  478. 2.55 * execResult[2],
  479. 2.55 * execResult[3],
  480. execResult[4]
  481. ];
  482. }
  483. }, {
  484. re: /hsl\(\s*(\d+(?:\.\d+)?)\s*,\s*(\d+(?:\.\d+)?)\%\s*,\s*(\d+(?:\.\d+)?)\%\s*?\)/,
  485. format: 'hsl',
  486. parse: function(execResult) {
  487. return [
  488. execResult[1] / 360,
  489. execResult[2] / 100,
  490. execResult[3] / 100,
  491. execResult[4]
  492. ];
  493. }
  494. }, {
  495. re: /hsla\(\s*(\d+(?:\.\d+)?)\s*,\s*(\d+(?:\.\d+)?)\%\s*,\s*(\d+(?:\.\d+)?)\%\s*(?:,\s*(\d+(?:\.\d+)?)\s*)?\)/,
  496. format: 'hsla',
  497. parse: function(execResult) {
  498. return [
  499. execResult[1] / 360,
  500. execResult[2] / 100,
  501. execResult[3] / 100,
  502. execResult[4]
  503. ];
  504. }
  505. }, {
  506. re: /#?([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/,
  507. format: 'hex',
  508. parse: function(execResult) {
  509. return [
  510. parseInt(execResult[1], 16),
  511. parseInt(execResult[2], 16),
  512. parseInt(execResult[3], 16),
  513. 1
  514. ];
  515. }
  516. }, {
  517. re: /#?([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])/,
  518. format: 'hex',
  519. parse: function(execResult) {
  520. return [
  521. parseInt(execResult[1] + execResult[1], 16),
  522. parseInt(execResult[2] + execResult[2], 16),
  523. parseInt(execResult[3] + execResult[3], 16),
  524. 1
  525. ];
  526. }
  527. }],
  528. colorNameToHex: function(name) {
  529. if (typeof this.colors[name.toLowerCase()] !== 'undefined') {
  530. return this.colors[name.toLowerCase()];
  531. }
  532. return false;
  533. }
  534. };
  535. var defaults = {
  536. horizontal: false, // horizontal mode layout ?
  537. inline: false, //forces to show the colorpicker as an inline element
  538. color: false, //forces a color
  539. format: false, //forces a format
  540. input: 'input', // children input selector
  541. container: false, // container selector
  542. component: '.add-on, .input-group-addon', // children component selector
  543. sliders: {
  544. saturation: {
  545. maxLeft: 100,
  546. maxTop: 100,
  547. callLeft: 'setSaturation',
  548. callTop: 'setBrightness'
  549. },
  550. hue: {
  551. maxLeft: 0,
  552. maxTop: 100,
  553. callLeft: false,
  554. callTop: 'setHue'
  555. },
  556. alpha: {
  557. maxLeft: 0,
  558. maxTop: 100,
  559. callLeft: false,
  560. callTop: 'setAlpha'
  561. }
  562. },
  563. slidersHorz: {
  564. saturation: {
  565. maxLeft: 100,
  566. maxTop: 100,
  567. callLeft: 'setSaturation',
  568. callTop: 'setBrightness'
  569. },
  570. hue: {
  571. maxLeft: 100,
  572. maxTop: 0,
  573. callLeft: 'setHue',
  574. callTop: false
  575. },
  576. alpha: {
  577. maxLeft: 100,
  578. maxTop: 0,
  579. callLeft: 'setAlpha',
  580. callTop: false
  581. }
  582. },
  583. template: '<div class="colorpicker dropdown-menu">' +
  584. '<div class="colorpicker-saturation"><i><b></b></i></div>' +
  585. '<div class="colorpicker-hue"><i></i></div>' +
  586. '<div class="colorpicker-alpha"><i></i></div>' +
  587. '<div class="colorpicker-color"><div /></div>' +
  588. '<div class="colorpicker-selectors"></div>' +
  589. '</div>',
  590. align: 'right',
  591. customClass: null,
  592. colorSelectors: null
  593. };
  594. var Colorpicker = function(element, options) {
  595. this.element = $(element).addClass('colorpicker-element');
  596. this.options = $.extend(true, {}, defaults, this.element.data(), options);
  597. this.component = this.options.component;
  598. this.component = (this.component !== false) ? this.element.find(this.component) : false;
  599. if (this.component && (this.component.length === 0)) {
  600. this.component = false;
  601. }
  602. this.container = (this.options.container === true) ? this.element : this.options.container;
  603. this.container = (this.container !== false) ? $(this.container) : false;
  604. // Is the element an input? Should we search inside for any input?
  605. this.input = this.element.is('input') ? this.element : (this.options.input ?
  606. this.element.find(this.options.input) : false);
  607. if (this.input && (this.input.length === 0)) {
  608. this.input = false;
  609. }
  610. // Set HSB color
  611. this.color = new Color(this.options.color !== false ? this.options.color : this.getValue(), this.options.colorSelectors);
  612. this.format = this.options.format !== false ? this.options.format : this.color.origFormat;
  613. if (this.options.color !== false) {
  614. this.updateInput(this.color);
  615. this.updateData(this.color);
  616. }
  617. // Setup picker
  618. this.picker = $(this.options.template);
  619. if (this.options.customClass) {
  620. this.picker.addClass(this.options.customClass);
  621. }
  622. if (this.options.inline) {
  623. this.picker.addClass('colorpicker-inline colorpicker-visible');
  624. } else {
  625. this.picker.addClass('colorpicker-hidden');
  626. }
  627. if (this.options.horizontal) {
  628. this.picker.addClass('colorpicker-horizontal');
  629. }
  630. if (this.format === 'rgba' || this.format === 'hsla' || this.options.format === false) {
  631. this.picker.addClass('colorpicker-with-alpha');
  632. }
  633. if (this.options.align === 'right') {
  634. this.picker.addClass('colorpicker-right');
  635. }
  636. if (this.options.inline === true) {
  637. this.picker.addClass('colorpicker-no-arrow');
  638. }
  639. if (this.options.colorSelectors) {
  640. var colorpicker = this;
  641. $.each(this.options.colorSelectors, function(name, color) {
  642. var $btn = $('<i />').css('background-color', color).data('class', name);
  643. $btn.click(function() {
  644. colorpicker.setValue($(this).css('background-color'));
  645. });
  646. colorpicker.picker.find('.colorpicker-selectors').append($btn);
  647. });
  648. this.picker.find('.colorpicker-selectors').show();
  649. }
  650. this.picker.on('mousedown.colorpicker touchstart.colorpicker', $.proxy(this.mousedown, this));
  651. this.picker.appendTo(this.container ? this.container : $('body'));
  652. // Bind events
  653. if (this.input !== false) {
  654. this.input.on({
  655. 'keyup.colorpicker': $.proxy(this.keyup, this)
  656. });
  657. this.input.on({
  658. 'change.colorpicker': $.proxy(this.change, this)
  659. });
  660. if (this.component === false) {
  661. this.element.on({
  662. 'focus.colorpicker': $.proxy(this.show, this)
  663. });
  664. }
  665. if (this.options.inline === false) {
  666. this.element.on({
  667. 'focusout.colorpicker': $.proxy(this.hide, this)
  668. });
  669. }
  670. }
  671. if (this.component !== false) {
  672. this.component.on({
  673. 'click.colorpicker': $.proxy(this.show, this)
  674. });
  675. }
  676. if ((this.input === false) && (this.component === false)) {
  677. this.element.on({
  678. 'click.colorpicker': $.proxy(this.show, this)
  679. });
  680. }
  681. // for HTML5 input[type='color']
  682. if ((this.input !== false) && (this.component !== false) && (this.input.attr('type') === 'color')) {
  683. this.input.on({
  684. 'click.colorpicker': $.proxy(this.show, this),
  685. 'focus.colorpicker': $.proxy(this.show, this)
  686. });
  687. }
  688. this.update();
  689. $($.proxy(function() {
  690. this.element.trigger('create');
  691. }, this));
  692. };
  693. Colorpicker.Color = Color;
  694. Colorpicker.prototype = {
  695. constructor: Colorpicker,
  696. destroy: function() {
  697. this.picker.remove();
  698. this.element.removeData('colorpicker').off('.colorpicker');
  699. if (this.input !== false) {
  700. this.input.off('.colorpicker');
  701. }
  702. if (this.component !== false) {
  703. this.component.off('.colorpicker');
  704. }
  705. this.element.removeClass('colorpicker-element');
  706. this.element.trigger({
  707. type: 'destroy'
  708. });
  709. },
  710. reposition: function() {
  711. if (this.options.inline !== false || this.options.container) {
  712. return false;
  713. }
  714. var type = this.container && this.container[0] !== document.body ? 'position' : 'offset';
  715. var element = this.component || this.element;
  716. var offset = element[type]();
  717. if (this.options.align === 'right') {
  718. offset.left -= this.picker.outerWidth() - element.outerWidth();
  719. }
  720. this.picker.css({
  721. top: offset.top + element.outerHeight(),
  722. left: offset.left
  723. });
  724. },
  725. show: function(e) {
  726. if (this.isDisabled()) {
  727. return false;
  728. }
  729. this.picker.addClass('colorpicker-visible').removeClass('colorpicker-hidden');
  730. this.reposition();
  731. $(window).on('resize.colorpicker', $.proxy(this.reposition, this));
  732. if (e && (!this.hasInput() || this.input.attr('type') === 'color')) {
  733. if (e.stopPropagation && e.preventDefault) {
  734. e.stopPropagation();
  735. e.preventDefault();
  736. }
  737. }
  738. if (this.options.inline === false) {
  739. $(window.document).on({
  740. 'mousedown.colorpicker': $.proxy(this.hide, this)
  741. });
  742. }
  743. this.element.trigger({
  744. type: 'showPicker',
  745. color: this.color
  746. });
  747. },
  748. hide: function() {
  749. this.picker.addClass('colorpicker-hidden').removeClass('colorpicker-visible');
  750. $(window).off('resize.colorpicker', this.reposition);
  751. $(document).off({
  752. 'mousedown.colorpicker': this.hide
  753. });
  754. this.update();
  755. this.element.trigger({
  756. type: 'hidePicker',
  757. color: this.color
  758. });
  759. },
  760. updateData: function(val) {
  761. val = val || this.color.toString(this.format);
  762. this.element.data('color', val);
  763. return val;
  764. },
  765. updateInput: function(val) {
  766. val = val || this.color.toString(this.format);
  767. if (this.input !== false) {
  768. if (this.options.colorSelectors) {
  769. var color = new Color(val, this.options.colorSelectors);
  770. var alias = color.toAlias();
  771. if (typeof this.options.colorSelectors[alias] !== 'undefined') {
  772. val = alias;
  773. }
  774. }
  775. this.input.prop('value', val);
  776. }
  777. return val;
  778. },
  779. updatePicker: function(val) {
  780. if (val !== undefined) {
  781. this.color = new Color(val, this.options.colorSelectors);
  782. }
  783. var sl = (this.options.horizontal === false) ? this.options.sliders : this.options.slidersHorz;
  784. var icns = this.picker.find('i');
  785. if (icns.length === 0) {
  786. return;
  787. }
  788. if (this.options.horizontal === false) {
  789. sl = this.options.sliders;
  790. icns.eq(1).css('top', sl.hue.maxTop * (1 - this.color.value.h)).end()
  791. .eq(2).css('top', sl.alpha.maxTop * (1 - this.color.value.a));
  792. } else {
  793. sl = this.options.slidersHorz;
  794. icns.eq(1).css('left', sl.hue.maxLeft * (1 - this.color.value.h)).end()
  795. .eq(2).css('left', sl.alpha.maxLeft * (1 - this.color.value.a));
  796. }
  797. icns.eq(0).css({
  798. 'top': sl.saturation.maxTop - this.color.value.b * sl.saturation.maxTop,
  799. 'left': this.color.value.s * sl.saturation.maxLeft
  800. });
  801. this.picker.find('.colorpicker-saturation').css('backgroundColor', this.color.toHex(this.color.value.h, 1, 1, 1));
  802. this.picker.find('.colorpicker-alpha').css('backgroundColor', this.color.toHex());
  803. this.picker.find('.colorpicker-color, .colorpicker-color div').css('backgroundColor', this.color.toString(this.format));
  804. return val;
  805. },
  806. updateComponent: function(val) {
  807. val = val || this.color.toString(this.format);
  808. if (this.component !== false) {
  809. var icn = this.component.find('i').eq(0);
  810. if (icn.length > 0) {
  811. icn.css({
  812. 'backgroundColor': val
  813. });
  814. } else {
  815. this.component.css({
  816. 'backgroundColor': val
  817. });
  818. }
  819. }
  820. return val;
  821. },
  822. update: function(force) {
  823. var val;
  824. if ((this.getValue(false) !== false) || (force === true)) {
  825. // Update input/data only if the current value is not empty
  826. val = this.updateComponent();
  827. this.updateInput(val);
  828. this.updateData(val);
  829. this.updatePicker(); // only update picker if value is not empty
  830. }
  831. return val;
  832. },
  833. setValue: function(val) { // set color manually
  834. this.color = new Color(val, this.options.colorSelectors);
  835. this.update(true);
  836. this.element.trigger({
  837. type: 'changeColor',
  838. color: this.color,
  839. value: val
  840. });
  841. },
  842. getValue: function(defaultValue) {
  843. defaultValue = (defaultValue === undefined) ? '#000000' : defaultValue;
  844. var val;
  845. if (this.hasInput()) {
  846. val = this.input.val();
  847. } else {
  848. val = this.element.data('color');
  849. }
  850. if ((val === undefined) || (val === '') || (val === null)) {
  851. // if not defined or empty, return default
  852. val = defaultValue;
  853. }
  854. return val;
  855. },
  856. hasInput: function() {
  857. return (this.input !== false);
  858. },
  859. isDisabled: function() {
  860. if (this.hasInput()) {
  861. return (this.input.prop('disabled') === true);
  862. }
  863. return false;
  864. },
  865. disable: function() {
  866. if (this.hasInput()) {
  867. this.input.prop('disabled', true);
  868. this.element.trigger({
  869. type: 'disable',
  870. color: this.color,
  871. value: this.getValue()
  872. });
  873. return true;
  874. }
  875. return false;
  876. },
  877. enable: function() {
  878. if (this.hasInput()) {
  879. this.input.prop('disabled', false);
  880. this.element.trigger({
  881. type: 'enable',
  882. color: this.color,
  883. value: this.getValue()
  884. });
  885. return true;
  886. }
  887. return false;
  888. },
  889. currentSlider: null,
  890. mousePointer: {
  891. left: 0,
  892. top: 0
  893. },
  894. mousedown: function(e) {
  895. if (!e.pageX && !e.pageY && e.originalEvent) {
  896. e.pageX = e.originalEvent.touches[0].pageX;
  897. e.pageY = e.originalEvent.touches[0].pageY;
  898. }
  899. e.stopPropagation();
  900. e.preventDefault();
  901. var target = $(e.target);
  902. //detect the slider and set the limits and callbacks
  903. var zone = target.closest('div');
  904. var sl = this.options.horizontal ? this.options.slidersHorz : this.options.sliders;
  905. if (!zone.is('.colorpicker')) {
  906. if (zone.is('.colorpicker-saturation')) {
  907. this.currentSlider = $.extend({}, sl.saturation);
  908. } else if (zone.is('.colorpicker-hue')) {
  909. this.currentSlider = $.extend({}, sl.hue);
  910. } else if (zone.is('.colorpicker-alpha')) {
  911. this.currentSlider = $.extend({}, sl.alpha);
  912. } else {
  913. return false;
  914. }
  915. var offset = zone.offset();
  916. //reference to guide's style
  917. this.currentSlider.guide = zone.find('i')[0].style;
  918. this.currentSlider.left = e.pageX - offset.left;
  919. this.currentSlider.top = e.pageY - offset.top;
  920. this.mousePointer = {
  921. left: e.pageX,
  922. top: e.pageY
  923. };
  924. //trigger mousemove to move the guide to the current position
  925. $(document).on({
  926. 'mousemove.colorpicker': $.proxy(this.mousemove, this),
  927. 'touchmove.colorpicker': $.proxy(this.mousemove, this),
  928. 'mouseup.colorpicker': $.proxy(this.mouseup, this),
  929. 'touchend.colorpicker': $.proxy(this.mouseup, this)
  930. }).trigger('mousemove');
  931. }
  932. return false;
  933. },
  934. mousemove: function(e) {
  935. if (!e.pageX && !e.pageY && e.originalEvent) {
  936. e.pageX = e.originalEvent.touches[0].pageX;
  937. e.pageY = e.originalEvent.touches[0].pageY;
  938. }
  939. e.stopPropagation();
  940. e.preventDefault();
  941. var left = Math.max(
  942. 0,
  943. Math.min(
  944. this.currentSlider.maxLeft,
  945. this.currentSlider.left + ((e.pageX || this.mousePointer.left) - this.mousePointer.left)
  946. )
  947. );
  948. var top = Math.max(
  949. 0,
  950. Math.min(
  951. this.currentSlider.maxTop,
  952. this.currentSlider.top + ((e.pageY || this.mousePointer.top) - this.mousePointer.top)
  953. )
  954. );
  955. this.currentSlider.guide.left = left + 'px';
  956. this.currentSlider.guide.top = top + 'px';
  957. if (this.currentSlider.callLeft) {
  958. this.color[this.currentSlider.callLeft].call(this.color, left / this.currentSlider.maxLeft);
  959. }
  960. if (this.currentSlider.callTop) {
  961. this.color[this.currentSlider.callTop].call(this.color, top / this.currentSlider.maxTop);
  962. }
  963. // Change format dynamically
  964. // Only occurs if user choose the dynamic format by
  965. // setting option format to false
  966. if (this.currentSlider.callTop === 'setAlpha' && this.options.format === false) {
  967. // Converting from hex / rgb to rgba
  968. if (this.color.value.a !== 1) {
  969. this.format = 'rgba';
  970. this.color.origFormat = 'rgba';
  971. }
  972. // Converting from rgba to hex
  973. else {
  974. this.format = 'hex';
  975. this.color.origFormat = 'hex';
  976. }
  977. }
  978. this.update(true);
  979. this.element.trigger({
  980. type: 'changeColor',
  981. color: this.color
  982. });
  983. return false;
  984. },
  985. mouseup: function(e) {
  986. e.stopPropagation();
  987. e.preventDefault();
  988. $(document).off({
  989. 'mousemove.colorpicker': this.mousemove,
  990. 'touchmove.colorpicker': this.mousemove,
  991. 'mouseup.colorpicker': this.mouseup,
  992. 'touchend.colorpicker': this.mouseup
  993. });
  994. return false;
  995. },
  996. change: function(e) {
  997. this.keyup(e);
  998. },
  999. keyup: function(e) {
  1000. if ((e.keyCode === 38)) {
  1001. if (this.color.value.a < 1) {
  1002. this.color.value.a = Math.round((this.color.value.a + 0.01) * 100) / 100;
  1003. }
  1004. this.update(true);
  1005. } else if ((e.keyCode === 40)) {
  1006. if (this.color.value.a > 0) {
  1007. this.color.value.a = Math.round((this.color.value.a - 0.01) * 100) / 100;
  1008. }
  1009. this.update(true);
  1010. } else {
  1011. this.color = new Color(this.input.val(), this.options.colorSelectors);
  1012. // Change format dynamically
  1013. // Only occurs if user choose the dynamic format by
  1014. // setting option format to false
  1015. if (this.color.origFormat && this.options.format === false) {
  1016. this.format = this.color.origFormat;
  1017. }
  1018. if (this.getValue(false) !== false) {
  1019. this.updateData();
  1020. this.updateComponent();
  1021. this.updatePicker();
  1022. }
  1023. }
  1024. this.element.trigger({
  1025. type: 'changeColor',
  1026. color: this.color,
  1027. value: this.input.val()
  1028. });
  1029. }
  1030. };
  1031. $.colorpicker = Colorpicker;
  1032. $.fn.colorpicker = function(option) {
  1033. var pickerArgs = arguments,
  1034. rv;
  1035. var $returnValue = this.each(function() {
  1036. var $this = $(this),
  1037. inst = $this.data('colorpicker'),
  1038. options = ((typeof option === 'object') ? option : {});
  1039. if ((!inst) && (typeof option !== 'string')) {
  1040. $this.data('colorpicker', new Colorpicker(this, options));
  1041. } else {
  1042. if (typeof option === 'string') {
  1043. rv = inst[option].apply(inst, Array.prototype.slice.call(pickerArgs, 1));
  1044. }
  1045. }
  1046. });
  1047. if (option === 'getValue') {
  1048. return rv;
  1049. }
  1050. return $returnValue;
  1051. };
  1052. $.fn.colorpicker.constructor = Colorpicker;
  1053. }));