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.
@@ -613,6 +613,15 @@ type IndexedImage = {
613
613
  */
614
614
  declare function makeIndexedImage(imageData: ImageData): IndexedImage;
615
615
 
616
+ /**
617
+ * Calculates the area-weighted average color of an IndexedImage.
618
+ * This accounts for how often each palette index appears in the pixel data.
619
+ * @param indexedImage - The IndexedImage containing pixel indices and the palette.
620
+ * @param includeTransparent - Whether to include the transparent pixels in the average.
621
+ * @returns The average RGBA color of the image.
622
+ */
623
+ declare function indexedImageToAverageColor(indexedImage: IndexedImage, includeTransparent?: boolean): Color32;
624
+
616
625
  /**
617
626
  * A convenience wrapper that extracts the first {@link File} from an
618
627
  * {@link HTMLInputElement} change event and converts it into {@link ImageData}.
@@ -819,4 +828,4 @@ declare function pixelDataToAlphaMask(pixelData: PixelData): AlphaMask;
819
828
  */
820
829
  declare function trimRectBounds<T extends Rect | SelectionRect>(target: T, bounds: Rect): void;
821
830
 
822
- export { type AlphaMask, type AnyMask, type ApplyMaskOptions, type Base64EncodedUInt8Array, type BinaryMask, type BlendColor32, BlendMode, type BlendModeIndex, COLOR_32_BLEND_MODES, COLOR_32_BLEND_TO_INDEX, type Color32, type ColorBlendOptions, type FloodFillImageDataOptions, type FloodFillResult, INDEX_TO_COLOR_32_BLEND, type ImageDataLike, type IndexedImage, MaskType, type PixelBlendOptions, type PixelCanvas, PixelData, type PixelOptions, type RGBA, type Rect, type RegisteredBlender, type ReusableCanvas, type SelectionRect, type SerializedImageData, UnsupportedFormatError, applyMaskToPixelData, base64DecodeArrayBuffer, base64EncodeArrayBuffer, blendColorPixelData, blendPixelData, clearPixelData, color32ToCssRGBA, color32ToHex, colorBurnColor32, colorDistance, colorDodgeColor32, copyImageData, copyImageDataLike, copyMask, darkenColor32, darkerColor32, deserializeImageData, deserializeNullableImageData, deserializeRawImageData, differenceColor32, divideColor32, exclusionColor32, extractImageDataPixels, extractMask, fileInputChangeToImageData, fileToImageData, fillPixelData, floodFillSelection, getImageDataFromClipboard, getSupportedPixelFormats, hardLightColor32, hardMixColor32, imageDataToAlphaMask, imageDataToDataUrl, imageDataToImgBlob, imgBlobToImageData, invertAlphaMask, invertBinaryMask, invertImageData, invertPixelData, lerpColor32, lerpColor32Fast, lightenColor32, lighterColor32, linearBurnColor32, linearDodgeColor32, linearLightColor32, makeIndexedImage, makePixelCanvas, makeReusableCanvas, mergeMasks, multiplyColor32, overlayColor32, overwriteColor32, packColor, packRGBA, pinLightColor32, pixelDataToAlphaMask, resizeImageData, screenColor32, serializeImageData, serializeNullableImageData, softLightColor32, sourceOverColor32, subtractColor32, trimRectBounds, unpackAlpha, unpackBlue, unpackColor, unpackColorTo, unpackGreen, unpackRed, vividLightColor32, writeImageDataPixels, writeImageDataToClipboard, writeImgBlobToClipboard };
831
+ export { type AlphaMask, type AnyMask, type ApplyMaskOptions, type Base64EncodedUInt8Array, type BinaryMask, type BlendColor32, BlendMode, type BlendModeIndex, COLOR_32_BLEND_MODES, COLOR_32_BLEND_TO_INDEX, type Color32, type ColorBlendOptions, type FloodFillImageDataOptions, type FloodFillResult, INDEX_TO_COLOR_32_BLEND, type ImageDataLike, type IndexedImage, MaskType, type PixelBlendOptions, type PixelCanvas, PixelData, type PixelOptions, type RGBA, type Rect, type RegisteredBlender, type ReusableCanvas, type SelectionRect, type SerializedImageData, UnsupportedFormatError, applyMaskToPixelData, base64DecodeArrayBuffer, base64EncodeArrayBuffer, blendColorPixelData, blendPixelData, clearPixelData, color32ToCssRGBA, color32ToHex, colorBurnColor32, colorDistance, colorDodgeColor32, copyImageData, copyImageDataLike, copyMask, darkenColor32, darkerColor32, deserializeImageData, deserializeNullableImageData, deserializeRawImageData, differenceColor32, divideColor32, exclusionColor32, extractImageDataPixels, extractMask, fileInputChangeToImageData, fileToImageData, fillPixelData, floodFillSelection, getImageDataFromClipboard, getSupportedPixelFormats, hardLightColor32, hardMixColor32, imageDataToAlphaMask, imageDataToDataUrl, imageDataToImgBlob, imgBlobToImageData, indexedImageToAverageColor, invertAlphaMask, invertBinaryMask, invertImageData, invertPixelData, lerpColor32, lerpColor32Fast, lightenColor32, lighterColor32, linearBurnColor32, linearDodgeColor32, linearLightColor32, makeIndexedImage, makePixelCanvas, makeReusableCanvas, mergeMasks, multiplyColor32, overlayColor32, overwriteColor32, packColor, packRGBA, pinLightColor32, pixelDataToAlphaMask, resizeImageData, screenColor32, serializeImageData, serializeNullableImageData, softLightColor32, sourceOverColor32, subtractColor32, trimRectBounds, unpackAlpha, unpackBlue, unpackColor, unpackColorTo, unpackGreen, unpackRed, vividLightColor32, writeImageDataPixels, writeImageDataToClipboard, writeImgBlobToClipboard };
@@ -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,