PHP/JavaScript webapp to analyse spending habits
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

jquery.colorhelpers.js 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /* Plugin for jQuery for working with colors.
  2. *
  3. * Version 1.0.
  4. *
  5. * Inspiration from jQuery color animation plugin by John Resig.
  6. *
  7. * Released under the MIT license by Ole Laursen, October 2009.
  8. *
  9. * Examples:
  10. *
  11. * $.color.parse("#fff").scale('rgb', 0.25).add('a', -0.5).toString()
  12. * var c = $.color.extract($("#mydiv"), 'background-color');
  13. * console.log(c.r, c.g, c.b, c.a);
  14. * $.color.make(100, 50, 25, 0.4).toString() // returns "rgba(100,50,25,0.4)"
  15. *
  16. * Note that .scale() and .add() work in-place instead of returning
  17. * new objects.
  18. */
  19. (function() {
  20. jQuery.color = {};
  21. // construct color object with some convenient chainable helpers
  22. jQuery.color.make = function (r, g, b, a) {
  23. var o = {};
  24. o.r = r || 0;
  25. o.g = g || 0;
  26. o.b = b || 0;
  27. o.a = a != null ? a : 1;
  28. o.add = function (c, d) {
  29. for (var i = 0; i < c.length; ++i)
  30. o[c.charAt(i)] += d;
  31. return o.normalize();
  32. };
  33. o.scale = function (c, f) {
  34. for (var i = 0; i < c.length; ++i)
  35. o[c.charAt(i)] *= f;
  36. return o.normalize();
  37. };
  38. o.toString = function () {
  39. if (o.a >= 1.0) {
  40. return "rgb("+[o.r, o.g, o.b].join(",")+")";
  41. } else {
  42. return "rgba("+[o.r, o.g, o.b, o.a].join(",")+")";
  43. }
  44. };
  45. o.normalize = function () {
  46. function clamp(min, value, max) {
  47. return value < min ? min: (value > max ? max: value);
  48. }
  49. o.r = clamp(0, parseInt(o.r), 255);
  50. o.g = clamp(0, parseInt(o.g), 255);
  51. o.b = clamp(0, parseInt(o.b), 255);
  52. o.a = clamp(0, o.a, 1);
  53. return o;
  54. };
  55. o.clone = function () {
  56. return jQuery.color.make(o.r, o.b, o.g, o.a);
  57. };
  58. return o.normalize();
  59. }
  60. // extract CSS color property from element, going up in the DOM
  61. // if it's "transparent"
  62. jQuery.color.extract = function (elem, css) {
  63. var c;
  64. do {
  65. c = elem.css(css).toLowerCase();
  66. // keep going until we find an element that has color, or
  67. // we hit the body
  68. if (c != '' && c != 'transparent')
  69. break;
  70. elem = elem.parent();
  71. } while (!jQuery.nodeName(elem.get(0), "body"));
  72. // catch Safari's way of signalling transparent
  73. if (c == "rgba(0, 0, 0, 0)")
  74. c = "transparent";
  75. return jQuery.color.parse(c);
  76. }
  77. // parse CSS color string (like "rgb(10, 32, 43)" or "#fff"),
  78. // returns color object
  79. jQuery.color.parse = function (str) {
  80. var res, m = jQuery.color.make;
  81. // Look for rgb(num,num,num)
  82. if (res = /rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(str))
  83. return m(parseInt(res[1], 10), parseInt(res[2], 10), parseInt(res[3], 10));
  84. // Look for rgba(num,num,num,num)
  85. if (res = /rgba\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]+(?:\.[0-9]+)?)\s*\)/.exec(str))
  86. return m(parseInt(res[1], 10), parseInt(res[2], 10), parseInt(res[3], 10), parseFloat(res[4]));
  87. // Look for rgb(num%,num%,num%)
  88. if (res = /rgb\(\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*\)/.exec(str))
  89. return m(parseFloat(res[1])*2.55, parseFloat(res[2])*2.55, parseFloat(res[3])*2.55);
  90. // Look for rgba(num%,num%,num%,num)
  91. if (res = /rgba\(\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\s*\)/.exec(str))
  92. return m(parseFloat(res[1])*2.55, parseFloat(res[2])*2.55, parseFloat(res[3])*2.55, parseFloat(res[4]));
  93. // Look for #a0b1c2
  94. if (res = /#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/.exec(str))
  95. return m(parseInt(res[1], 16), parseInt(res[2], 16), parseInt(res[3], 16));
  96. // Look for #fff
  97. if (res = /#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])/.exec(str))
  98. return m(parseInt(res[1]+res[1], 16), parseInt(res[2]+res[2], 16), parseInt(res[3]+res[3], 16));
  99. // Otherwise, we're most likely dealing with a named color
  100. var name = jQuery.trim(str).toLowerCase();
  101. if (name == "transparent")
  102. return m(255, 255, 255, 0);
  103. else {
  104. res = lookupColors[name];
  105. return m(res[0], res[1], res[2]);
  106. }
  107. }
  108. var lookupColors = {
  109. aqua:[0,255,255],
  110. azure:[240,255,255],
  111. beige:[245,245,220],
  112. black:[0,0,0],
  113. blue:[0,0,255],
  114. brown:[165,42,42],
  115. cyan:[0,255,255],
  116. darkblue:[0,0,139],
  117. darkcyan:[0,139,139],
  118. darkgrey:[169,169,169],
  119. darkgreen:[0,100,0],
  120. darkkhaki:[189,183,107],
  121. darkmagenta:[139,0,139],
  122. darkolivegreen:[85,107,47],
  123. darkorange:[255,140,0],
  124. darkorchid:[153,50,204],
  125. darkred:[139,0,0],
  126. darksalmon:[233,150,122],
  127. darkviolet:[148,0,211],
  128. fuchsia:[255,0,255],
  129. gold:[255,215,0],
  130. green:[0,128,0],
  131. indigo:[75,0,130],
  132. khaki:[240,230,140],
  133. lightblue:[173,216,230],
  134. lightcyan:[224,255,255],
  135. lightgreen:[144,238,144],
  136. lightgrey:[211,211,211],
  137. lightpink:[255,182,193],
  138. lightyellow:[255,255,224],
  139. lime:[0,255,0],
  140. magenta:[255,0,255],
  141. maroon:[128,0,0],
  142. navy:[0,0,128],
  143. olive:[128,128,0],
  144. orange:[255,165,0],
  145. pink:[255,192,203],
  146. purple:[128,0,128],
  147. violet:[128,0,128],
  148. red:[255,0,0],
  149. silver:[192,192,192],
  150. white:[255,255,255],
  151. yellow:[255,255,0]
  152. };
  153. })();