pixel-data-js 0.15.1 → 0.17.0

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.
Files changed (36) hide show
  1. package/dist/index.dev.cjs +1884 -1203
  2. package/dist/index.dev.cjs.map +1 -1
  3. package/dist/index.dev.js +1858 -1190
  4. package/dist/index.dev.js.map +1 -1
  5. package/dist/index.prod.cjs +1884 -1203
  6. package/dist/index.prod.cjs.map +1 -1
  7. package/dist/index.prod.d.ts +320 -66
  8. package/dist/index.prod.js +1858 -1190
  9. package/dist/index.prod.js.map +1 -1
  10. package/package.json +1 -1
  11. package/src/BlendModes/BlendModeRegistry.ts +62 -0
  12. package/src/BlendModes/blend-modes-fast.ts +31 -84
  13. package/src/BlendModes/blend-modes-perfect.ts +343 -215
  14. package/src/BlendModes/blend-modes.ts +28 -30
  15. package/src/History/HistoryManager.ts +83 -0
  16. package/src/History/PixelAccumulator.ts +191 -0
  17. package/src/History/PixelEngineConfig.ts +18 -0
  18. package/src/History/PixelMutator/mutatorApplyMask.ts +20 -0
  19. package/src/History/PixelMutator/mutatorBlendColor.ts +22 -0
  20. package/src/History/PixelMutator/mutatorBlendPixel.ts +37 -0
  21. package/src/History/PixelMutator/mutatorBlendPixelData.ts +24 -0
  22. package/src/History/PixelMutator/mutatorFillPixelData.ts +21 -0
  23. package/src/History/PixelMutator/mutatorInvert.ts +18 -0
  24. package/src/History/PixelMutator.ts +18 -0
  25. package/src/History/PixelPatchTiles.ts +52 -0
  26. package/src/History/PixelWriter.ts +79 -0
  27. package/src/ImageData/{writeImageDataPixels.ts → writeImageDataBuffer.ts} +3 -3
  28. package/src/PixelData/applyCircleBrushToPixelData.ts +69 -0
  29. package/src/PixelData/applyMaskToPixelData.ts +1 -1
  30. package/src/PixelData/applyRectBrushToPixelData.ts +102 -0
  31. package/src/PixelData/blendPixelData.ts +2 -3
  32. package/src/PixelData/invertPixelData.ts +74 -7
  33. package/src/PixelData/writePixelDataBuffer.ts +65 -0
  34. package/src/_types.ts +31 -11
  35. package/src/index.ts +20 -10
  36. package/src/BlendModes/blend-mode-getters.ts +0 -14
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "pixel-data-js",
3
3
  "type": "module",
4
- "version": "0.15.1",
4
+ "version": "0.17.0",
5
5
  "packageManager": "pnpm@10.30.0",
6
6
  "description": "JS Pixel and ImageData operations",
7
7
  "author": {
@@ -0,0 +1,62 @@
1
+ import type { BlendColor32 } from '../_types'
2
+ import type { BaseBlendModes } from './blend-modes'
3
+
4
+ export type BlendModeRegistry<
5
+ BlendModes extends BaseBlendModes = BaseBlendModes,
6
+ Name extends keyof BlendModes = keyof BlendModes,
7
+ Index extends BlendModes[Name] = BlendModes[Name]
8
+ > = ReturnType<typeof makeBlendModeRegistry<BlendModes, Name, Index>>
9
+
10
+ export function makeBlendModeRegistry<
11
+ BlendModes extends BaseBlendModes,
12
+ Name extends keyof BlendModes = keyof BlendModes,
13
+ Index extends BlendModes[Name] = BlendModes[Name]
14
+
15
+ >(
16
+ blendModes: BlendModes,
17
+ initialEntries: Record<Index, BlendColor32>,
18
+ ) {
19
+
20
+ const blendToName = new Map<BlendColor32, Name>()
21
+ const blendToIndex = new Map<BlendColor32, Index>()
22
+ const indexToName: Name[] = []
23
+ const indexToBlend: BlendColor32[] = []
24
+ const nameToBlend = {} as { [K in keyof BlendModes]: BlendColor32 }
25
+ const nameToIndex = {} as Record<Name, Index>
26
+
27
+ const add = (name: Name, index: Index, blendFn: BlendColor32) => {
28
+ if (!Number.isFinite(index)) {
29
+ throw new Error(`Index "${index}" is not a number. Attempting to add name: "${name as string}", index: "${index}"`)
30
+ }
31
+
32
+ if (indexToBlend[index]) {
33
+ throw new Error(`Blend Mode index: ${index} is already used. Attempting to add name: "${name as string}", index: "${index}"`)
34
+ }
35
+
36
+ indexToName[index] = name
37
+ indexToBlend[index] = blendFn
38
+ blendToIndex.set(blendFn, index)
39
+ blendToName.set(blendFn, name)
40
+ nameToBlend[name] = blendFn
41
+ nameToIndex[name] = index
42
+ }
43
+
44
+ for (const [name, index] of Object.entries(blendModes)) {
45
+ const blend = initialEntries[index as Index]
46
+ add(name as Name, index as Index, blend)
47
+ }
48
+
49
+ return {
50
+ nameToBlend,
51
+ nameToIndex,
52
+
53
+ blendToIndex,
54
+ blendToName,
55
+
56
+ indexToBlend,
57
+ indexToName,
58
+
59
+ indexType: null as unknown as Index,
60
+ nameType: null as unknown as Name,
61
+ }
62
+ }
@@ -1,6 +1,6 @@
1
1
  import type { BlendColor32, Color32 } from '../_types'
2
- import type { BaseBlendToIndexGetter, BaseIndexToBlendGetter } from './blend-mode-getters'
3
- import { BlendMode, type BlendModeIndex, overwriteBase } from './blend-modes'
2
+ import { BaseBlendMode, overwriteBase } from './blend-modes'
3
+ import { makeBlendModeRegistry } from './BlendModeRegistry'
4
4
 
5
5
  export const overwriteFast = overwriteBase
6
6
 
@@ -575,88 +575,35 @@ export const divideFast: BlendColor32 = (src, dst) => {
575
575
  return ((a << 24) | (b << 16) | (g << 8) | r) >>> 0 as Color32
576
576
  }
577
577
 
578
- export const FAST_BLENDER_REGISTRY = [
579
- [BlendMode.overwrite, overwriteFast],
580
- [BlendMode.sourceOver, sourceOverFast],
581
-
582
- [BlendMode.darken, darkenFast],
583
- [BlendMode.multiply, multiplyFast],
584
- [BlendMode.colorBurn, colorBurnFast],
585
- [BlendMode.linearBurn, linearBurnFast],
586
- [BlendMode.darkerColor, darkerFast],
587
-
588
- [BlendMode.lighten, lightenFast],
589
- [BlendMode.screen, screenFast],
590
- [BlendMode.colorDodge, colorDodgeFast],
591
- [BlendMode.linearDodge, linearDodgeFast],
592
- [BlendMode.lighterColor, lighterFast],
593
-
594
- [BlendMode.overlay, overlayFast],
595
- [BlendMode.softLight, softLightFast],
596
- [BlendMode.hardLight, hardLightFast],
597
- [BlendMode.vividLight, vividLightFast],
598
- [BlendMode.linearLight, linearLightFast],
599
- [BlendMode.pinLight, pinLightFast],
600
- [BlendMode.hardMix, hardMixFast],
601
-
602
- [BlendMode.difference, differenceFast],
603
- [BlendMode.exclusion, exclusionFast],
604
- [BlendMode.subtract, subtractFast],
605
- [BlendMode.divide, divideFast],
606
- ] as const
607
-
608
- export type RegisteredFastBlender = typeof FAST_BLENDER_REGISTRY[number][1]
609
-
610
- export const FAST_BLEND_MODES: BlendColor32[] = []
611
-
612
- for (const [index, blend] of FAST_BLENDER_REGISTRY) {
613
- FAST_BLEND_MODES[index as BlendModeIndex] = blend
578
+ export const BASE_FAST_BLEND_MODE_FUNCTIONS: Record<number, BlendColor32> = {
579
+ [BaseBlendMode.overwrite]: overwriteFast,
580
+ [BaseBlendMode.sourceOver]: sourceOverFast,
581
+ [BaseBlendMode.darken]: darkenFast,
582
+ [BaseBlendMode.multiply]: multiplyFast,
583
+ [BaseBlendMode.colorBurn]: colorBurnFast,
584
+ [BaseBlendMode.linearBurn]: linearBurnFast,
585
+ [BaseBlendMode.darkerColor]: darkerFast,
586
+
587
+ [BaseBlendMode.lighten]: lightenFast,
588
+ [BaseBlendMode.screen]: screenFast,
589
+ [BaseBlendMode.colorDodge]: colorDodgeFast,
590
+ [BaseBlendMode.linearDodge]: linearDodgeFast,
591
+ [BaseBlendMode.lighterColor]: lighterFast,
592
+
593
+ [BaseBlendMode.overlay]: overlayFast,
594
+ [BaseBlendMode.softLight]: softLightFast,
595
+ [BaseBlendMode.hardLight]: hardLightFast,
596
+ [BaseBlendMode.vividLight]: vividLightFast,
597
+ [BaseBlendMode.linearLight]: linearLightFast,
598
+ [BaseBlendMode.pinLight]: pinLightFast,
599
+ [BaseBlendMode.hardMix]: hardMixFast,
600
+
601
+ [BaseBlendMode.difference]: differenceFast,
602
+ [BaseBlendMode.exclusion]: exclusionFast,
603
+ [BaseBlendMode.subtract]: subtractFast,
604
+ [BaseBlendMode.divide]: divideFast,
614
605
  }
615
606
 
616
- export const FAST_BLEND_TO_INDEX = new Map<RegisteredFastBlender, BlendModeIndex>(
617
- FAST_BLENDER_REGISTRY.map((entry, index) => {
618
- return [
619
- entry[1],
620
- index as BlendModeIndex,
621
- ]
622
- }),
623
- ) as BaseBlendToIndexGetter<RegisteredFastBlender>
624
-
625
- export const INDEX_TO_FAST_BLEND = new Map<BlendModeIndex, RegisteredFastBlender>(
626
- FAST_BLENDER_REGISTRY.map((entry, index) => {
627
- return [
628
- index as BlendModeIndex,
629
- entry[1],
630
- ]
631
- }),
632
- ) as BaseIndexToBlendGetter<RegisteredFastBlender>
633
-
634
- export type FastBlendModes = {
635
- [K in keyof typeof BlendMode]: RegisteredFastBlender
607
+ export function makeFastBlendModeRegistry() {
608
+ return makeBlendModeRegistry(BaseBlendMode, BASE_FAST_BLEND_MODE_FUNCTIONS)
636
609
  }
637
-
638
- export const FAST_BLEND_MODE_BY_NAME: FastBlendModes = {
639
- overwrite: overwriteFast,
640
- sourceOver: sourceOverFast,
641
- darken: darkenFast,
642
- multiply: multiplyFast,
643
- colorBurn: colorBurnFast,
644
- linearBurn: linearBurnFast,
645
- darkerColor: darkerFast,
646
- lighten: lightenFast,
647
- screen: screenFast,
648
- colorDodge: colorDodgeFast,
649
- linearDodge: linearDodgeFast,
650
- lighterColor: lighterFast,
651
- overlay: overlayFast,
652
- softLight: softLightFast,
653
- hardLight: hardLightFast,
654
- vividLight: vividLightFast,
655
- linearLight: linearLightFast,
656
- pinLight: pinLightFast,
657
- hardMix: hardMixFast,
658
- difference: differenceFast,
659
- exclusion: exclusionFast,
660
- subtract: subtractFast,
661
- divide: divideFast,
662
- } as const