Guest User

Untitled

a guest
Oct 24th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width">
  6. <title>JS Bin</title>
  7. <style id="jsbin-css">
  8. #app {
  9. font-family: 'Courier'
  10. }
  11.  
  12. #app li {
  13. list-style: none;
  14. }
  15.  
  16. #app div {
  17. border: solid 1px #999;
  18. color: white;
  19. }
  20. </style>
  21. </head>
  22. <body>
  23. <div id="app"></div>
  24. <script id="jsbin-javascript">
  25. "use strict";
  26.  
  27. var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
  28.  
  29. function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  30.  
  31. function hexToRGB(hex) {
  32. var match = hex.replace(/#/, '').match(/.{1,2}/g);
  33.  
  34. return {
  35. r: parseInt(match[0], 16),
  36. g: parseInt(match[1], 16),
  37. b: parseInt(match[2], 16)
  38. };
  39. }
  40.  
  41. var rgbToCSSString = function rgbToCSSString(_ref) {
  42. var r = _ref.r;
  43. var g = _ref.g;
  44. var b = _ref.b;
  45. return "rgb(" + r + "," + g + "," + b + ")";
  46. };
  47.  
  48. function interpolateColors(left, right, percentage) {
  49. return ["r", "g", "b"].reduce(function (acc, c) {
  50. return _extends({}, acc, _defineProperty({}, c, Math.round(left[c] + (right[c] - left[c]) * percentage / 100)));
  51. }, {});
  52. }
  53.  
  54. // usage:
  55.  
  56. var startColor = hexToRGB("#FF0033");
  57. var endColor = hexToRGB("#FFCC33");
  58.  
  59. function render() {
  60. var colors = [];
  61.  
  62. for (var ratio = 0; ratio < 100; ratio += 2) {
  63. colors.push(rgbToCSSString(interpolateColors(startColor, endColor, ratio)));
  64. }
  65.  
  66. var color = function color(x) {
  67. return "\n <li>\n <div style=\"background-color: " + x + "; padding: 5px 10px\">\n </div>\n </li>\n ";
  68. };
  69.  
  70. var template = "<ul>" + colors.map(color).join('') + "</ul>";
  71.  
  72. document.getElementById('app').innerHTML = template;
  73. }
  74.  
  75. render();
  76. </script>
  77.  
  78.  
  79. <script id="jsbin-source-css" type="text/css">#app {
  80. font-family: 'Courier'
  81. }
  82.  
  83. #app li {
  84. list-style: none;
  85. }
  86.  
  87. #app div {
  88. border: solid 1px #999;
  89. color: white;
  90. }</script>
  91.  
  92. <script id="jsbin-source-javascript" type="text/javascript">function hexToRGB(hex) {
  93. const match = hex.replace(/#/,'').match(/.{1,2}/g)
  94.  
  95. return {
  96. r: parseInt(match[0], 16),
  97. g: parseInt(match[1], 16),
  98. b: parseInt(match[2], 16)
  99. }
  100. }
  101.  
  102. const rgbToCSSString = ({ r, g, b }) => `rgb(${r},${g},${b})`
  103.  
  104. function interpolateColors(left, right, percentage) {
  105. return ["r", "g", "b"].reduce(
  106. (acc, c) => ({
  107. ...acc,
  108. [c]: Math.round(left[c] + (right[c] - left[c]) * percentage / 100)
  109. }),
  110. {}
  111. )
  112. }
  113.  
  114. // usage:
  115.  
  116. const startColor = hexToRGB("#FF0033")
  117. const endColor = hexToRGB("#FFCC33")
  118.  
  119. function render() {
  120. const colors = []
  121.  
  122. for (let ratio = 0; ratio < 100; ratio+=2) {
  123. colors.push(
  124. rgbToCSSString(
  125. interpolateColors(startColor, endColor, ratio)
  126. )
  127. )
  128. }
  129.  
  130. const color = x => `
  131. <li>
  132. <div style="background-color: ${x}; padding: 5px 10px">
  133. </div>
  134. </li>
  135. `
  136.  
  137. const template = `<ul>${colors.map(color).join('')}</ul>`
  138.  
  139. document.getElementById('app').innerHTML = template
  140. }
  141.  
  142. render()</script></body>
  143. </html>
Add Comment
Please, Sign In to add comment