farbtastic.js 9.7 KB

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