colpick.js 20 KB

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