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.cjs +45 -1
- package/dist/index.dev.cjs.map +1 -1
- package/dist/index.dev.js +44 -1
- package/dist/index.dev.js.map +1 -1
- package/dist/index.prod.cjs +45 -1
- package/dist/index.prod.cjs.map +1 -1
- package/dist/index.prod.d.ts +10 -1
- package/dist/index.prod.js +44 -1
- package/dist/index.prod.js.map +1 -1
- package/package.json +1 -1
- package/src/Algorithm/floodFillSelection.ts +1 -1
- package/src/IndexedImage/indexedImageToAverageColor.ts +65 -0
- package/src/{PixelData.ts → PixelData/PixelData.ts} +1 -1
- package/src/PixelData/applyMaskToPixelData.ts +1 -1
- package/src/PixelData/blendColorPixelData.ts +1 -1
- package/src/PixelData/blendPixelData.ts +1 -1
- package/src/PixelData/clearPixelData.ts +1 -1
- package/src/PixelData/fillPixelData.ts +1 -1
- package/src/PixelData/invertPixelData.ts +1 -1
- package/src/PixelData/pixelDataToAlphaMask.ts +1 -1
- package/src/index.ts +2 -1
package/package.json
CHANGED
|
@@ -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,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 '
|
|
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 '
|
|
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.
|
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'
|