brilliantsole 0.0.34 → 0.0.36
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/brilliantsole.cjs +213 -0
- package/build/brilliantsole.cjs.map +1 -1
- package/build/brilliantsole.js.map +1 -1
- package/build/brilliantsole.ls.js +244 -0
- package/build/brilliantsole.ls.js.map +1 -1
- package/build/brilliantsole.min.js.map +1 -1
- package/build/brilliantsole.module.d.ts +22 -22
- package/build/brilliantsole.module.js.map +1 -1
- package/build/brilliantsole.module.min.d.ts +22 -22
- package/build/brilliantsole.module.min.js.map +1 -1
- package/build/brilliantsole.node.module.d.ts +53 -23
- package/build/brilliantsole.node.module.js +205 -1
- package/build/brilliantsole.node.module.js.map +1 -1
- package/build/dts/BS.d.ts +0 -2
- package/build/index.node.d.ts +31 -1
- package/examples/display-workout/script.js +0 -6
- package/package.json +1 -1
- package/src/BS.ts +0 -2
- package/examples/display-workout/uMyoBLE.js +0 -133
package/build/brilliantsole.cjs
CHANGED
|
@@ -5609,6 +5609,9 @@ function removeSubstrings(string, substrings) {
|
|
|
5609
5609
|
|
|
5610
5610
|
const _console$r = createConsole("DisplaySpriteSheetUtils", { log: false });
|
|
5611
5611
|
const spriteHeaderLength = 3 * 2;
|
|
5612
|
+
function calculateSpriteSheetHeaderLength(numberOfSprites) {
|
|
5613
|
+
return 2 + numberOfSprites * 2 + numberOfSprites * spriteHeaderLength;
|
|
5614
|
+
}
|
|
5612
5615
|
function getCurvesPoints(curves) {
|
|
5613
5616
|
const curvePoints = [];
|
|
5614
5617
|
curves.forEach((curve, index) => {
|
|
@@ -6388,6 +6391,15 @@ function resizeImage(image, width, height, canvas) {
|
|
|
6388
6391
|
ctx.drawImage(image, 0, 0, width, height);
|
|
6389
6392
|
return canvas;
|
|
6390
6393
|
}
|
|
6394
|
+
function cropCanvas(canvas, x, y, width, height, targetCanvas) {
|
|
6395
|
+
targetCanvas = targetCanvas || document.createElement("canvas");
|
|
6396
|
+
const ctx = targetCanvas.getContext("2d", { willReadFrequently: true });
|
|
6397
|
+
targetCanvas.width = width;
|
|
6398
|
+
targetCanvas.height = height;
|
|
6399
|
+
ctx.imageSmoothingEnabled = false;
|
|
6400
|
+
ctx.drawImage(canvas, x, y, width, height, 0, 0, width, height);
|
|
6401
|
+
return targetCanvas;
|
|
6402
|
+
}
|
|
6391
6403
|
function removeAlphaFromCanvas(canvas) {
|
|
6392
6404
|
const ctx = canvas.getContext("2d", { willReadFrequently: true });
|
|
6393
6405
|
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
|
|
@@ -6426,6 +6438,77 @@ async function imageToBitmap(image, width, height, colors, bitmapColorIndices, n
|
|
|
6426
6438
|
};
|
|
6427
6439
|
return { blob, bitmap };
|
|
6428
6440
|
}
|
|
6441
|
+
const drawSpriteBitmapCommandHeaderLength = 1 + 2 + 2 + 2 + 2 + 1 + 2;
|
|
6442
|
+
async function canvasToBitmaps(canvas, numberOfColors, mtu) {
|
|
6443
|
+
const { blob, colors, colorIndices } = await quantizeCanvas(canvas, numberOfColors);
|
|
6444
|
+
const bitmapRows = [];
|
|
6445
|
+
const { width, height } = canvas;
|
|
6446
|
+
const numberOfPixels = width * height;
|
|
6447
|
+
const pixelDepth = DisplayPixelDepths.find((pixelDepth) => pixelDepthToNumberOfColors(pixelDepth) >= numberOfColors);
|
|
6448
|
+
_console$q.assertWithError(pixelDepth, `no pixelDepth found that covers ${numberOfColors} colors`);
|
|
6449
|
+
const pixelsPerByte = pixelDepthToPixelsPerByte(pixelDepth);
|
|
6450
|
+
const numberOfBytes = Math.ceil(numberOfPixels / pixelsPerByte);
|
|
6451
|
+
_console$q.log({
|
|
6452
|
+
width,
|
|
6453
|
+
height,
|
|
6454
|
+
numberOfPixels,
|
|
6455
|
+
pixelDepth,
|
|
6456
|
+
pixelsPerByte,
|
|
6457
|
+
numberOfBytes,
|
|
6458
|
+
mtu,
|
|
6459
|
+
});
|
|
6460
|
+
const maxPixelDataLength = mtu - (drawSpriteBitmapCommandHeaderLength + 5);
|
|
6461
|
+
const maxPixels = Math.floor(maxPixelDataLength / pixelsPerByte);
|
|
6462
|
+
const maxBitmapWidth = Math.min(maxPixels, width);
|
|
6463
|
+
let maxBitmapHeight = 1;
|
|
6464
|
+
if (maxBitmapWidth == width) {
|
|
6465
|
+
const bitmapRowPixelDataLength = Math.ceil(width / pixelsPerByte);
|
|
6466
|
+
maxBitmapHeight = Math.floor(maxPixelDataLength / bitmapRowPixelDataLength);
|
|
6467
|
+
}
|
|
6468
|
+
_console$q.log({
|
|
6469
|
+
maxPixelDataLength,
|
|
6470
|
+
maxPixels,
|
|
6471
|
+
maxBitmapHeight,
|
|
6472
|
+
maxBitmapWidth,
|
|
6473
|
+
});
|
|
6474
|
+
if (maxBitmapHeight >= height) {
|
|
6475
|
+
_console$q.log("image is small enough for a single bitmap");
|
|
6476
|
+
const bitmap = {
|
|
6477
|
+
numberOfColors,
|
|
6478
|
+
pixels: colorIndices,
|
|
6479
|
+
width,
|
|
6480
|
+
height,
|
|
6481
|
+
};
|
|
6482
|
+
bitmapRows.push([bitmap]);
|
|
6483
|
+
}
|
|
6484
|
+
else {
|
|
6485
|
+
let offsetX = 0;
|
|
6486
|
+
let offsetY = 0;
|
|
6487
|
+
const bitmapCanvas = document.createElement("canvas");
|
|
6488
|
+
const bitmapColorIndices = new Array(numberOfColors)
|
|
6489
|
+
.fill(0)
|
|
6490
|
+
.map((_, i) => i);
|
|
6491
|
+
while (offsetY < height) {
|
|
6492
|
+
const bitmapHeight = Math.min(maxBitmapHeight, height - offsetY);
|
|
6493
|
+
offsetX = 0;
|
|
6494
|
+
const bitmapRow = [];
|
|
6495
|
+
bitmapRows.push(bitmapRow);
|
|
6496
|
+
while (offsetX < width) {
|
|
6497
|
+
const bitmapWidth = Math.min(maxBitmapWidth, width - offsetX);
|
|
6498
|
+
cropCanvas(canvas, offsetX, offsetY, bitmapWidth, bitmapHeight, bitmapCanvas);
|
|
6499
|
+
const { bitmap } = await imageToBitmap(bitmapCanvas, bitmapWidth, bitmapHeight, colors, bitmapColorIndices, numberOfColors);
|
|
6500
|
+
bitmapRow.push(bitmap);
|
|
6501
|
+
offsetX += bitmapWidth;
|
|
6502
|
+
}
|
|
6503
|
+
offsetY += bitmapHeight;
|
|
6504
|
+
}
|
|
6505
|
+
}
|
|
6506
|
+
return { bitmapRows, colors };
|
|
6507
|
+
}
|
|
6508
|
+
async function imageToBitmaps(image, width, height, numberOfColors, mtu) {
|
|
6509
|
+
const canvas = resizeImage(image, width, height);
|
|
6510
|
+
return canvasToBitmaps(canvas, numberOfColors, mtu);
|
|
6511
|
+
}
|
|
6429
6512
|
function getBitmapNumberOfBytes(bitmap) {
|
|
6430
6513
|
const pixelDepth = numberOfColorsToPixelDepth(bitmap.numberOfColors);
|
|
6431
6514
|
const pixelsPerByte = pixelDepthToPixelsPerByte(pixelDepth);
|
|
@@ -6445,6 +6528,127 @@ function assertValidBitmapPixels(bitmap) {
|
|
|
6445
6528
|
_console$q.assertRangeWithError(`bitmap.pixels[${index}]`, pixel, 0, bitmap.numberOfColors - 1);
|
|
6446
6529
|
});
|
|
6447
6530
|
}
|
|
6531
|
+
async function canvasToSprite(canvas, spriteName, numberOfColors, paletteName, overridePalette, spriteSheet, paletteOffset = 0) {
|
|
6532
|
+
const { width, height } = canvas;
|
|
6533
|
+
let palette = spriteSheet.palettes?.find((palette) => palette.name == paletteName);
|
|
6534
|
+
if (!palette) {
|
|
6535
|
+
palette = {
|
|
6536
|
+
name: paletteName,
|
|
6537
|
+
numberOfColors,
|
|
6538
|
+
colors: new Array(numberOfColors).fill("#000000"),
|
|
6539
|
+
};
|
|
6540
|
+
spriteSheet.palettes = spriteSheet.palettes || [];
|
|
6541
|
+
spriteSheet.palettes?.push(palette);
|
|
6542
|
+
}
|
|
6543
|
+
_console$q.log("pallete", palette);
|
|
6544
|
+
const sprite = {
|
|
6545
|
+
name: spriteName,
|
|
6546
|
+
width,
|
|
6547
|
+
height,
|
|
6548
|
+
paletteSwaps: [],
|
|
6549
|
+
commands: [],
|
|
6550
|
+
};
|
|
6551
|
+
const results = await quantizeCanvas(canvas, numberOfColors, !overridePalette ? palette.colors : undefined);
|
|
6552
|
+
const blob = results.blob;
|
|
6553
|
+
const colorIndices = results.colorIndices;
|
|
6554
|
+
if (overridePalette) {
|
|
6555
|
+
results.colors.forEach((color, index) => {
|
|
6556
|
+
palette.colors[index + paletteOffset] = color;
|
|
6557
|
+
});
|
|
6558
|
+
}
|
|
6559
|
+
sprite.commands.push({
|
|
6560
|
+
type: "selectBitmapColors",
|
|
6561
|
+
bitmapColorPairs: new Array(numberOfColors).fill(0).map((_, index) => ({
|
|
6562
|
+
bitmapColorIndex: index,
|
|
6563
|
+
colorIndex: index + paletteOffset,
|
|
6564
|
+
})),
|
|
6565
|
+
});
|
|
6566
|
+
const bitmap = {
|
|
6567
|
+
numberOfColors,
|
|
6568
|
+
pixels: colorIndices,
|
|
6569
|
+
width,
|
|
6570
|
+
height,
|
|
6571
|
+
};
|
|
6572
|
+
sprite.commands.push({ type: "drawBitmap", offsetX: 0, offsetY: 0, bitmap });
|
|
6573
|
+
const spriteIndex = spriteSheet.sprites.findIndex((sprite) => sprite.name == spriteName);
|
|
6574
|
+
if (spriteIndex == -1) {
|
|
6575
|
+
spriteSheet.sprites.push(sprite);
|
|
6576
|
+
}
|
|
6577
|
+
else {
|
|
6578
|
+
_console$q.log(`overwriting spriteIndex ${spriteIndex}`);
|
|
6579
|
+
spriteSheet.sprites[spriteIndex] = sprite;
|
|
6580
|
+
}
|
|
6581
|
+
return { sprite, blob };
|
|
6582
|
+
}
|
|
6583
|
+
async function imageToSprite(image, spriteName, width, height, numberOfColors, paletteName, overridePalette, spriteSheet, paletteOffset = 0) {
|
|
6584
|
+
const canvas = resizeImage(image, width, height);
|
|
6585
|
+
return canvasToSprite(canvas, spriteName, numberOfColors, paletteName, overridePalette, spriteSheet, paletteOffset);
|
|
6586
|
+
}
|
|
6587
|
+
const spriteSheetWithSingleBitmapCommandLength = calculateSpriteSheetHeaderLength(1) + drawSpriteBitmapCommandHeaderLength;
|
|
6588
|
+
function spriteSheetWithBitmapCommandAndSelectBitmapColorsLength(numberOfColors) {
|
|
6589
|
+
return (spriteSheetWithSingleBitmapCommandLength + (1 + 1 + numberOfColors * 2));
|
|
6590
|
+
}
|
|
6591
|
+
async function canvasToSpriteSheet(canvas, spriteSheetName, numberOfColors, paletteName, maxFileLength) {
|
|
6592
|
+
const spriteSheet = {
|
|
6593
|
+
name: spriteSheetName,
|
|
6594
|
+
palettes: [],
|
|
6595
|
+
paletteSwaps: [],
|
|
6596
|
+
sprites: [],
|
|
6597
|
+
};
|
|
6598
|
+
if (maxFileLength == undefined) {
|
|
6599
|
+
await canvasToSprite(canvas, "image", numberOfColors, paletteName, true, spriteSheet);
|
|
6600
|
+
}
|
|
6601
|
+
else {
|
|
6602
|
+
const { width, height } = canvas;
|
|
6603
|
+
const numberOfPixels = width * height;
|
|
6604
|
+
const pixelDepth = DisplayPixelDepths.find((pixelDepth) => pixelDepthToNumberOfColors(pixelDepth) >= numberOfColors);
|
|
6605
|
+
_console$q.assertWithError(pixelDepth, `no pixelDepth found that covers ${numberOfColors} colors`);
|
|
6606
|
+
const pixelsPerByte = pixelDepthToPixelsPerByte(pixelDepth);
|
|
6607
|
+
const numberOfBytes = Math.ceil(numberOfPixels / pixelsPerByte);
|
|
6608
|
+
_console$q.log({
|
|
6609
|
+
width,
|
|
6610
|
+
height,
|
|
6611
|
+
numberOfPixels,
|
|
6612
|
+
pixelDepth,
|
|
6613
|
+
pixelsPerByte,
|
|
6614
|
+
numberOfBytes,
|
|
6615
|
+
maxFileLength,
|
|
6616
|
+
});
|
|
6617
|
+
const maxPixelDataLength = maxFileLength -
|
|
6618
|
+
(spriteSheetWithBitmapCommandAndSelectBitmapColorsLength(numberOfColors) +
|
|
6619
|
+
5);
|
|
6620
|
+
const imageRowPixelDataLength = Math.ceil(width / pixelsPerByte);
|
|
6621
|
+
const maxSpriteHeight = Math.floor(maxPixelDataLength / imageRowPixelDataLength);
|
|
6622
|
+
if (maxSpriteHeight >= height) {
|
|
6623
|
+
_console$q.log("image is small enough for a single sprite");
|
|
6624
|
+
await canvasToSprite(canvas, "image", numberOfColors, paletteName, true, spriteSheet);
|
|
6625
|
+
}
|
|
6626
|
+
else {
|
|
6627
|
+
const { colors } = await quantizeCanvas(canvas, numberOfColors);
|
|
6628
|
+
spriteSheet.palettes?.push({ name: paletteName, numberOfColors, colors });
|
|
6629
|
+
let offsetY = 0;
|
|
6630
|
+
let imageIndex = 0;
|
|
6631
|
+
const spriteCanvas = document.createElement("canvas");
|
|
6632
|
+
while (offsetY < height) {
|
|
6633
|
+
const spriteHeight = Math.min(maxSpriteHeight, height - offsetY);
|
|
6634
|
+
cropCanvas(canvas, 0, offsetY, width, spriteHeight, spriteCanvas);
|
|
6635
|
+
offsetY += spriteHeight;
|
|
6636
|
+
_console$q.log(`cropping sprite ${imageIndex}`, {
|
|
6637
|
+
offsetY,
|
|
6638
|
+
width,
|
|
6639
|
+
spriteHeight,
|
|
6640
|
+
});
|
|
6641
|
+
await canvasToSprite(spriteCanvas, `image${imageIndex}`, numberOfColors, paletteName, false, spriteSheet);
|
|
6642
|
+
imageIndex++;
|
|
6643
|
+
}
|
|
6644
|
+
}
|
|
6645
|
+
}
|
|
6646
|
+
return spriteSheet;
|
|
6647
|
+
}
|
|
6648
|
+
async function imageToSpriteSheet(image, spriteSheetName, width, height, numberOfColors, paletteName, maxFileLength) {
|
|
6649
|
+
const canvas = resizeImage(image, width, height);
|
|
6650
|
+
return canvasToSpriteSheet(canvas, spriteSheetName, numberOfColors, paletteName, maxFileLength);
|
|
6651
|
+
}
|
|
6448
6652
|
|
|
6449
6653
|
const _console$p = createConsole("DisplayManagerInterface", { log: false });
|
|
6450
6654
|
async function runDisplayContextCommand(displayManager, command, sendImmediately) {
|
|
@@ -14464,6 +14668,9 @@ exports.VibrationLocations = VibrationLocations;
|
|
|
14464
14668
|
exports.VibrationTypes = VibrationTypes;
|
|
14465
14669
|
exports.VibrationWaveformEffects = VibrationWaveformEffects;
|
|
14466
14670
|
exports.WebSocketServer = WebSocketServer;
|
|
14671
|
+
exports.canvasToBitmaps = canvasToBitmaps;
|
|
14672
|
+
exports.canvasToSprite = canvasToSprite;
|
|
14673
|
+
exports.canvasToSpriteSheet = canvasToSpriteSheet;
|
|
14467
14674
|
exports.displayCurveTypeToNumberOfControlPoints = displayCurveTypeToNumberOfControlPoints;
|
|
14468
14675
|
exports.englishRegex = englishRegex;
|
|
14469
14676
|
exports.fontToSpriteSheet = fontToSpriteSheet;
|
|
@@ -14472,12 +14679,18 @@ exports.getFontMetrics = getFontMetrics;
|
|
|
14472
14679
|
exports.getFontUnicodeRange = getFontUnicodeRange;
|
|
14473
14680
|
exports.getMaxSpriteSheetSize = getMaxSpriteSheetSize;
|
|
14474
14681
|
exports.hexToRGB = hexToRGB;
|
|
14682
|
+
exports.imageToBitmaps = imageToBitmaps;
|
|
14683
|
+
exports.imageToSprite = imageToSprite;
|
|
14684
|
+
exports.imageToSpriteSheet = imageToSpriteSheet;
|
|
14475
14685
|
exports.intersectWireframes = intersectWireframes;
|
|
14476
14686
|
exports.isWireframePolygon = isWireframePolygon;
|
|
14477
14687
|
exports.maxDisplayScale = maxDisplayScale;
|
|
14478
14688
|
exports.mergeWireframes = mergeWireframes;
|
|
14479
14689
|
exports.parseFont = parseFont;
|
|
14480
14690
|
exports.pixelDepthToNumberOfColors = pixelDepthToNumberOfColors;
|
|
14691
|
+
exports.quantizeImage = quantizeImage;
|
|
14692
|
+
exports.resizeAndQuantizeImage = resizeAndQuantizeImage;
|
|
14693
|
+
exports.resizeImage = resizeImage;
|
|
14481
14694
|
exports.rgbToHex = rgbToHex;
|
|
14482
14695
|
exports.setAllConsoleLevelFlags = setAllConsoleLevelFlags;
|
|
14483
14696
|
exports.setConsoleLevelFlagsForType = setConsoleLevelFlagsForType;
|