colpick.js 22 KB

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