farbtastic.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. // $Id: farbtastic.js,v 1.1 2009/10/01 18:28:22 ccheng Exp $
  2. // Farbtastic 1.2
  3. jQuery.fn.farbtastic = function (callback) {
  4. $.farbtastic(this, callback);
  5. return this;
  6. };
  7. jQuery.farbtastic = function (container, callback) {
  8. var container = $(container).get(0);
  9. return container.farbtastic || (container.farbtastic = new jQuery._farbtastic(container, callback));
  10. }
  11. jQuery._farbtastic = function (container, callback) {
  12. // Store farbtastic object
  13. var fb = this;
  14. // Insert markup
  15. $(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>');
  16. var e = $('.farbtastic', container);
  17. fb.wheel = $('.wheel', container).get(0);
  18. // Dimensions
  19. fb.radius = 84;
  20. fb.square = 100;
  21. fb.width = 194;
  22. // Fix background PNGs in IE6
  23. if (navigator.appVersion.match(/MSIE [0-6]\./)) {
  24. $('*', e).each(function () {
  25. if (this.currentStyle.backgroundImage != 'none') {
  26. var image = this.currentStyle.backgroundImage;
  27. image = this.currentStyle.backgroundImage.substring(5, image.length - 2);
  28. $(this).css({
  29. 'backgroundImage': 'none',
  30. 'filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=crop, src='" + image + "')"
  31. });
  32. }
  33. });
  34. }
  35. /**
  36. * Link to the given element(s) or callback.
  37. */
  38. fb.linkTo = function (callback) {
  39. // Unbind previous nodes
  40. if (typeof fb.callback == 'object') {
  41. $(fb.callback).unbind('keyup', fb.updateValue);
  42. }
  43. // Reset color
  44. fb.color = null;
  45. // Bind callback or elements
  46. if (typeof callback == 'function') {
  47. fb.callback = callback;
  48. }
  49. else if (typeof callback == 'object' || typeof callback == 'string') {
  50. fb.callback = $(callback);
  51. fb.callback.bind('keyup', fb.updateValue);
  52. if (fb.callback.get(0).value) {
  53. fb.setColor(fb.callback.get(0).value);
  54. }
  55. }
  56. return this;
  57. }
  58. fb.updateValue = function (event) {
  59. if (this.value && this.value != fb.color) {
  60. fb.setColor(this.value);
  61. }
  62. }
  63. /**
  64. * Change color with HTML syntax #123456
  65. */
  66. fb.setColor = function (color) {
  67. var unpack = fb.unpack(color);
  68. if (fb.color != color && unpack) {
  69. fb.color = color;
  70. fb.rgb = unpack;
  71. fb.hsl = fb.RGBToHSL(fb.rgb);
  72. fb.updateDisplay();
  73. }
  74. return this;
  75. }
  76. /**
  77. * Change color with HSL triplet [0..1, 0..1, 0..1]
  78. */
  79. fb.setHSL = function (hsl) {
  80. fb.hsl = hsl;
  81. fb.rgb = fb.HSLToRGB(hsl);
  82. fb.color = fb.pack(fb.rgb);
  83. fb.updateDisplay();
  84. return this;
  85. }
  86. /////////////////////////////////////////////////////
  87. /**
  88. * Retrieve the coordinates of the given event relative to the center
  89. * of the widget.
  90. */
  91. fb.widgetCoords = function (event) {
  92. var x, y;
  93. var el = event.target || event.srcElement;
  94. var reference = fb.wheel;
  95. if (typeof event.offsetX != 'undefined') {
  96. // Use offset coordinates and find common offsetParent
  97. var pos = { x: event.offsetX, y: event.offsetY };
  98. // Send the coordinates upwards through the offsetParent chain.
  99. var e = el;
  100. while (e) {
  101. e.mouseX = pos.x;
  102. e.mouseY = pos.y;
  103. pos.x += e.offsetLeft;
  104. pos.y += e.offsetTop;
  105. e = e.offsetParent;
  106. }
  107. // Look for the coordinates starting from the wheel widget.
  108. var e = reference;
  109. var offset = { x: 0, y: 0 }
  110. while (e) {
  111. if (typeof e.mouseX != 'undefined') {
  112. x = e.mouseX - offset.x;
  113. y = e.mouseY - offset.y;
  114. break;
  115. }
  116. offset.x += e.offsetLeft;
  117. offset.y += e.offsetTop;
  118. e = e.offsetParent;
  119. }
  120. // Reset stored coordinates
  121. e = el;
  122. while (e) {
  123. e.mouseX = undefined;
  124. e.mouseY = undefined;
  125. e = e.offsetParent;
  126. }
  127. }
  128. else {
  129. // Use absolute coordinates
  130. var pos = fb.absolutePosition(reference);
  131. x = (event.pageX || 0*(event.clientX + $('html').get(0).scrollLeft)) - pos.x;
  132. y = (event.pageY || 0*(event.clientY + $('html').get(0).scrollTop)) - pos.y;
  133. }
  134. // Subtract distance to middle
  135. return { x: x - fb.width / 2, y: y - fb.width / 2 };
  136. }
  137. /**
  138. * Mousedown handler
  139. */
  140. fb.mousedown = function (event) {
  141. // Capture mouse
  142. if (!document.dragging) {
  143. $(document).bind('mousemove', fb.mousemove).bind('mouseup', fb.mouseup);
  144. document.dragging = true;
  145. }
  146. // Check which area is being dragged
  147. var pos = fb.widgetCoords(event);
  148. fb.circleDrag = Math.max(Math.abs(pos.x), Math.abs(pos.y)) * 2 > fb.square;
  149. // Process
  150. fb.mousemove(event);
  151. return false;
  152. }
  153. /**
  154. * Mousemove handler
  155. */
  156. fb.mousemove = function (event) {
  157. // Get coordinates relative to color picker center
  158. var pos = fb.widgetCoords(event);
  159. // Set new HSL parameters
  160. if (fb.circleDrag) {
  161. var hue = Math.atan2(pos.x, -pos.y) / 6.28;
  162. if (hue < 0) hue += 1;
  163. fb.setHSL([hue, fb.hsl[1], fb.hsl[2]]);
  164. }
  165. else {
  166. var sat = Math.max(0, Math.min(1, -(pos.x / fb.square) + .5));
  167. var lum = Math.max(0, Math.min(1, -(pos.y / fb.square) + .5));
  168. fb.setHSL([fb.hsl[0], sat, lum]);
  169. }
  170. return false;
  171. }
  172. /**
  173. * Mouseup handler
  174. */
  175. fb.mouseup = function () {
  176. // Uncapture mouse
  177. $(document).unbind('mousemove', fb.mousemove);
  178. $(document).unbind('mouseup', fb.mouseup);
  179. document.dragging = false;
  180. }
  181. /**
  182. * Update the markers and styles
  183. */
  184. fb.updateDisplay = function () {
  185. // Markers
  186. var angle = fb.hsl[0] * 6.28;
  187. $('.h-marker', e).css({
  188. left: Math.round(Math.sin(angle) * fb.radius + fb.width / 2) + 'px',
  189. top: Math.round(-Math.cos(angle) * fb.radius + fb.width / 2) + 'px'
  190. });
  191. $('.sl-marker', e).css({
  192. left: Math.round(fb.square * (.5 - fb.hsl[1]) + fb.width / 2) + 'px',
  193. top: Math.round(fb.square * (.5 - fb.hsl[2]) + fb.width / 2) + 'px'
  194. });
  195. // Saturation/Luminance gradient
  196. $('.color', e).css('backgroundColor', fb.pack(fb.HSLToRGB([fb.hsl[0], 1, 0.5])));
  197. // Linked elements or callback
  198. if (typeof fb.callback == 'object') {
  199. // Set background/foreground color
  200. $(fb.callback).css({
  201. backgroundColor: fb.color,
  202. color: fb.hsl[2] > 0.5 ? '#000' : '#fff'
  203. });
  204. // Change linked value
  205. $(fb.callback).each(function() {
  206. if (this.value && this.value != fb.color) {
  207. this.value = fb.color;
  208. }
  209. });
  210. }
  211. else if (typeof fb.callback == 'function') {
  212. fb.callback.call(fb, fb.color);
  213. }
  214. }
  215. /**
  216. * Get absolute position of element
  217. */
  218. fb.absolutePosition = function (el) {
  219. var r = { x: el.offsetLeft, y: el.offsetTop };
  220. // Resolve relative to offsetParent
  221. if (el.offsetParent) {
  222. var tmp = fb.absolutePosition(el.offsetParent);
  223. r.x += tmp.x;
  224. r.y += tmp.y;
  225. }
  226. return r;
  227. };
  228. /* Various color utility functions */
  229. fb.pack = function (rgb) {
  230. var r = Math.round(rgb[0] * 255);
  231. var g = Math.round(rgb[1] * 255);
  232. var b = Math.round(rgb[2] * 255);
  233. return '#' + (r < 16 ? '0' : '') + r.toString(16) +
  234. (g < 16 ? '0' : '') + g.toString(16) +
  235. (b < 16 ? '0' : '') + b.toString(16);
  236. }
  237. fb.unpack = function (color) {
  238. if (color.length == 7) {
  239. return [parseInt('0x' + color.substring(1, 3)) / 255,
  240. parseInt('0x' + color.substring(3, 5)) / 255,
  241. parseInt('0x' + color.substring(5, 7)) / 255];
  242. }
  243. else if (color.length == 4) {
  244. return [parseInt('0x' + color.substring(1, 2)) / 15,
  245. parseInt('0x' + color.substring(2, 3)) / 15,
  246. parseInt('0x' + color.substring(3, 4)) / 15];
  247. }
  248. }
  249. fb.HSLToRGB = function (hsl) {
  250. var m1, m2, r, g, b;
  251. var h = hsl[0], s = hsl[1], l = hsl[2];
  252. m2 = (l <= 0.5) ? l * (s + 1) : l + s - l*s;
  253. m1 = l * 2 - m2;
  254. return [this.hueToRGB(m1, m2, h+0.33333),
  255. this.hueToRGB(m1, m2, h),
  256. this.hueToRGB(m1, m2, h-0.33333)];
  257. }
  258. fb.hueToRGB = function (m1, m2, h) {
  259. h = (h < 0) ? h + 1 : ((h > 1) ? h - 1 : h);
  260. if (h * 6 < 1) return m1 + (m2 - m1) * h * 6;
  261. if (h * 2 < 1) return m2;
  262. if (h * 3 < 2) return m1 + (m2 - m1) * (0.66666 - h) * 6;
  263. return m1;
  264. }
  265. fb.RGBToHSL = function (rgb) {
  266. var min, max, delta, h, s, l;
  267. var r = rgb[0], g = rgb[1], b = rgb[2];
  268. min = Math.min(r, Math.min(g, b));
  269. max = Math.max(r, Math.max(g, b));
  270. delta = max - min;
  271. l = (min + max) / 2;
  272. s = 0;
  273. if (l > 0 && l < 1) {
  274. s = delta / (l < 0.5 ? (2 * l) : (2 - 2 * l));
  275. }
  276. h = 0;
  277. if (delta > 0) {
  278. if (max == r && max != g) h += (g - b) / delta;
  279. if (max == g && max != b) h += (2 + (b - r) / delta);
  280. if (max == b && max != r) h += (4 + (r - g) / delta);
  281. h /= 6;
  282. }
  283. return [h, s, l];
  284. }
  285. // Install mousedown handler (the others are set on the document on-demand)
  286. $('*', e).mousedown(fb.mousedown);
  287. // Init color
  288. fb.setColor('#000000');
  289. // Set linked elements/callback
  290. if (callback) {
  291. fb.linkTo(callback);
  292. }
  293. }