asciify-engine 1.0.60 → 1.0.61

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.js CHANGED
@@ -732,11 +732,34 @@ function imageToAsciiFrame(source, options, targetWidth, targetHeight) {
732
732
  ctx.drawImage(source, 0, 0, cols, rows);
733
733
  const imageData = ctx.getImageData(0, 0, cols, rows);
734
734
  const pixels = imageData.data;
735
+ const ck = options.chromaKey;
736
+ const ckEnabled = ck != null && ck !== false;
737
+ const ckHeuristicGreen = ck === true;
738
+ const ckHeuristicBlue = ck === "blue-screen";
739
+ let ckRGB = null;
740
+ let ckTolSq = 0;
741
+ if (ckEnabled && !ckHeuristicGreen && !ckHeuristicBlue) {
742
+ ckRGB = parseChromaKeyColor(ck);
743
+ ckTolSq = (options.chromaKeyTolerance ?? 60) ** 2;
744
+ }
735
745
  let normMin = 0;
736
746
  let normRange = 255;
737
747
  if (options.normalize) {
738
748
  let lo = 255, hi = 0;
739
749
  for (let k = 0; k < pixels.length; k += 4) {
750
+ if (ckEnabled) {
751
+ const pr = pixels[k], pg = pixels[k + 1], pb = pixels[k + 2];
752
+ let keyed = false;
753
+ if (ckHeuristicGreen) {
754
+ keyed = pg > pr * 1.4 && pg > pb * 1.4 && pg > 80;
755
+ } else if (ckHeuristicBlue) {
756
+ keyed = pb > pr * 1.4 && pb > pg * 1.4 && pb > 80;
757
+ } else if (ckRGB !== null) {
758
+ const dr = pr - ckRGB.r, dg = pg - ckRGB.g, db = pb - ckRGB.b;
759
+ keyed = dr * dr + dg * dg + db * db <= ckTolSq;
760
+ }
761
+ if (keyed) continue;
762
+ }
740
763
  const l = 0.299 * pixels[k] + 0.587 * pixels[k + 1] + 0.114 * pixels[k + 2];
741
764
  if (l < lo) lo = l;
742
765
  if (l > hi) hi = l;
@@ -746,16 +769,6 @@ function imageToAsciiFrame(source, options, targetWidth, targetHeight) {
746
769
  }
747
770
  const frame = [];
748
771
  const invertVal = resolveInvert(options.invert);
749
- const ck = options.chromaKey;
750
- const ckEnabled = ck != null && ck !== false;
751
- const ckHeuristicGreen = ck === true;
752
- const ckHeuristicBlue = ck === "blue-screen";
753
- let ckRGB = null;
754
- let ckTolSq = 0;
755
- if (ckEnabled && !ckHeuristicGreen && !ckHeuristicBlue) {
756
- ckRGB = parseChromaKeyColor(ck);
757
- ckTolSq = (options.chromaKeyTolerance ?? 60) ** 2;
758
- }
759
772
  for (let y = 0; y < rows; y++) {
760
773
  const row = [];
761
774
  for (let x = 0; x < cols; x++) {