block_coloration.html 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <script src='https://code.jquery.com/jquery-2.1.3.min.js'></script>
  2. <style>
  3. span {
  4. display: inline-block;
  5. }
  6. </style>
  7. <div>
  8. <span>Control:</span>
  9. <span>Decisions: 330</span>
  10. <span>Iteration: 300</span>
  11. <span>Functions: 210</span>
  12. <span>Properties:</span>
  13. <span>Manipulations: 240</span>
  14. <span>Calculations: 270</span>
  15. <span>Special:</span>
  16. <span>Generic Function: 180</span>
  17. <span>Plotting: 170</span>
  18. <span>Utility: 160</span>
  19. <span>Data:</span>
  20. <span>Simple Data: 100</span>
  21. <span>Lists: 30</span>
  22. <span>Dictionaries: 0</span>
  23. <span>Concrete:</span>
  24. <span>UNRESERVED: 75</span>
  25. <span>Weather: 70</span>
  26. <span>Earthquakes: 65</span>
  27. <span>Stocks: 60</span>
  28. <span>Crime: 55</span>
  29. <span>Books: 50</span>
  30. <span>ENDING: 330</span>
  31. </div>
  32. <script>
  33. // Changes the RGB/HEX temporarily to a HSL-Value, modifies that value
  34. // and changes it back to RGB/HEX.
  35. function changeHue(rgb, degree) {
  36. var hsl = rgbToHSL(rgb);
  37. hsl.h += degree;
  38. if (hsl.h > 360) {
  39. hsl.h -= 360;
  40. }
  41. else if (hsl.h < 0) {
  42. hsl.h += 360;
  43. }
  44. return hslToRGB(hsl);
  45. }
  46. // exepcts a string and returns an object
  47. function rgbToHSL(rgb) {
  48. // strip the leading # if it's there
  49. rgb = rgb.replace(/^\s*#|\s*$/g, '');
  50. // convert 3 char codes --> 6, e.g. `E0F` --> `EE00FF`
  51. if(rgb.length == 3){
  52. rgb = rgb.replace(/(.)/g, '$1$1');
  53. }
  54. var r = parseInt(rgb.substr(0, 2), 16) / 255,
  55. g = parseInt(rgb.substr(2, 2), 16) / 255,
  56. b = parseInt(rgb.substr(4, 2), 16) / 255,
  57. cMax = Math.max(r, g, b),
  58. cMin = Math.min(r, g, b),
  59. delta = cMax - cMin,
  60. l = (cMax + cMin) / 2,
  61. h = 0,
  62. s = 0;
  63. if (delta == 0) {
  64. h = 0;
  65. }
  66. else if (cMax == r) {
  67. h = 60 * (((g - b) / delta) % 6);
  68. }
  69. else if (cMax == g) {
  70. h = 60 * (((b - r) / delta) + 2);
  71. }
  72. else {
  73. h = 60 * (((r - g) / delta) + 4);
  74. }
  75. if (delta == 0) {
  76. s = 0;
  77. }
  78. else {
  79. s = (delta/(1-Math.abs(2*l - 1)))
  80. }
  81. return {
  82. h: h,
  83. s: s,
  84. l: l
  85. }
  86. }
  87. // expects an object and returns a string
  88. function hslToRGB(hsl) {
  89. var h = hsl.h,
  90. s = hsl.s,
  91. l = hsl.l,
  92. c = (1 - Math.abs(2*l - 1)) * s,
  93. x = c * ( 1 - Math.abs((h / 60 ) % 2 - 1 )),
  94. m = l - c/ 2,
  95. r, g, b;
  96. if (h < 60) {
  97. r = c;
  98. g = x;
  99. b = 0;
  100. }
  101. else if (h < 120) {
  102. r = x;
  103. g = c;
  104. b = 0;
  105. }
  106. else if (h < 180) {
  107. r = 0;
  108. g = c;
  109. b = x;
  110. }
  111. else if (h < 240) {
  112. r = 0;
  113. g = x;
  114. b = c;
  115. }
  116. else if (h < 300) {
  117. r = x;
  118. g = 0;
  119. b = c;
  120. }
  121. else {
  122. r = c;
  123. g = 0;
  124. b = x;
  125. }
  126. r = normalize_rgb_value(r, m);
  127. g = normalize_rgb_value(g, m);
  128. b = normalize_rgb_value(b, m);
  129. return rgbToHex(r,g,b);
  130. }
  131. function normalize_rgb_value(color, m) {
  132. color = Math.floor((color + m) * 255);
  133. if (color < 0) {
  134. color = 0;
  135. }
  136. return color;
  137. }
  138. function rgbToHex(r, g, b) {
  139. return "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
  140. }
  141. $("span").each(function(i, e) {
  142. var i = parseInt($(e).text().split(":")[1])-130;
  143. $(e).css("background-color",
  144. changeHue('5ba55b',
  145. i)).after("<br/>");
  146. });</script>