Advertisement
Guest User

Untitled

a guest
Sep 1st, 2015
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. var crypto = require('crypto');
  2.  
  3. function sha1hexFromString(src){
  4. var sha1hash = crypto.createHash('sha1');
  5. sha1hash.update(src, 'binary');
  6. return sha1hash.digest('hex');
  7. }
  8.  
  9. function arrayFromHexString(hexstr){
  10. var r = [];
  11. for(var i=0,j=hexstr.length; i<j; i++){
  12. r.push(parseInt(hexstr[i], 16));
  13. }
  14. return r;
  15. }
  16.  
  17. function configFromArray(arr){
  18. var nodes = [];
  19. for(var i=8,j=arr.length; i<j; i+=2){
  20. nodes.push(arr[i] + arr[i+1]);
  21. }
  22. return {
  23. red: arr[0] * 16 + arr[4],
  24. green: arr[1] * 16 + arr[5],
  25. blue: arr[2] * 16 + arr[6],
  26. alpha: arr[3] * 16 + arr[7],
  27. nodes: nodes
  28. };
  29. }
  30.  
  31. function svgFromConfig(config, width, height){
  32. width = width || config.width || 64;
  33. height = height || config.height || 64;
  34. var color = 'rgba(' + [config.red, config.green, config.blue, config.alpha].join(', ') + ')';
  35. var buf = [];
  36.  
  37. var scaleX = width / 256;
  38. var scaleY = height / 256;
  39.  
  40. buf.push('<svg width="'+width+'" height="'+height+'">');
  41. buf.push('<g transform="scale('+ scaleX + ',' + scaleY + ')">');
  42. var nodes = config.nodes;
  43. for(var i=0, j=nodes.length; i<j; i++){
  44. var size = nodes[i] * 2;
  45. var delta = 32 - nodes[i];
  46. var x = (Math.floor(i % 4) * 64) + delta;
  47. var y = (Math.floor(i / 4) * 64) + delta;
  48. buf.push('<rect width="'+ size + '" height="'+ size +'" fill="' + color + '" x="' + x + '" y="' + y + '">');
  49. buf.push('</rect>');
  50. }
  51. buf.push('</g>');
  52. buf.push('</svg>');
  53. return buf.join('\n');
  54. }
  55.  
  56. // console.log(sha1hexFromString('hmm'));
  57. // console.log(arrayFromHexString(sha1hexFromString('hmm')));
  58.  
  59. var candidates = [
  60. "a",
  61. "b",
  62. "c",
  63. "memo",
  64. "test",
  65. "foo",
  66. "memo1",
  67. "memo2",
  68. "memo3",
  69. "memo4",
  70. "memo5",
  71. "memo6",
  72. "memo7",
  73. "memo8",
  74. "memo9",
  75. "memo10",
  76. "memo11",
  77. "memo12",
  78. "memo13",
  79. "memo14",
  80. "memo15",
  81. "memo16",
  82. "memo17",
  83. "memo18",
  84. "memo19",
  85. "memo110"
  86. ];
  87.  
  88. console.log("<table>");
  89. candidates.forEach(function(s){
  90. console.log("<tr>");
  91. console.log("<td>");
  92. console.log(s);
  93. console.log("</td>");
  94. console.log("<td>");
  95. console.log(
  96. svgFromConfig(
  97. configFromArray(
  98. arrayFromHexString(
  99. sha1hexFromString(s)))));
  100. console.log("</td>");
  101. console.log("<tr>");
  102. });
  103. console.log("</table>");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement