farbtastic.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*!
  2. * Farbtastic: jQuery color picker plug-in v1.3u
  3. *
  4. * Licensed under the GPL license:
  5. * http://www.gnu.org/licenses/gpl.html
  6. */
  7. (function($) {
  8. $.fn.farbtastic = function (options) {
  9. $.farbtastic(this, options);
  10. return this;
  11. };
  12. $.farbtastic = function (container, callback) {
  13. var container = $(container).get(0);
  14. return container.farbtastic || (container.farbtastic = new $._farbtastic(container, callback));
  15. };
  16. $._farbtastic = function (container, callback) {
  17. // Store farbtastic object
  18. var fb = this;
  19. // Insert markup
  20. $(container).html('<div class="farbtastic"><div class="color"></div><div class="wheel"></div><div class="overlay"></div><div class="h-marker marker"></div><div class="sl-marker marker"></div></div>');
  21. var e = $('.farbtastic', container);
  22. fb.wheel = $('.wheel', container).get(0);
  23. // Dimensions
  24. fb.radius = 84;
  25. fb.square = 100;
  26. fb.width = 194;
  27. // Fix background PNGs in IE6
  28. if (navigator.appVersion.match(/MSIE [0-6]\./)) {
  29. $('*', e).each(function () {
  30. if (this.currentStyle.backgroundImage != 'none') {
  31. var image = this.currentStyle.backgroundImage;
  32. image = this.currentStyle.backgroundImage.substring(5, image.length - 2);
  33. $(this).css({
  34. 'backgroundImage': 'none',
  35. 'filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=crop, src='" + image + "')"
  36. });
  37. }
  38. });
  39. }
  40. /**
  41. * Link to the given element(s) or callback.
  42. */
  43. fb.linkTo = function (callback) {
  44. // Unbind previous nodes
  45. if (typeof fb.callback == 'object') {
  46. $(fb.callback).unbind('keyup', fb.updateValue);
  47. }
  48. // Reset color
  49. fb.color = null;
  50. // Bind callback or elements
  51. if (typeof callback == 'function') {
  52. fb.callback = callback;
  53. }
  54. else if (typeof callback == 'object' || typeof callback == 'string') {
  55. fb.callback = $(callback);
  56. fb.callback.bind('keyup', fb.updateValue);
  57. if (fb.callback.get(0).value) {
  58. fb.setColor(fb.callback.get(0).value);
  59. }
  60. }
  61. return this;
  62. };
  63. fb.updateValue = function (event) {
  64. if (this.value && this.value != fb.color) {
  65. fb.setColor(this.value);
  66. }
  67. };
  68. /**
  69. * Change color with HTML syntax #123456
  70. */
  71. fb.setColor = function (color) {
  72. var unpack = fb.unpack(color);
  73. if (fb.color != color && unpack) {
  74. fb.color = color;
  75. fb.rgb = unpack;
  76. fb.hsl = fb.RGBToHSL(fb.rgb);
  77. fb.updateDisplay();
  78. }
  79. return this;
  80. };
  81. /**
  82. * Change color with HSL triplet [0..1, 0..1, 0..1]
  83. */
  84. fb.setHSL = function (hsl) {
  85. fb.hsl = hsl;
  86. fb.rgb = fb.HSLToRGB(hsl);
  87. fb.color = fb.pack(fb.rgb);
  88. fb.updateDisplay();
  89. return this;
  90. };
  91. /////////////////////////////////////////////////////
  92. /**
  93. * Retrieve the coordinates of the given event relative to the center
  94. * of the widget.
  95. */
  96. fb.widgetCoords = function (event) {
  97. var offset = $(fb.wheel).offset();
  98. return { x: (event.pageX - offset.left) - fb.width / 2, y: (event.pageY - offset.top) - fb.width / 2 };
  99. };
  100. /**
  101. * Mousedown handler
  102. */
  103. fb.mousedown = function (event) {
  104. // Capture mouse
  105. if (!document.dragging) {
  106. $(document).bind('mousemove', fb.mousemove).bind('mouseup', fb.mouseup);
  107. document.dragging = true;
  108. }
  109. // Check which area is being dragged
  110. var pos = fb.widgetCoords(event);
  111. fb.circleDrag = Math.max(Math.abs(pos.x), Math.abs(pos.y)) * 2 > fb.square;
  112. // Process
  113. fb.mousemove(event);
  114. return false;
  115. };
  116. /**
  117. * Mousemove handler
  118. */
  119. fb.mousemove = function (event) {
  120. // Get coordinates relative to color picker center
  121. var pos = fb.widgetCoords(event);
  122. // Set new HSL parameters
  123. if (fb.circleDrag) {
  124. var hue = Math.atan2(pos.x, -pos.y) / 6.28;
  125. if (hue < 0) hue += 1;
  126. fb.setHSL([hue, fb.hsl[1], fb.hsl[2]]);
  127. }
  128. else {
  129. var sat = Math.max(0, Math.min(1, -(pos.x / fb.square) + .5));
  130. var lum = Math.max(0, Math.min(1, -(pos.y / fb.square) + .5));
  131. fb.setHSL([fb.hsl[0], sat, lum]);
  132. }
  133. return false;
  134. };
  135. /**
  136. * Mouseup handler
  137. */
  138. fb.mouseup = function () {
  139. // Uncapture mouse
  140. $(document).unbind('mousemove', fb.mousemove);
  141. $(document).unbind('mouseup', fb.mouseup);
  142. document.dragging = false;
  143. };
  144. /**
  145. * Update the markers and styles
  146. */
  147. fb.updateDisplay = function () {
  148. // Markers
  149. var angle = fb.hsl[0] * 6.28;
  150. $('.h-marker', e).css({
  151. left: Math.round(Math.sin(angle) * fb.radius + fb.width / 2) + 'px',
  152. top: Math.round(-Math.cos(angle) * fb.radius + fb.width / 2) + 'px'
  153. });
  154. $('.sl-marker', e).css({
  155. left: Math.round(fb.square * (.5 - fb.hsl[1]) + fb.width / 2) + 'px',
  156. top: Math.round(fb.square * (.5 - fb.hsl[2]) + fb.width / 2) + 'px'
  157. });
  158. // Saturation/Luminance gradient
  159. $('.color', e).css('backgroundColor', fb.pack(fb.HSLToRGB([fb.hsl[0], 1, 0.5])));
  160. // Linked elements or callback
  161. if (typeof fb.callback == 'object') {
  162. // Set background/foreground color
  163. $(fb.callback).css({
  164. backgroundColor: fb.color,
  165. color: fb.hsl[2] > 0.5 ? '#000' : '#fff'
  166. });
  167. // Change linked value
  168. $(fb.callback).each(function() {
  169. if (this.value && this.value != fb.color) {
  170. this.value = fb.color;
  171. }
  172. });
  173. }
  174. else if (typeof fb.callback == 'function') {
  175. fb.callback.call(fb, fb.color);
  176. }
  177. };
  178. /* Various color utility functions */
  179. fb.pack = function (rgb) {
  180. var r = Math.round(rgb[0] * 255);
  181. var g = Math.round(rgb[1] * 255);
  182. var b = Math.round(rgb[2] * 255);
  183. return '#' + (r < 16 ? '0' : '') + r.toString(16) +
  184. (g < 16 ? '0' : '') + g.toString(16) +
  185. (b < 16 ? '0' : '') + b.toString(16);
  186. };
  187. fb.unpack = function (color) {
  188. if (color.length == 7) {
  189. return [parseInt('0x' + color.substring(1, 3)) / 255,
  190. parseInt('0x' + color.substring(3, 5)) / 255,
  191. parseInt('0x' + color.substring(5, 7)) / 255];
  192. }
  193. else if (color.length == 4) {
  194. return [parseInt('0x' + color.substring(1, 2)) / 15,
  195. parseInt('0x' + color.substring(2, 3)) / 15,
  196. parseInt('0x' + color.substring(3, 4)) / 15];
  197. }
  198. };
  199. fb.HSLToRGB = function (hsl) {
  200. var m1, m2, r, g, b;
  201. var h = hsl[0], s = hsl[1], l = hsl[2];
  202. m2 = (l <= 0.5) ? l * (s + 1) : l + s - l*s;
  203. m1 = l * 2 - m2;
  204. return [this.hueToRGB(m1, m2, h+0.33333),
  205. this.hueToRGB(m1, m2, h),
  206. this.hueToRGB(m1, m2, h-0.33333)];
  207. };
  208. fb.hueToRGB = function (m1, m2, h) {
  209. h = (h < 0) ? h + 1 : ((h > 1) ? h - 1 : h);
  210. if (h * 6 < 1) return m1 + (m2 - m1) * h * 6;
  211. if (h * 2 < 1) return m2;
  212. if (h * 3 < 2) return m1 + (m2 - m1) * (0.66666 - h) * 6;
  213. return m1;
  214. };
  215. fb.RGBToHSL = function (rgb) {
  216. var min, max, delta, h, s, l;
  217. var r = rgb[0], g = rgb[1], b = rgb[2];
  218. min = Math.min(r, Math.min(g, b));
  219. max = Math.max(r, Math.max(g, b));
  220. delta = max - min;
  221. l = (min + max) / 2;
  222. s = 0;
  223. if (l > 0 && l < 1) {
  224. s = delta / (l < 0.5 ? (2 * l) : (2 - 2 * l));
  225. }
  226. h = 0;
  227. if (delta > 0) {
  228. if (max == r && max != g) h += (g - b) / delta;
  229. if (max == g && max != b) h += (2 + (b - r) / delta);
  230. if (max == b && max != r) h += (4 + (r - g) / delta);
  231. h /= 6;
  232. }
  233. return [h, s, l];
  234. };
  235. // Install mousedown handler (the others are set on the document on-demand)
  236. $('*', e).mousedown(fb.mousedown);
  237. // Init color
  238. fb.setColor('#000000');
  239. // Set linked elements/callback
  240. if (callback) {
  241. fb.linkTo(callback);
  242. }
  243. };
  244. })(jQuery);