bootstrap-colorpicker.js 32 KB

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