Skip to content

Commit

Permalink
feat(text-canvas): add imageCanvas/String565() fns
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Mar 24, 2021
1 parent 3bf5b47 commit 6e254eb
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion packages/text-canvas/src/image.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import type { UIntArray } from "@thi.ng/api";
import { peek } from "@thi.ng/arrays";
import { isNumber } from "@thi.ng/checks";
import { clamp0 } from "@thi.ng/math";
import { ClipRect, ImageOpts, SHADES_BLOCK } from "./api";
import { Canvas } from "./canvas";
import { canvas, Canvas } from "./canvas";
import { FMT_ANSI565 } from "./format";
import { toString } from "./string";
import { charCode, intersectRect } from "./utils";

export const blit = (canvas: Canvas, x: number, y: number, src: Canvas) => {
Expand Down Expand Up @@ -177,6 +180,35 @@ export const imageRaw = (
}
};

/**
* Syntax sugar for {@link imageRaw}. Takes a thi.ng/pixel compatible 16bit
* pixel buffer in RGB565 format and converts it into a new {@link canvas}. The
* optional `char` will be used as character for each pixel.
*
* @param src
* @param char
*/
export const imageCanvas565 = (
src: { width: number; height: number; pixels: UIntArray },
char?: string
) => {
const dest = canvas(src.width, src.height);
imageRaw(dest, 0, 0, src.width, src.height, src.pixels, char);
return dest;
};

/**
* Same as {@link imageCanvas565}, but returns resulting canvas as 24bit ANSI
* string.
*
* @param src
* @param char
*/
export const imageString565 = (
src: { width: number; height: number; pixels: UIntArray },
char?: string
) => toString(imageCanvas565(src, char), FMT_ANSI565);

const imgRect = (
canvas: Canvas,
x: number,
Expand Down

0 comments on commit 6e254eb

Please sign in to comment.