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.
@@ -63,6 +63,7 @@ __export(src_exports, {
63
63
  imageDataToDataUrl: () => imageDataToDataUrl,
64
64
  imageDataToImgBlob: () => imageDataToImgBlob,
65
65
  imgBlobToImageData: () => imgBlobToImageData,
66
+ indexedImageToAverageColor: () => indexedImageToAverageColor,
66
67
  invertAlphaMask: () => invertAlphaMask,
67
68
  invertBinaryMask: () => invertBinaryMask,
68
69
  invertImageData: () => invertImageData,
@@ -1230,6 +1231,48 @@ function makeIndexedImage(imageData) {
1230
1231
  };
1231
1232
  }
1232
1233
 
1234
+ // src/IndexedImage/indexedImageToAverageColor.ts
1235
+ function indexedImageToAverageColor(indexedImage, includeTransparent = false) {
1236
+ const { data, palette, transparentPalletIndex } = indexedImage;
1237
+ const counts = new Uint32Array(palette.length / 4);
1238
+ for (let i = 0; i < data.length; i++) {
1239
+ const id = data[i];
1240
+ counts[id]++;
1241
+ }
1242
+ let rSum = 0;
1243
+ let gSum = 0;
1244
+ let bSum = 0;
1245
+ let aSum = 0;
1246
+ let totalWeight = 0;
1247
+ for (let id = 0; id < counts.length; id++) {
1248
+ const weight = counts[id];
1249
+ if (weight === 0) {
1250
+ continue;
1251
+ }
1252
+ if (!includeTransparent && id === transparentPalletIndex) {
1253
+ continue;
1254
+ }
1255
+ const pIdx = id * 4;
1256
+ const r2 = palette[pIdx];
1257
+ const g2 = palette[pIdx + 1];
1258
+ const b2 = palette[pIdx + 2];
1259
+ const a2 = palette[pIdx + 3];
1260
+ rSum += r2 * weight;
1261
+ gSum += g2 * weight;
1262
+ bSum += b2 * weight;
1263
+ aSum += a2 * weight;
1264
+ totalWeight += weight;
1265
+ }
1266
+ if (totalWeight === 0) {
1267
+ return packColor(0, 0, 0, 0);
1268
+ }
1269
+ const r = rSum / totalWeight | 0;
1270
+ const g = gSum / totalWeight | 0;
1271
+ const b = bSum / totalWeight | 0;
1272
+ const a = aSum / totalWeight | 0;
1273
+ return packColor(r, g, b, a);
1274
+ }
1275
+
1233
1276
  // src/Input/fileInputChangeToImageData.ts
1234
1277
  async function fileInputChangeToImageData(event) {
1235
1278
  const target = event.target;
@@ -1399,7 +1442,7 @@ function mergeMasks(dst, dstWidth, src, opts) {
1399
1442
  }
1400
1443
  }
1401
1444
 
1402
- // src/PixelData.ts
1445
+ // src/PixelData/PixelData.ts
1403
1446
  var PixelData = class {
1404
1447
  constructor(imageData) {
1405
1448
  this.imageData = imageData;
@@ -1853,6 +1896,7 @@ function invertPixelData(pixelData) {
1853
1896
  imageDataToDataUrl,
1854
1897
  imageDataToImgBlob,
1855
1898
  imgBlobToImageData,
1899
+ indexedImageToAverageColor,
1856
1900
  invertAlphaMask,
1857
1901
  invertBinaryMask,
1858
1902
  invertImageData,