Skip to content

Commit

Permalink
feat(text-canvas): add bar chart & image fns
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Feb 18, 2020
1 parent 8909ce0 commit 3130fe4
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
60 changes: 60 additions & 0 deletions packages/text-canvas/src/bars.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { fitClamped, fract } from "@thi.ng/math";
import { padLeft, padRight, repeat } from "@thi.ng/strings";
import { map } from "@thi.ng/transducers";
import { BARS_H, BARS_V } from "./api";

export const barChartHStr = (
height: number,
vals: Iterable<number>,
min = 0,
max = 1
) => {
const bars = [...map((x) => barVertical(height, x, min, max, ""), vals)];
const num = bars.length;
const res: string[] = [];
for (let i = 0; i < height; i++) {
let line = "";
for (let j = 0; j < num; j++) {
line += bars[j][i];
}
res.push(line);
}
return res.join("\n");
};

export const barChartVStr = (
width: number,
vals: Iterable<number>,
min = 0,
max = 1
) => [...map((x) => barHorizontal(width, x, min, max), vals)].join("\n");

export const barHorizontal = (width: number, x: number, min = 0, max = 1) =>
bar(BARS_H, width, false, x, min, max, "");

export const barVertical = (
height: number,
x: number,
min = 0,
max = 1,
delim = "\n"
) => bar(BARS_V, height, true, x, min, max, delim);

const bar = (
chars: string,
size: number,
left: boolean,
x: number,
min: number,
max: number,
delim: string
) => {
x = fitClamped(x, min, max, 0, size);
const f = (fract(x) * 9) | 0;
const full = repeat(chars[8] + delim, x | 0);
const partial = f > 0 ? chars[f] + delim : "";
size += size * delim.length;
return left
? padLeft(size, " ")(partial + full)
: padRight(size, " ")(full + partial);
};
36 changes: 36 additions & 0 deletions packages/text-canvas/src/image.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { peek } from "@thi.ng/arrays";
import { SHADES } from "./api";
import { Canvas } from "./canvas";
import { charCode, intersectRect } from "./utils";

// @ts-ignore
export const image = (
canvas: Canvas,
x: number,
y: number,
w: number,
h: number,
pixels: ArrayLike<number>,
format = canvas.format,
gamma = 2.2
) => {
x |= 0;
y |= 0;
const { buf, width } = canvas;
const clipRect = peek(canvas.clipRects);
const srcRect = { x1: x, y1: y, x2: x + w, y2: y + h, w, h };
const { x1, y1, x2, y2, w: iw, h: ih } = intersectRect(srcRect, clipRect);
if (!iw || !ih) return;
const sx = Math.max(0, x1 - x);
const sy = Math.max(0, y1 - y);
for (let yy = sy, dy = y1; dy < y2; yy++, dy++) {
let sidx = sx + yy * w;
let didx = x1 + dy * width;
for (let xx = sx, dx = x1; dx < x2; xx++, dx++) {
buf[didx++] = charCode(
SHADES[(Math.pow(pixels[sidx++] / 255, gamma) * 4) | 0],
format
);
}
}
};
2 changes: 2 additions & 0 deletions packages/text-canvas/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
export * from "./api";
export * from "./ansi";
export * from "./bars";
export * from "./canvas";
export * from "./circle";
export * from "./html";
export * from "./hvline";
export * from "./image";
export * from "./line";
export * from "./rect";
export * from "./string";
Expand Down

0 comments on commit 3130fe4

Please sign in to comment.