File size: 1,040 Bytes
7e3630c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import chalk from "chalk";
import type { PixelData } from "./types.js";

const HALF_BLOCK = "\u2584";

export function renderHalfBlock(pixels: PixelData): string {
  const { data, info } = pixels;
  const { width, height, channels } = info;

  let result = "";
  for (let y = 0; y < height - 1; y += 2) {
    for (let x = 0; x < width; x++) {
      const topPixelIndex = (y * width + x) * channels;
      const bottomPixelIndex = ((y + 1) * width + x) * channels;

      const r = data[topPixelIndex] as number;
      const g = data[topPixelIndex + 1] as number;
      const b = data[topPixelIndex + 2] as number;
      const a = channels === 4 ? (data[topPixelIndex + 3] as number) : 255;

      const r2 = data[bottomPixelIndex] as number;
      const g2 = data[bottomPixelIndex + 1] as number;
      const b2 = data[bottomPixelIndex + 2] as number;

      result +=
        a === 0
          ? chalk.reset(" ")
          : chalk.bgRgb(r, g, b).rgb(r2, g2, b2)(HALF_BLOCK);
    }

    result += "\n";
  }

  return result.slice(0, -1);
}