farbtastic.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. // JAMIE WAS HERE rgb(r, g, b) modification
  85. if (!unpack && color) {
  86. color = fb.RGBToHex(color);
  87. if (color) unpack = fb.unpack(color);
  88. }
  89. // JAMIE WAS HERE rgb(r, g, b) modification
  90. if (fb.color != color && unpack) {
  91. fb.color = color;
  92. fb.rgb = unpack;
  93. fb.hsl = fb.RGBToHSL(fb.rgb);
  94. fb.updateDisplay();
  95. }
  96. return this;
  97. }
  98. /**
  99. * Change color with HSL triplet [0..1, 0..1, 0..1]
  100. */
  101. fb.setHSL = function (hsl) {
  102. fb.hsl = hsl;
  103. fb.rgb = fb.HSLToRGB(hsl);
  104. fb.color = fb.pack(fb.rgb);
  105. fb.updateDisplay();
  106. return this;
  107. }
  108. /////////////////////////////////////////////////////
  109. /**
  110. * Retrieve the coordinates of the given event relative to the center
  111. * of the widget.
  112. */
  113. fb.widgetCoords = function (event) {
  114. var x, y;
  115. var el = event.target || event.srcElement;
  116. var reference = fb.wheel;
  117. if (typeof event.offsetX != 'undefined') {
  118. // Use offset coordinates and find common offsetParent
  119. var pos = { x: event.offsetX, y: event.offsetY };
  120. // Send the coordinates upwards through the offsetParent chain.
  121. var e = el;
  122. while (e) {
  123. e.mouseX = pos.x;
  124. e.mouseY = pos.y;
  125. pos.x += e.offsetLeft;
  126. pos.y += e.offsetTop;
  127. e = e.offsetParent;
  128. }
  129. // Look for the coordinates starting from the wheel widget.
  130. var e = reference;
  131. var offset = { x: 0, y: 0 }
  132. while (e) {
  133. if (typeof e.mouseX != 'undefined') {
  134. x = e.mouseX - offset.x;
  135. y = e.mouseY - offset.y;
  136. break;
  137. }
  138. offset.x += e.offsetLeft;
  139. offset.y += e.offsetTop;
  140. e = e.offsetParent;
  141. }
  142. // Reset stored coordinates
  143. e = el;
  144. while (e) {
  145. e.mouseX = undefined;
  146. e.mouseY = undefined;
  147. e = e.offsetParent;
  148. }
  149. }
  150. else {
  151. // Use absolute coordinates
  152. var pos = fb.absolutePosition(reference);
  153. x = (event.pageX || 0*(event.clientX + $('html').get(0).scrollLeft)) - pos.x;
  154. y = (event.pageY || 0*(event.clientY + $('html').get(0).scrollTop)) - pos.y;
  155. }
  156. // Subtract distance to middle
  157. return { x: x - fb.width / 2, y: y - fb.width / 2 };
  158. }
  159. /**
  160. * Mousedown handler
  161. */
  162. fb.mousedown = function (event) {
  163. // Capture mouse
  164. if (!document.dragging) {
  165. $(document).bind('mousemove', fb.mousemove).bind('mouseup', fb.mouseup);
  166. document.dragging = true;
  167. }
  168. // Check which area is being dragged
  169. var pos = fb.widgetCoords(event);
  170. fb.circleDrag = Math.max(Math.abs(pos.x), Math.abs(pos.y)) * 2 > fb.square;
  171. // Process
  172. fb.mousemove(event);
  173. return false;
  174. }
  175. /**
  176. * Mousemove handler
  177. */
  178. fb.mousemove = function (event) {
  179. // Get coordinates relative to color picker center
  180. var pos = fb.widgetCoords(event);
  181. // Set new HSL parameters
  182. if (fb.circleDrag) {
  183. var hue = Math.atan2(pos.x, -pos.y) / 6.28;
  184. if (hue < 0) hue += 1;
  185. fb.setHSL([hue, fb.hsl[1], fb.hsl[2]]);
  186. }
  187. else {
  188. var sat = Math.max(0, Math.min(1, -(pos.x / fb.square) + .5));
  189. var lum = Math.max(0, Math.min(1, -(pos.y / fb.square) + .5));
  190. fb.setHSL([fb.hsl[0], sat, lum]);
  191. }
  192. return false;
  193. }
  194. /**
  195. * Mouseup handler
  196. */
  197. fb.mouseup = function () {
  198. // Uncapture mouse
  199. $(document).unbind('mousemove', fb.mousemove);
  200. $(document).unbind('mouseup', fb.mouseup);
  201. document.dragging = false;
  202. }
  203. /**
  204. * Update the markers and styles
  205. */
  206. fb.updateDisplay = function () {
  207. // Markers
  208. var angle = fb.hsl[0] * 6.28;
  209. $('.h-marker', e).css({
  210. left: Math.round(Math.sin(angle) * fb.radius + fb.width / 2) + 'px',
  211. top: Math.round(-Math.cos(angle) * fb.radius + fb.width / 2) + 'px'
  212. });
  213. $('.sl-marker', e).css({
  214. left: Math.round(fb.square * (.5 - fb.hsl[1]) + fb.width / 2) + 'px',
  215. top: Math.round(fb.square * (.5 - fb.hsl[2]) + fb.width / 2) + 'px'
  216. });
  217. // Saturation/Luminance gradient
  218. $('.color', e).css('backgroundColor', fb.pack(fb.HSLToRGB([fb.hsl[0], 1, 0.5])));
  219. // Linked elements or callback
  220. if (typeof fb.callback == 'object') {
  221. // Set background/foreground color
  222. $(fb.callback).css({
  223. backgroundColor: fb.color,
  224. color: fb.hsl[2] > 0.5 ? '#000' : '#fff'
  225. });
  226. // Change linked value
  227. $(fb.callback).each(function() {
  228. if (this.value && this.value != fb.color) {
  229. this.value = fb.color;
  230. }
  231. });
  232. }
  233. else if (typeof fb.callback == 'function') {
  234. fb.callback.call(fb, fb.color);
  235. }
  236. }
  237. /**
  238. * Get absolute position of element
  239. */
  240. fb.absolutePosition = function (el) {
  241. var r = { x: el.offsetLeft, y: el.offsetTop };
  242. // Resolve relative to offsetParent
  243. if (el.offsetParent) {
  244. var tmp = fb.absolutePosition(el.offsetParent);
  245. r.x += tmp.x;
  246. r.y += tmp.y;
  247. }
  248. return r;
  249. };
  250. /* Various color utility functions */
  251. fb.pack = function (rgb) {
  252. var r = Math.round(rgb[0] * 255);
  253. var g = Math.round(rgb[1] * 255);
  254. var b = Math.round(rgb[2] * 255);
  255. return '#' + (r < 16 ? '0' : '') + r.toString(16) +
  256. (g < 16 ? '0' : '') + g.toString(16) +
  257. (b < 16 ? '0' : '') + b.toString(16);
  258. }
  259. fb.unpack = function (color) {
  260. if (color.length == 7) {
  261. return [parseInt('0x' + color.substring(1, 3)) / 255,
  262. parseInt('0x' + color.substring(3, 5)) / 255,
  263. parseInt('0x' + color.substring(5, 7)) / 255];
  264. }
  265. else if (color.length == 4) {
  266. return [parseInt('0x' + color.substring(1, 2)) / 15,
  267. parseInt('0x' + color.substring(2, 3)) / 15,
  268. parseInt('0x' + color.substring(3, 4)) / 15];
  269. }
  270. }
  271. fb.HSLToRGB = function (hsl) {
  272. var m1, m2, r, g, b;
  273. var h = hsl[0], s = hsl[1], l = hsl[2];
  274. m2 = (l <= 0.5) ? l * (s + 1) : l + s - l*s;
  275. m1 = l * 2 - m2;
  276. return [this.hueToRGB(m1, m2, h+0.33333),
  277. this.hueToRGB(m1, m2, h),
  278. this.hueToRGB(m1, m2, h-0.33333)];
  279. }
  280. fb.hueToRGB = function (m1, m2, h) {
  281. h = (h < 0) ? h + 1 : ((h > 1) ? h - 1 : h);
  282. if (h * 6 < 1) return m1 + (m2 - m1) * h * 6;
  283. if (h * 2 < 1) return m2;
  284. if (h * 3 < 2) return m1 + (m2 - m1) * (0.66666 - h) * 6;
  285. return m1;
  286. }
  287. // JAMIE WAS HERE
  288. fb.RGBToHex = function(color) {
  289. var exp = new RegExp(/rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3}\s*)\)/i);
  290. if(exp.test(color)) {
  291. var results = exp.exec(color);
  292. var r = parseInt(results[1]).toString(16);
  293. var g = parseInt(results[2]).toString(16);
  294. var b = parseInt(results[3]).toString(16);
  295. if (r.length < 2) r = '0' + r;
  296. if (g.length < 2) g = '0' + g;
  297. if (b.length < 2) b = '0' + b;
  298. return '#' + r + g + b;
  299. }
  300. }
  301. // JAMIE WAS HERE
  302. fb.RGBToHSL = function (rgb) {
  303. var min, max, delta, h, s, l;
  304. var r = rgb[0], g = rgb[1], b = rgb[2];
  305. min = Math.min(r, Math.min(g, b));
  306. max = Math.max(r, Math.max(g, b));
  307. delta = max - min;
  308. l = (min + max) / 2;
  309. s = 0;
  310. if (l > 0 && l < 1) {
  311. s = delta / (l < 0.5 ? (2 * l) : (2 - 2 * l));
  312. }
  313. h = 0;
  314. if (delta > 0) {
  315. if (max == r && max != g) h += (g - b) / delta;
  316. if (max == g && max != b) h += (2 + (b - r) / delta);
  317. if (max == b && max != r) h += (4 + (r - g) / delta);
  318. h /= 6;
  319. }
  320. return [h, s, l];
  321. }
  322. // Install mousedown handler (the others are set on the document on-demand)
  323. $('*', e).mousedown(fb.mousedown);
  324. // Init color
  325. fb.setColor('#000000');
  326. // Set linked elements/callback
  327. if (callback) {
  328. fb.linkTo(callback);
  329. }
  330. }