pixel-data-js 0.5.2 → 0.5.3

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.dev.js CHANGED
@@ -1122,6 +1122,48 @@ function makeIndexedImage(imageData) {
1122
1122
  };
1123
1123
  }
1124
1124
 
1125
+ // src/IndexedImage/indexedImageToAverageColor.ts
1126
+ function indexedImageToAverageColor(indexedImage, includeTransparent = false) {
1127
+ const { data, palette, transparentPalletIndex } = indexedImage;
1128
+ const counts = new Uint32Array(palette.length / 4);
1129
+ for (let i = 0; i < data.length; i++) {
1130
+ const id = data[i];
1131
+ counts[id]++;
1132
+ }
1133
+ let rSum = 0;
1134
+ let gSum = 0;
1135
+ let bSum = 0;
1136
+ let aSum = 0;
1137
+ let totalWeight = 0;
1138
+ for (let id = 0; id < counts.length; id++) {
1139
+ const weight = counts[id];
1140
+ if (weight === 0) {
1141
+ continue;
1142
+ }
1143
+ if (!includeTransparent && id === transparentPalletIndex) {
1144
+ continue;
1145
+ }
1146
+ const pIdx = id * 4;
1147
+ const r2 = palette[pIdx];
1148
+ const g2 = palette[pIdx + 1];
1149
+ const b2 = palette[pIdx + 2];
1150
+ const a2 = palette[pIdx + 3];
1151
+ rSum += r2 * weight;
1152
+ gSum += g2 * weight;
1153
+ bSum += b2 * weight;
1154
+ aSum += a2 * weight;
1155
+ totalWeight += weight;
1156
+ }
1157
+ if (totalWeight === 0) {
1158
+ return packColor(0, 0, 0, 0);
1159
+ }
1160
+ const r = rSum / totalWeight | 0;
1161
+ const g = gSum / totalWeight | 0;
1162
+ const b = bSum / totalWeight | 0;
1163
+ const a = aSum / totalWeight | 0;
1164
+ return packColor(r, g, b, a);
1165
+ }
1166
+
1125
1167
  // src/Input/fileInputChangeToImageData.ts
1126
1168
  async function fileInputChangeToImageData(event) {
1127
1169
  const target = event.target;
@@ -1291,7 +1333,7 @@ function mergeMasks(dst, dstWidth, src, opts) {
1291
1333
  }
1292
1334
  }
1293
1335
 
1294
- // src/PixelData.ts
1336
+ // src/PixelData/PixelData.ts
1295
1337
  var PixelData = class {
1296
1338
  constructor(imageData) {
1297
1339
  this.imageData = imageData;
@@ -1744,6 +1786,7 @@ export {
1744
1786
  imageDataToDataUrl,
1745
1787
  imageDataToImgBlob,
1746
1788
  imgBlobToImageData,
1789
+ indexedImageToAverageColor,
1747
1790
  invertAlphaMask,
1748
1791
  invertBinaryMask,
1749
1792
  invertImageData,