colpick.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. /*!
  2. * colpick Color Picker
  3. * https://github.com/mrgrain/colpick
  4. *
  5. * Copyright 2013, 2015 Moritz Kornher, Jose Vargas, Stefan Petre
  6. * Released under the MIT license and GPLv2 license
  7. * https://github.com/mrgrain/colpick/blob/master/LICENSE
  8. */
  9. (function (factory) {
  10. if (typeof define === 'function' && define.amd) {
  11. // AMD. Register as an anonymous module.
  12. define(['jquery'], factory);
  13. } else if (typeof exports === 'object') {
  14. // Node/CommonJS
  15. factory(require('jquery'));
  16. } else {
  17. // Browser globals
  18. factory(jQuery);
  19. }
  20. }(function ($) {
  21. var colpick = function () {
  22. var
  23. tpl = '<div class="colpick"><div class="colpick_color"><div class="colpick_color_overlay1"><div class="colpick_color_overlay2"><div class="colpick_selector_outer"><div class="colpick_selector_inner"></div></div></div></div></div><div class="colpick_hue"><div class="colpick_hue_arrs"><div class="colpick_hue_larr"></div><div class="colpick_hue_rarr"></div></div></div><div class="colpick_new_color"></div><div class="colpick_current_color"></div><div class="colpick_hex_field"><div class="colpick_field_letter">#</div><input type="text" maxlength="6" size="6" /></div><div class="colpick_rgb_r colpick_field"><div class="colpick_field_letter">R</div><input type="text" maxlength="3" size="3" /><div class="colpick_field_arrs"><div class="colpick_field_uarr"></div><div class="colpick_field_darr"></div></div></div><div class="colpick_rgb_g colpick_field"><div class="colpick_field_letter">G</div><input type="text" maxlength="3" size="3" /><div class="colpick_field_arrs"><div class="colpick_field_uarr"></div><div class="colpick_field_darr"></div></div></div><div class="colpick_rgb_b colpick_field"><div class="colpick_field_letter">B</div><input type="text" maxlength="3" size="3" /><div class="colpick_field_arrs"><div class="colpick_field_uarr"></div><div class="colpick_field_darr"></div></div></div><div class="colpick_hsb_h colpick_field"><div class="colpick_field_letter">H</div><input type="text" maxlength="3" size="3" /><div class="colpick_field_arrs"><div class="colpick_field_uarr"></div><div class="colpick_field_darr"></div></div></div><div class="colpick_hsb_s colpick_field"><div class="colpick_field_letter">S</div><input type="text" maxlength="3" size="3" /><div class="colpick_field_arrs"><div class="colpick_field_uarr"></div><div class="colpick_field_darr"></div></div></div><div class="colpick_hsb_b colpick_field"><div class="colpick_field_letter">B</div><input type="text" maxlength="3" size="3" /><div class="colpick_field_arrs"><div class="colpick_field_uarr"></div><div class="colpick_field_darr"></div></div></div><div class="colpick_submit"></div></div>',
  24. defaults = {
  25. showEvent: 'click',
  26. onShow: function () {
  27. },
  28. onBeforeShow: function () {
  29. },
  30. onHide: function () {
  31. },
  32. onChange: function () {
  33. },
  34. onSubmit: function () {
  35. },
  36. colorScheme: 'light',
  37. color: 'auto',
  38. livePreview: true,
  39. flat: false,
  40. layout: 'full',
  41. submit: 1,
  42. submitText: 'OK',
  43. height: 156,
  44. polyfill: false,
  45. styles: false
  46. },
  47. //Fill the inputs of the plugin
  48. fillRGBFields = function (hsb, cal) {
  49. var rgb = hsbToRgb(hsb);
  50. $(cal).data('colpick').fields
  51. .eq(1).val(rgb.r).end()
  52. .eq(2).val(rgb.g).end()
  53. .eq(3).val(rgb.b).end();
  54. },
  55. fillHSBFields = function (hsb, cal) {
  56. $(cal).data('colpick').fields
  57. .eq(4).val(Math.round(hsb.h)).end()
  58. .eq(5).val(Math.round(hsb.s)).end()
  59. .eq(6).val(Math.round(hsb.b)).end();
  60. },
  61. fillHexFields = function (hsb, cal) {
  62. $(cal).data('colpick').fields.eq(0).val(hsbToHex(hsb));
  63. },
  64. //Set the round selector position
  65. setSelector = function (hsb, cal) {
  66. $(cal).data('colpick').selector.css('backgroundColor', '#' + hsbToHex({h: hsb.h, s: 100, b: 100}));
  67. $(cal).data('colpick').selectorIndic.css({
  68. left: parseInt($(cal).data('colpick').height * hsb.s / 100, 10),
  69. top: parseInt($(cal).data('colpick').height * (100 - hsb.b) / 100, 10)
  70. });
  71. },
  72. //Set the hue selector position
  73. setHue = function (hsb, cal) {
  74. $(cal).data('colpick').hue.css('top', parseInt($(cal).data('colpick').height - $(cal).data('colpick').height * hsb.h / 360, 10));
  75. },
  76. //Set current and new colors
  77. setCurrentColor = function (hsb, cal) {
  78. $(cal).data('colpick').currentColor.css('backgroundColor', '#' + hsbToHex(hsb));
  79. },
  80. setNewColor = function (hsb, cal) {
  81. $(cal).data('colpick').newColor.css('backgroundColor', '#' + hsbToHex(hsb));
  82. },
  83. //Called when the new color is changed
  84. change = function () {
  85. var cal = $(this).parent().parent(), col;
  86. if (this.parentNode.className.indexOf('_hex') > 0) {
  87. cal.data('colpick').color = col = hexToHsb(fixHex(this.value));
  88. fillRGBFields(col, cal.get(0));
  89. fillHSBFields(col, cal.get(0));
  90. } else if (this.parentNode.className.indexOf('_hsb') > 0) {
  91. cal.data('colpick').color = col = fixHSB({
  92. h: parseInt(cal.data('colpick').fields.eq(4).val(), 10),
  93. s: parseInt(cal.data('colpick').fields.eq(5).val(), 10),
  94. b: parseInt(cal.data('colpick').fields.eq(6).val(), 10)
  95. });
  96. fillRGBFields(col, cal.get(0));
  97. fillHexFields(col, cal.get(0));
  98. } else {
  99. cal.data('colpick').color = col = rgbToHsb(fixRGB({
  100. r: parseInt(cal.data('colpick').fields.eq(1).val(), 10),
  101. g: parseInt(cal.data('colpick').fields.eq(2).val(), 10),
  102. b: parseInt(cal.data('colpick').fields.eq(3).val(), 10)
  103. }));
  104. fillHexFields(col, cal.get(0));
  105. fillHSBFields(col, cal.get(0));
  106. }
  107. setSelector(col, cal.get(0));
  108. setHue(col, cal.get(0));
  109. setNewColor(col, cal.get(0));
  110. cal.data('colpick').onChange.apply(cal.parent(), [col, hsbToHex(col), hsbToRgb(col), cal.data('colpick').el, 0]);
  111. },
  112. //Change style on blur and on focus of inputs
  113. blur = function () {
  114. $(this).parent().removeClass('colpick_focus');
  115. },
  116. focus = function () {
  117. $(this).parent().parent().data('colpick').fields.parent().removeClass('colpick_focus');
  118. $(this).parent().addClass('colpick_focus');
  119. },
  120. //Increment/decrement arrows functions
  121. downIncrement = function (ev) {
  122. ev.preventDefault ? ev.preventDefault() : ev.returnValue = false;
  123. var field = $(this).parent().find('input').focus();
  124. var current = {
  125. el: $(this).parent().addClass('colpick_slider'),
  126. max: this.parentNode.className.indexOf('_hsb_h') > 0 ? 360 : (this.parentNode.className.indexOf('_hsb') > 0 ? 100 : 255),
  127. y: ev.pageY,
  128. field: field,
  129. val: parseInt(field.val(), 10),
  130. preview: $(this).parent().parent().data('colpick').livePreview
  131. };
  132. $(document).mouseup(current, upIncrement);
  133. $(document).mousemove(current, moveIncrement);
  134. },
  135. moveIncrement = function (ev) {
  136. ev.data.field.val(Math.max(0, Math.min(ev.data.max, parseInt(ev.data.val - ev.pageY + ev.data.y, 10))));
  137. if (ev.data.preview) {
  138. change.apply(ev.data.field.get(0), [true]);
  139. }
  140. return false;
  141. },
  142. upIncrement = function (ev) {
  143. change.apply(ev.data.field.get(0), [true]);
  144. ev.data.el.removeClass('colpick_slider').find('input').focus();
  145. $(document).off('mouseup', upIncrement);
  146. $(document).off('mousemove', moveIncrement);
  147. return false;
  148. },
  149. //Hue slider functions
  150. downHue = function (ev) {
  151. ev.preventDefault ? ev.preventDefault() : ev.returnValue = false;
  152. var current = {
  153. cal: $(this).parent(),
  154. y: $(this).offset().top
  155. };
  156. $(document).on('mouseup touchend', current, upHue);
  157. $(document).on('mousemove touchmove', current, moveHue);
  158. var pageY = ((ev.type == 'touchstart') ? ev.originalEvent.changedTouches[0].pageY : ev.pageY );
  159. change.apply(
  160. current.cal.data('colpick')
  161. .fields.eq(4).val(parseInt(360 * (current.cal.data('colpick').height - (pageY - current.y)) / current.cal.data('colpick').height, 10))
  162. .get(0),
  163. [current.cal.data('colpick').livePreview]
  164. );
  165. return false;
  166. },
  167. moveHue = function (ev) {
  168. var pageY = ((ev.type == 'touchmove') ? ev.originalEvent.changedTouches[0].pageY : ev.pageY );
  169. change.apply(
  170. ev.data.cal.data('colpick')
  171. .fields.eq(4).val(parseInt(360 * (ev.data.cal.data('colpick').height - Math.max(0, Math.min(ev.data.cal.data('colpick').height, (pageY - ev.data.y)))) / ev.data.cal.data('colpick').height, 10))
  172. .get(0),
  173. [ev.data.preview]
  174. );
  175. return false;
  176. },
  177. upHue = function (ev) {
  178. fillRGBFields(ev.data.cal.data('colpick').color, ev.data.cal.get(0));
  179. fillHexFields(ev.data.cal.data('colpick').color, ev.data.cal.get(0));
  180. $(document).off('mouseup touchend', upHue);
  181. $(document).off('mousemove touchmove', moveHue);
  182. return false;
  183. },
  184. //Color selector functions
  185. downSelector = function (ev) {
  186. ev.preventDefault ? ev.preventDefault() : ev.returnValue = false;
  187. var current = {
  188. cal: $(this).parent(),
  189. pos: $(this).offset()
  190. };
  191. current.preview = current.cal.data('colpick').livePreview;
  192. $(document).on('mouseup touchend', current, upSelector);
  193. $(document).on('mousemove touchmove', current, moveSelector);
  194. var pageX, pageY;
  195. if (ev.type == 'touchstart') {
  196. pageX = ev.originalEvent.changedTouches[0].pageX;
  197. pageY = ev.originalEvent.changedTouches[0].pageY;
  198. } else {
  199. pageX = ev.pageX;
  200. pageY = ev.pageY;
  201. }
  202. change.apply(
  203. current.cal.data('colpick').fields
  204. .eq(6).val(parseInt(100 * (current.cal.data('colpick').height - (pageY - current.pos.top)) / current.cal.data('colpick').height, 10)).end()
  205. .eq(5).val(parseInt(100 * (pageX - current.pos.left) / current.cal.data('colpick').height, 10))
  206. .get(0),
  207. [current.preview]
  208. );
  209. return false;
  210. },
  211. moveSelector = function (ev) {
  212. var pageX, pageY;
  213. if (ev.type == 'touchmove') {
  214. pageX = ev.originalEvent.changedTouches[0].pageX;
  215. pageY = ev.originalEvent.changedTouches[0].pageY;
  216. } else {
  217. pageX = ev.pageX;
  218. pageY = ev.pageY;
  219. }
  220. change.apply(
  221. ev.data.cal.data('colpick').fields
  222. .eq(6).val(parseInt(100 * (ev.data.cal.data('colpick').height - Math.max(0, Math.min(ev.data.cal.data('colpick').height, (pageY - ev.data.pos.top)))) / ev.data.cal.data('colpick').height, 10)).end()
  223. .eq(5).val(parseInt(100 * (Math.max(0, Math.min(ev.data.cal.data('colpick').height, (pageX - ev.data.pos.left)))) / ev.data.cal.data('colpick').height, 10))
  224. .get(0),
  225. [ev.data.preview]
  226. );
  227. return false;
  228. },
  229. upSelector = function (ev) {
  230. fillRGBFields(ev.data.cal.data('colpick').color, ev.data.cal.get(0));
  231. fillHexFields(ev.data.cal.data('colpick').color, ev.data.cal.get(0));
  232. $(document).off('mouseup touchend', upSelector);
  233. $(document).off('mousemove touchmove', moveSelector);
  234. return false;
  235. },
  236. //Submit button
  237. clickSubmit = function () {
  238. var cal = $(this).parent();
  239. var col = cal.data('colpick').color;
  240. cal.data('colpick').origColor = col;
  241. setCurrentColor(col, cal.get(0));
  242. cal.data('colpick').onSubmit(col, hsbToHex(col), hsbToRgb(col), cal.data('colpick').el);
  243. },
  244. //Show/hide the color picker
  245. show = function (ev) {
  246. if (ev) {
  247. // Prevent the trigger of any direct parent
  248. ev.stopPropagation();
  249. }
  250. var cal = $('#' + $(this).data('colpickId'));
  251. if (ev && !cal.data('colpick').polyfill) {
  252. ev.preventDefault();
  253. }
  254. cal.data('colpick').onBeforeShow.apply(this, [cal.get(0)]);
  255. var pos = $(this).offset();
  256. var top = pos.top + this.offsetHeight;
  257. var left = pos.left;
  258. var viewPort = getViewport();
  259. var calW = cal.width();
  260. if (left + calW > viewPort.l + viewPort.w) {
  261. left -= calW;
  262. }
  263. cal.css({left: left + 'px', top: top + 'px'});
  264. if (cal.data('colpick').onShow.apply(this, [cal.get(0)]) != false) {
  265. cal.show();
  266. }
  267. //Hide when user clicks outside
  268. $('html').mousedown({cal: cal}, hide);
  269. cal.mousedown(function (ev) {
  270. ev.stopPropagation();
  271. })
  272. },
  273. hide = function (ev) {
  274. var cal = $('#' + $(this).data('colpickId'));
  275. if (ev) {
  276. cal = ev.data.cal;
  277. }
  278. if (cal.data('colpick').onHide.apply(this, [cal.get(0)]) != false) {
  279. cal.hide();
  280. }
  281. $('html').off('mousedown', hide);
  282. },
  283. getViewport = function () {
  284. var m = document.compatMode == 'CSS1Compat';
  285. return {
  286. l: window.pageXOffset || (m ? document.documentElement.scrollLeft : document.body.scrollLeft),
  287. w: window.innerWidth || (m ? document.documentElement.clientWidth : document.body.clientWidth)
  288. };
  289. },
  290. //Fix the values if the user enters a negative or high value
  291. fixHSB = function (hsb) {
  292. return {
  293. h: Math.min(360, Math.max(0, hsb.h)),
  294. s: Math.min(100, Math.max(0, hsb.s)),
  295. b: Math.min(100, Math.max(0, hsb.b))
  296. };
  297. },
  298. fixRGB = function (rgb) {
  299. return {
  300. r: Math.min(255, Math.max(0, rgb.r)),
  301. g: Math.min(255, Math.max(0, rgb.g)),
  302. b: Math.min(255, Math.max(0, rgb.b))
  303. };
  304. },
  305. fixHex = function (hex) {
  306. var len = 6 - hex.length;
  307. if (len == 3) {
  308. var e = [];
  309. for (var j = 0; j < len; j++) {
  310. e.push(hex[j]);
  311. e.push(hex[j]);
  312. }
  313. hex = e.join('');
  314. } else {
  315. if (len > 0) {
  316. var o = [];
  317. for (var i = 0; i < len; i++) {
  318. o.push('0');
  319. }
  320. o.push(hex);
  321. hex = o.join('');
  322. }
  323. }
  324. return hex;
  325. },
  326. restoreOriginal = function () {
  327. var cal = $(this).parent();
  328. var col = cal.data('colpick').origColor;
  329. cal.data('colpick').color = col;
  330. fillRGBFields(col, cal.get(0));
  331. fillHexFields(col, cal.get(0));
  332. fillHSBFields(col, cal.get(0));
  333. setSelector(col, cal.get(0));
  334. setHue(col, cal.get(0));
  335. setNewColor(col, cal.get(0));
  336. };
  337. return {
  338. init: function (opt) {
  339. opt = $.extend({}, defaults, opt || {});
  340. //Set color
  341. if (opt.color === 'auto') {
  342. } else if (typeof opt.color == 'string') {
  343. opt.color = hexToHsb(opt.color);
  344. } else if (opt.color.r != undefined && opt.color.g != undefined && opt.color.b != undefined) {
  345. opt.color = rgbToHsb(opt.color);
  346. } else if (opt.color.h != undefined && opt.color.s != undefined && opt.color.b != undefined) {
  347. opt.color = fixHSB(opt.color);
  348. } else {
  349. return this;
  350. }
  351. //For each selected DOM element
  352. return this.each(function () {
  353. //If the element does not have an ID
  354. if (!$(this).data('colpickId')) {
  355. var options = $.extend({}, opt);
  356. //Color
  357. if (opt.color === 'auto') {
  358. options.color = $(this).val() ? hexToHsb($(this).val()) : {h: 0, s: 0, b: 0};
  359. }
  360. options.origColor = options.color;
  361. //Polyfill
  362. if (typeof opt.polyfill == 'function') {
  363. options.polyfill = opt.polyfill(this);
  364. }
  365. if (options.polyfill && $(this).is('input') && this.type === "color") {
  366. return;
  367. }
  368. //Generate and assign a random ID
  369. var id = 'collorpicker_' + parseInt(Math.random() * 1000);
  370. $(this).data('colpickId', id);
  371. //Set the tpl's ID and get the HTML
  372. var cal = $(tpl).attr('id', id);
  373. //Add class according to layout
  374. cal.addClass('colpick_' + options.layout + (options.submit ? '' : ' colpick_' + options.layout + '_ns'));
  375. //Add class if the color scheme is not default
  376. if (options.colorScheme != 'light') {
  377. cal.addClass('colpick_' + options.colorScheme);
  378. }
  379. //Setup submit button
  380. cal.find('div.colpick_submit').html(options.submitText).click(clickSubmit);
  381. //Setup input fields
  382. options.fields = cal.find('input').change(change).blur(blur).focus(focus);
  383. cal.find('div.colpick_field_arrs').mousedown(downIncrement).end().find('div.colpick_current_color').click(restoreOriginal);
  384. //Setup hue selector
  385. options.selector = cal.find('div.colpick_color').on('mousedown touchstart', downSelector);
  386. options.selectorIndic = options.selector.find('div.colpick_selector_outer');
  387. //Store parts of the plugin
  388. options.el = this;
  389. options.hue = cal.find('div.colpick_hue_arrs');
  390. var huebar = options.hue.parent();
  391. //Paint the hue bar
  392. var UA = navigator.userAgent.toLowerCase();
  393. var isIE = navigator.appName === 'Microsoft Internet Explorer';
  394. var IEver = isIE ? parseFloat(UA.match(/msie ([0-9]*[\.0-9]+)/)[1]) : 0;
  395. var ngIE = ( isIE && IEver < 10 );
  396. var stops = ['#ff0000', '#ff0080', '#ff00ff', '#8000ff', '#0000ff', '#0080ff', '#00ffff', '#00ff80', '#00ff00', '#80ff00', '#ffff00', '#ff8000', '#ff0000'];
  397. if (ngIE) {
  398. var i, div;
  399. for (i = 0; i <= 11; i++) {
  400. div = $('<div></div>').attr('style', 'height:8.333333%; filter:progid:DXImageTransform.Microsoft.gradient(GradientType=0,startColorstr=' + stops[i] + ', endColorstr=' + stops[i + 1] + '); -ms-filter: "progid:DXImageTransform.Microsoft.gradient(GradientType=0,startColorstr=' + stops[i] + ', endColorstr=' + stops[i + 1] + ')";');
  401. huebar.append(div);
  402. }
  403. } else {
  404. var stopList = stops.join(',');
  405. huebar.attr('style', 'background:-webkit-linear-gradient(top,' + stopList + '); background: -o-linear-gradient(top,' + stopList + '); background: -ms-linear-gradient(top,' + stopList + '); background:-moz-linear-gradient(top,' + stopList + '); -webkit-linear-gradient(top,' + stopList + '); background:linear-gradient(to bottom,' + stopList + '); ');
  406. }
  407. cal.find('div.colpick_hue').on('mousedown touchstart', downHue);
  408. options.newColor = cal.find('div.colpick_new_color');
  409. options.currentColor = cal.find('div.colpick_current_color');
  410. //Store options and fill with default color
  411. cal.data('colpick', options);
  412. fillRGBFields(options.color, cal.get(0));
  413. fillHSBFields(options.color, cal.get(0));
  414. fillHexFields(options.color, cal.get(0));
  415. setHue(options.color, cal.get(0));
  416. setSelector(options.color, cal.get(0));
  417. setCurrentColor(options.color, cal.get(0));
  418. setNewColor(options.color, cal.get(0));
  419. //Append to body if flat=false, else show in place
  420. if (options.flat) {
  421. cal.appendTo(options.appendTo || this).show();
  422. cal.css(options.styles || {
  423. position: 'relative',
  424. display: 'block'
  425. });
  426. } else {
  427. cal.appendTo(options.appendTo || document.body);
  428. $(this).on(options.showEvent, show);
  429. cal.css(options.styles || {
  430. position: 'absolute'
  431. });
  432. }
  433. }
  434. });
  435. },
  436. //Shows the picker
  437. showPicker: function () {
  438. return this.each(function () {
  439. if ($(this).data('colpickId')) {
  440. show.apply(this);
  441. }
  442. });
  443. },
  444. //Hides the picker
  445. hidePicker: function () {
  446. return this.each(function () {
  447. if ($(this).data('colpickId')) {
  448. hide.apply(this);
  449. }
  450. });
  451. },
  452. //Sets a color as new and current (default)
  453. setColor: function (col, setCurrent) {
  454. setCurrent = (typeof setCurrent === "undefined") ? 1 : setCurrent;
  455. if (typeof col == 'string') {
  456. col = hexToHsb(col);
  457. } else if (col.r != undefined && col.g != undefined && col.b != undefined) {
  458. col = rgbToHsb(col);
  459. } else if (col.h != undefined && col.s != undefined && col.b != undefined) {
  460. col = fixHSB(col);
  461. } else {
  462. return this;
  463. }
  464. return this.each(function () {
  465. if ($(this).data('colpickId')) {
  466. var cal = $('#' + $(this).data('colpickId'));
  467. cal.data('colpick').color = col;
  468. cal.data('colpick').origColor = col;
  469. fillRGBFields(col, cal.get(0));
  470. fillHSBFields(col, cal.get(0));
  471. fillHexFields(col, cal.get(0));
  472. setHue(col, cal.get(0));
  473. setSelector(col, cal.get(0));
  474. setNewColor(col, cal.get(0));
  475. cal.data('colpick').onChange.apply(cal.parent(), [col, hsbToHex(col), hsbToRgb(col), cal.data('colpick').el, 1]);
  476. if (setCurrent) {
  477. setCurrentColor(col, cal.get(0));
  478. }
  479. }
  480. });
  481. },
  482. destroy: function () {
  483. $('#' + $(this).data('colpickId')).remove();
  484. }
  485. };
  486. }();
  487. //Color space conversions
  488. var hexToRgb = function (hex) {
  489. hex = parseInt(((hex.indexOf('#') > -1) ? hex.substring(1) : hex), 16);
  490. return {r: hex >> 16, g: (hex & 0x00FF00) >> 8, b: (hex & 0x0000FF)};
  491. };
  492. var hexToHsb = function (hex) {
  493. return rgbToHsb(hexToRgb(hex));
  494. };
  495. var rgbToHsb = function (rgb) {
  496. var hsb = {h: 0, s: 0, b: 0};
  497. var min = Math.min(rgb.r, rgb.g, rgb.b);
  498. var max = Math.max(rgb.r, rgb.g, rgb.b);
  499. var delta = max - min;
  500. hsb.b = max;
  501. hsb.s = max != 0 ? 255 * delta / max : 0;
  502. if (hsb.s != 0) {
  503. if (rgb.r == max) hsb.h = (rgb.g - rgb.b) / delta;
  504. else if (rgb.g == max) hsb.h = 2 + (rgb.b - rgb.r) / delta;
  505. else hsb.h = 4 + (rgb.r - rgb.g) / delta;
  506. } else hsb.h = -1;
  507. hsb.h *= 60;
  508. if (hsb.h < 0) hsb.h += 360;
  509. hsb.s *= 100 / 255;
  510. hsb.b *= 100 / 255;
  511. return hsb;
  512. };
  513. var hsbToRgb = function (hsb) {
  514. var rgb = {};
  515. var h = hsb.h;
  516. var s = hsb.s * 255 / 100;
  517. var v = hsb.b * 255 / 100;
  518. if (s == 0) {
  519. rgb.r = rgb.g = rgb.b = v;
  520. } else {
  521. var t1 = v;
  522. var t2 = (255 - s) * v / 255;
  523. var t3 = (t1 - t2) * (h % 60) / 60;
  524. if (h == 360) h = 0;
  525. if (h < 60) {
  526. rgb.r = t1;
  527. rgb.b = t2;
  528. rgb.g = t2 + t3
  529. }
  530. else if (h < 120) {
  531. rgb.g = t1;
  532. rgb.b = t2;
  533. rgb.r = t1 - t3
  534. }
  535. else if (h < 180) {
  536. rgb.g = t1;
  537. rgb.r = t2;
  538. rgb.b = t2 + t3
  539. }
  540. else if (h < 240) {
  541. rgb.b = t1;
  542. rgb.r = t2;
  543. rgb.g = t1 - t3
  544. }
  545. else if (h < 300) {
  546. rgb.b = t1;
  547. rgb.g = t2;
  548. rgb.r = t2 + t3
  549. }
  550. else if (h < 360) {
  551. rgb.r = t1;
  552. rgb.g = t2;
  553. rgb.b = t1 - t3
  554. }
  555. else {
  556. rgb.r = 0;
  557. rgb.g = 0;
  558. rgb.b = 0
  559. }
  560. }
  561. return {r: Math.round(rgb.r), g: Math.round(rgb.g), b: Math.round(rgb.b)};
  562. };
  563. var rgbToHex = function (rgb) {
  564. var hex = [
  565. rgb.r.toString(16),
  566. rgb.g.toString(16),
  567. rgb.b.toString(16)
  568. ];
  569. $.each(hex, function (nr, val) {
  570. if (val.length == 1) {
  571. hex[nr] = '0' + val;
  572. }
  573. });
  574. return hex.join('');
  575. };
  576. var hsbToHex = function (hsb) {
  577. return rgbToHex(hsbToRgb(hsb));
  578. };
  579. $.fn.extend({
  580. colpick: colpick.init,
  581. colpickHide: colpick.hidePicker,
  582. colpickShow: colpick.showPicker,
  583. colpickSetColor: colpick.setColor,
  584. colpickDestroy: colpick.destroy
  585. });
  586. $.extend({
  587. colpick: {
  588. rgbToHex: rgbToHex,
  589. rgbToHsb: rgbToHsb,
  590. hsbToHex: hsbToHex,
  591. hsbToRgb: hsbToRgb,
  592. hexToHsb: hexToHsb,
  593. hexToRgb: hexToRgb
  594. }
  595. });
  596. }));