asciify-engine 1.0.60 → 1.0.63

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/dist/index.cjs CHANGED
@@ -284,9 +284,7 @@ function getCellColorStr(cell, colorMode, acR, acG, acB, _isInverted = false) {
284
284
  case "matrix":
285
285
  return GREEN_LUT[0.299 * cell.r + 0.587 * cell.g + 0.114 * cell.b | 0];
286
286
  case "accent": {
287
- const rawAb = (0.299 * cell.r + 0.587 * cell.g + 0.114 * cell.b) / 255;
288
- const ab = _isInverted ? 1 - rawAb : rawAb;
289
- return `rgb(${acR * ab | 0},${acG * ab | 0},${acB * ab | 0})`;
287
+ return `rgb(${acR},${acG},${acB})`;
290
288
  }
291
289
  default: {
292
290
  const gray = 0.299 * cell.r + 0.587 * cell.g + 0.114 * cell.b | 0;
@@ -310,11 +308,9 @@ function getCellColorRGB(cell, colorMode, acR, acG, acB, _isInverted = false) {
310
308
  break;
311
309
  }
312
310
  case "accent": {
313
- const rawAb = (0.299 * cell.r + 0.587 * cell.g + 0.114 * cell.b) / 255;
314
- const ab = _isInverted ? 1 - rawAb : rawAb;
315
- _colorRGB[0] = acR * ab | 0;
316
- _colorRGB[1] = acG * ab | 0;
317
- _colorRGB[2] = acB * ab | 0;
311
+ _colorRGB[0] = acR;
312
+ _colorRGB[1] = acG;
313
+ _colorRGB[2] = acB;
318
314
  break;
319
315
  }
320
316
  default: {
@@ -734,11 +730,34 @@ function imageToAsciiFrame(source, options, targetWidth, targetHeight) {
734
730
  ctx.drawImage(source, 0, 0, cols, rows);
735
731
  const imageData = ctx.getImageData(0, 0, cols, rows);
736
732
  const pixels = imageData.data;
733
+ const ck = options.chromaKey;
734
+ const ckEnabled = ck != null && ck !== false;
735
+ const ckHeuristicGreen = ck === true;
736
+ const ckHeuristicBlue = ck === "blue-screen";
737
+ let ckRGB = null;
738
+ let ckTolSq = 0;
739
+ if (ckEnabled && !ckHeuristicGreen && !ckHeuristicBlue) {
740
+ ckRGB = parseChromaKeyColor(ck);
741
+ ckTolSq = (options.chromaKeyTolerance ?? 60) ** 2;
742
+ }
737
743
  let normMin = 0;
738
744
  let normRange = 255;
739
745
  if (options.normalize) {
740
746
  let lo = 255, hi = 0;
741
747
  for (let k = 0; k < pixels.length; k += 4) {
748
+ if (ckEnabled) {
749
+ const pr = pixels[k], pg = pixels[k + 1], pb = pixels[k + 2];
750
+ let keyed = false;
751
+ if (ckHeuristicGreen) {
752
+ keyed = pg > pr * 1.4 && pg > pb * 1.4 && pg > 80;
753
+ } else if (ckHeuristicBlue) {
754
+ keyed = pb > pr * 1.4 && pb > pg * 1.4 && pb > 80;
755
+ } else if (ckRGB !== null) {
756
+ const dr = pr - ckRGB.r, dg = pg - ckRGB.g, db = pb - ckRGB.b;
757
+ keyed = dr * dr + dg * dg + db * db <= ckTolSq;
758
+ }
759
+ if (keyed) continue;
760
+ }
742
761
  const l = 0.299 * pixels[k] + 0.587 * pixels[k + 1] + 0.114 * pixels[k + 2];
743
762
  if (l < lo) lo = l;
744
763
  if (l > hi) hi = l;
@@ -748,16 +767,7 @@ function imageToAsciiFrame(source, options, targetWidth, targetHeight) {
748
767
  }
749
768
  const frame = [];
750
769
  const invertVal = resolveInvert(options.invert);
751
- const ck = options.chromaKey;
752
- const ckEnabled = ck != null && ck !== false;
753
- const ckHeuristicGreen = ck === true;
754
- const ckHeuristicBlue = ck === "blue-screen";
755
- let ckRGB = null;
756
- let ckTolSq = 0;
757
- if (ckEnabled && !ckHeuristicGreen && !ckHeuristicBlue) {
758
- ckRGB = parseChromaKeyColor(ck);
759
- ckTolSq = (options.chromaKeyTolerance ?? 60) ** 2;
760
- }
770
+ const effectiveCharset = ckEnabled ? options.charset.replace(/ /g, "") || options.charset : options.charset;
761
771
  for (let y = 0; y < rows; y++) {
762
772
  const row = [];
763
773
  for (let x = 0; x < cols; x++) {
@@ -787,7 +797,7 @@ function imageToAsciiFrame(source, options, targetWidth, targetHeight) {
787
797
  const lum = options.normalize ? (rawLum - normMin) / normRange * 255 : rawLum;
788
798
  const adjustedLum = adjustLuminance(lum, options.brightness, options.contrast);
789
799
  const ditheredLum = applyDither(adjustedLum, x, y, options.ditherStrength);
790
- const char = options.customText ? customTextToChar(ditheredLum, options.customText, x, y, cols, invertVal) : luminanceToChar(ditheredLum, options.charset, invertVal);
800
+ const char = options.customText ? customTextToChar(ditheredLum, options.customText, x, y, cols, invertVal) : luminanceToChar(ditheredLum, effectiveCharset, invertVal);
791
801
  row.push({ char, r, g, b, a, lum: ditheredLum });
792
802
  }
793
803
  frame.push(row);