-
-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(text-canvas): add bar chart & image fns
- Loading branch information
1 parent
8909ce0
commit 3130fe4
Showing
3 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters