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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "pixel-data-js",
3
3
  "type": "module",
4
- "version": "0.5.2",
4
+ "version": "0.5.3",
5
5
  "packageManager": "pnpm@10.30.0",
6
6
  "description": "JS Pixel and ImageData operations",
7
7
  "author": {
@@ -1,7 +1,7 @@
1
1
  import { type Color32, type ImageDataLike, MaskType, type Rect, type SelectionRect } from '../_types'
2
2
  import { colorDistance } from '../color'
3
3
  import { extractImageDataPixels } from '../ImageData/extractImageDataPixels'
4
- import type { PixelData } from '../PixelData'
4
+ import type { PixelData } from '../PixelData/PixelData'
5
5
  import { trimRectBounds } from '../Rect/trimRectBounds'
6
6
 
7
7
  export type FloodFillImageDataOptions = {
@@ -0,0 +1,65 @@
1
+ import type { Color32 } from '../_types'
2
+ import { packColor } from '../color'
3
+ import type { IndexedImage } from './IndexedImage'
4
+
5
+ /**
6
+ * Calculates the area-weighted average color of an IndexedImage.
7
+ * This accounts for how often each palette index appears in the pixel data.
8
+ * @param indexedImage - The IndexedImage containing pixel indices and the palette.
9
+ * @param includeTransparent - Whether to include the transparent pixels in the average.
10
+ * @returns The average RGBA color of the image.
11
+ */
12
+ export function indexedImageToAverageColor(
13
+ indexedImage: IndexedImage,
14
+ includeTransparent: boolean = false,
15
+ ): Color32 {
16
+ const { data, palette, transparentPalletIndex } = indexedImage
17
+ const counts = new Uint32Array(palette.length / 4)
18
+
19
+ // Tally occurrences of each index
20
+ for (let i = 0; i < data.length; i++) {
21
+ const id = data[i]!
22
+ counts[id]!++
23
+ }
24
+
25
+ let rSum = 0
26
+ let gSum = 0
27
+ let bSum = 0
28
+ let aSum = 0
29
+ let totalWeight = 0
30
+
31
+ for (let id = 0; id < counts.length; id++) {
32
+ const weight = counts[id]!
33
+
34
+ if (weight === 0) {
35
+ continue
36
+ }
37
+
38
+ if (!includeTransparent && id === transparentPalletIndex) {
39
+ continue
40
+ }
41
+
42
+ const pIdx = id * 4
43
+ const r = palette[pIdx]!
44
+ const g = palette[pIdx + 1]!
45
+ const b = palette[pIdx + 2]!
46
+ const a = palette[pIdx + 3]!
47
+
48
+ rSum += r * weight
49
+ gSum += g * weight
50
+ bSum += b * weight
51
+ aSum += a * weight
52
+ totalWeight += weight
53
+ }
54
+
55
+ if (totalWeight === 0) {
56
+ return packColor(0, 0, 0, 0)
57
+ }
58
+
59
+ const r = (rSum / totalWeight) | 0
60
+ const g = (gSum / totalWeight) | 0
61
+ const b = (bSum / totalWeight) | 0
62
+ const a = (aSum / totalWeight) | 0
63
+
64
+ return packColor(r, g, b, a)
65
+ }
@@ -1,4 +1,4 @@
1
- import type { ImageDataLike } from './_types'
1
+ import type { ImageDataLike } from '../_types'
2
2
 
3
3
  export class PixelData {
4
4
  public readonly data32: Uint32Array
@@ -1,5 +1,5 @@
1
1
  import { type AnyMask, type ApplyMaskOptions, MaskType } from '../_types'
2
- import type { PixelData } from '../PixelData'
2
+ import type { PixelData } from './PixelData'
3
3
 
4
4
  /**
5
5
  * Directly applies a mask to a region of PixelData,
@@ -1,6 +1,6 @@
1
1
  import { type Color32, type ColorBlendOptions, MaskType } from '../_types'
2
2
  import { sourceOverColor32 } from '../blend-modes'
3
- import type { PixelData } from '../PixelData'
3
+ import type { PixelData } from './PixelData'
4
4
 
5
5
  /**
6
6
  * Fills a rectangle in the destination PixelData with a single color,
@@ -1,6 +1,6 @@
1
1
  import { type Color32, MaskType, type PixelBlendOptions } from '../_types'
2
2
  import { sourceOverColor32 } from '../blend-modes'
3
- import type { PixelData } from '../PixelData'
3
+ import type { PixelData } from './PixelData'
4
4
 
5
5
  /**
6
6
  * Blits source PixelData into a destination PixelData using 32-bit integer bitwise blending.
@@ -1,5 +1,5 @@
1
1
  import type { Color32, Rect } from '../_types'
2
- import type { PixelData } from '../PixelData'
2
+ import type { PixelData } from './PixelData'
3
3
  import { fillPixelData } from './fillPixelData'
4
4
 
5
5
  /**
@@ -1,5 +1,5 @@
1
1
  import type { Color32, Rect } from '../_types'
2
- import type { PixelData } from '../PixelData'
2
+ import type { PixelData } from './PixelData'
3
3
 
4
4
  /**
5
5
  * Fills a region or the {@link PixelData} buffer with a solid color.
@@ -1,4 +1,4 @@
1
- import type { PixelData } from '../PixelData'
1
+ import type { PixelData } from './PixelData'
2
2
 
3
3
  export function invertPixelData(
4
4
  pixelData: PixelData,
@@ -1,5 +1,5 @@
1
1
  import type { AlphaMask } from '../_types'
2
- import type { PixelData } from '../PixelData'
2
+ import type { PixelData } from './PixelData'
3
3
 
4
4
  /**
5
5
  * Extracts the alpha channel from PixelData into a single-channel mask.
package/src/index.ts CHANGED
@@ -23,6 +23,7 @@ export * from './ImageData/serialization'
23
23
  export * from './ImageData/writeImageDataPixels'
24
24
 
25
25
  export * from './IndexedImage/IndexedImage'
26
+ export * from './IndexedImage/indexedImageToAverageColor'
26
27
 
27
28
  export * from './Input/fileInputChangeToImageData'
28
29
  export * from './Input/fileToImageData'
@@ -33,7 +34,7 @@ export * from './Mask/extractMask'
33
34
  export * from './Mask/invertMask'
34
35
  export * from './Mask/mergeMasks'
35
36
 
36
- export * from './PixelData'
37
+ export * from './PixelData/PixelData'
37
38
  export * from './PixelData/applyMaskToPixelData'
38
39
  export * from './PixelData/blendColorPixelData'
39
40
  export * from './PixelData/blendPixelData'