Advertisement
Guest User

Untitled

a guest
May 24th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. const ruby = "676B9C82275548C1B5737C2D9274E02F";
  2. const emerald = "9B0C8C7B4AB04DAA9786C65D9C7F479A";
  3. const sapphire = "893AC64565E64403B6BE62831F35CE70"
  4. const opal = "DA34226AED3B4111BC3BAD312231F633";
  5.  
  6. const all = [
  7. ruby,
  8. emerald,
  9. sapphire,
  10. opal
  11. ];
  12.  
  13. function splitEachN(s: string, n: number): string[] {
  14. const result = [];
  15. for (let i = 0; i < s.length; i += n) {
  16. result.push(s.slice(i, i + n));
  17. }
  18. return result;
  19. }
  20.  
  21. function splitPairs(s: string): string[] {
  22. return splitEachN(s, 2);
  23. }
  24.  
  25. function hexToInt(s: string): number {
  26. return parseInt(s, 16);
  27. }
  28.  
  29. function intToBinary8(n: number): string {
  30. const b = (n).toString(2);
  31. return ("0" as any).repeat(8 - b.length) + b;
  32. }
  33.  
  34. function binaryToInt(s: string): number {
  35. return parseInt(s, 2);
  36. }
  37.  
  38. function splitNybbles(n: number): number[] {
  39. return [(n >> 4), n & 0x0f];
  40. }
  41.  
  42. function add(n: number): (v: number) => number {
  43. return (x: number) => x + n;
  44. }
  45.  
  46. function toAscii(n: number): string {
  47. return String.fromCharCode(n);
  48. }
  49.  
  50. function compose<A, B, C>(f1: (v: A) => B, f2: (v: B) => C) {
  51. return (v: A) => {
  52. return f2(f1(v));
  53. };
  54. }
  55.  
  56. function mapFunc<A, B>(f: (v: A) => B): ((vs: A[]) => B[]) {
  57. return (vs: A[]) => {
  58. return vs.map(f);
  59. };
  60. }
  61.  
  62. function interleave(s1: string, s2: string, n?: number): string {
  63. if (s1.length !== s2.length) {
  64. throw new Error("lengths don't match");
  65. }
  66. n = n || 1;
  67. const chars: string[] = [];
  68. for (let i = 0; i < s1.length; i += n) {
  69. chars.push(s1.slice(i, i + n));
  70. chars.push(s2.slice(i, i + n));
  71. }
  72. return chars.join("");
  73. }
  74.  
  75. function flatten<T>(vss: T[][]): T[] {
  76. const output: T[] = [];
  77. vss.forEach(vs => {
  78. vs.forEach(v => output.push(v));
  79. });
  80. return output;
  81. }
  82.  
  83. function replace(olds: string, news: string): (v: string) => string {
  84. return (x => x.replace(olds, news));
  85. }
  86.  
  87. // all.forEach(a => {
  88. // all.forEach(b => {
  89. // if (a !== b) {
  90. // const input = interleave(a, b);
  91. // // console.log(input);
  92. // const binary = splitPairs(input).map(hexToInt).map(intToBinary8).join("");
  93. // console.log(binary.replace(/0/g, " "));
  94. // // console.log(splitEachN(binary, 7).map(binaryToInt).map(add(32)).map(toAscii).join(""));
  95. // // console.log("");
  96. // }
  97. // });
  98. // });
  99.  
  100. const s = sapphire;
  101. const o = opal;
  102. const r = ruby;
  103. const e = emerald;
  104.  
  105. let img = [interleave(interleave(r,o),interleave(s, e), 16)].map(input => {
  106. return splitPairs(input).map(hexToInt).map(intToBinary8).join("");
  107. }).join("").replace(/0/g, " ").replace(/1/g, "\u2588");
  108.  
  109. //img = ("0123456789abcdefghijklmnopqrstuvwxyz" as any).repeat(20).slice(0, 512);
  110.  
  111. const width = 32;
  112. const height = 16;
  113.  
  114.  
  115.  
  116. const output: string[][] = splitEachN(("." as any).repeat(width * height) as string, width).map(r => splitEachN(r, 1));
  117.  
  118. splitEachN(img, 1).forEach((c, i) => {
  119. const group = Math.floor(i / 4);
  120. const offset = i % 4;
  121.  
  122. const x = group % width;
  123. const y = Math.floor(group / width) * 4 + offset;
  124.  
  125. output[y][x] = c;
  126. });
  127.  
  128. const rows = output.map(row => row.join(""));
  129.  
  130. for (let i = 0; i < rows.length; ++i) {
  131. const newI = (i * 4) % height + Math.floor(i / 4);
  132. // console.log(newI);
  133. const row = rows[newI];
  134. console.log(row);
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement