@nice2dev/ui-graphics 1.3.14 → 1.3.16

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 (294) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/dist/cjs/animation/AnimatedPerson.js +470 -143
  3. package/dist/cjs/animation/AnimationEditor.js +289 -101
  4. package/dist/cjs/animation/Audience.js +83 -61
  5. package/dist/cjs/animation/BodyRenderer.js +69 -28
  6. package/dist/cjs/animation/animationHelper.js +281 -288
  7. package/dist/cjs/animation/characterTypes.js +200 -203
  8. package/dist/cjs/animation/choreoDSL.js +205 -219
  9. package/dist/cjs/animation/choreography.js +4 -32
  10. package/dist/cjs/animation/karaokeIntegration.js +68 -77
  11. package/dist/cjs/animation/lottieExport.js +108 -114
  12. package/dist/cjs/animation/motionPath.js +225 -248
  13. package/dist/cjs/animation/physics.js +227 -254
  14. package/dist/cjs/animation/rig/RigPlayer.js +53 -56
  15. package/dist/cjs/animation/rig/RiggedBody.js +71 -26
  16. package/dist/cjs/animation/rig/choreoRigBridge.js +72 -77
  17. package/dist/cjs/animation/rig/ik.js +13 -18
  18. package/dist/cjs/animation/rig/poseOps.js +34 -41
  19. package/dist/cjs/animation/rig/presets.js +64 -72
  20. package/dist/cjs/animation/rig/rigMath.js +25 -32
  21. package/dist/cjs/animation/rig/rigTypes.js +59 -63
  22. package/dist/cjs/animation/shapes/arms.js +31 -13
  23. package/dist/cjs/animation/shapes/eyes.js +129 -47
  24. package/dist/cjs/animation/shapes/hair.js +72 -45
  25. package/dist/cjs/animation/shapes/heads.js +131 -57
  26. package/dist/cjs/animation/shapes/headwear.js +47 -38
  27. package/dist/cjs/animation/shapes/legs.js +31 -13
  28. package/dist/cjs/animation/shapes/mouths.js +120 -44
  29. package/dist/cjs/animation/shapes/noses.js +80 -25
  30. package/dist/cjs/animation/shapes/outfits.js +101 -39
  31. package/dist/cjs/animation/shapes/torsos.js +15 -11
  32. package/dist/cjs/animation/spineExport.js +103 -121
  33. package/dist/cjs/core/LocalUI.js +108 -43
  34. package/dist/cjs/core/NiceBrushEngine.js +758 -552
  35. package/dist/cjs/core/collaboration.js +249 -251
  36. package/dist/cjs/core/colorBlindness.js +100 -115
  37. package/dist/cjs/core/dragDrop.js +155 -158
  38. package/dist/cjs/core/editorAsk.js +51 -41
  39. package/dist/cjs/core/fixes.js +140 -171
  40. package/dist/cjs/core/gridGuides.js +166 -201
  41. package/dist/cjs/core/historyVisual.js +156 -135
  42. package/dist/cjs/core/i18n.js +13 -14
  43. package/dist/cjs/core/integrations.js +238 -249
  44. package/dist/cjs/core/minimap.js +149 -151
  45. package/dist/cjs/core/plugins.js +98 -113
  46. package/dist/cjs/core/rtl.js +71 -83
  47. package/dist/cjs/core/shortcuts.js +196 -225
  48. package/dist/cjs/core/theme.js +142 -149
  49. package/dist/cjs/core/yjsCollaboration.js +232 -293
  50. package/dist/cjs/design/design-tools.js +12 -22
  51. package/dist/cjs/font/NiceFontEditor.js +877 -316
  52. package/dist/cjs/font/fontFormatUtils.js +388 -448
  53. package/dist/cjs/game/GameAsset2dEditor.js +1201 -392
  54. package/dist/cjs/game/GameAsset2dEditor.module.css.js +146 -5
  55. package/dist/cjs/game/gameAssetTypes.js +7 -16
  56. package/dist/cjs/game/gameAssetUtils.js +390 -413
  57. package/dist/cjs/game/useGameAssetEditor.js +820 -763
  58. package/dist/cjs/index.js +81 -84
  59. package/dist/cjs/nice2dev-ui-graphics.css +1 -1
  60. package/dist/cjs/node_modules/lib0/array.js +8 -70
  61. package/dist/cjs/node_modules/lib0/binary.js +3 -20
  62. package/dist/cjs/node_modules/lib0/broadcastchannel.js +24 -86
  63. package/dist/cjs/node_modules/lib0/buffer.js +15 -82
  64. package/dist/cjs/node_modules/lib0/conditions.js +3 -16
  65. package/dist/cjs/node_modules/lib0/decoding.js +108 -287
  66. package/dist/cjs/node_modules/lib0/dom.js +14 -44
  67. package/dist/cjs/node_modules/lib0/encoding.js +68 -418
  68. package/dist/cjs/node_modules/lib0/environment.js +26 -92
  69. package/dist/cjs/node_modules/lib0/error.js +5 -29
  70. package/dist/cjs/node_modules/lib0/function.js +27 -66
  71. package/dist/cjs/node_modules/lib0/iterator.js +8 -33
  72. package/dist/cjs/node_modules/lib0/logging.common.js +12 -22
  73. package/dist/cjs/node_modules/lib0/logging.js +32 -71
  74. package/dist/cjs/node_modules/lib0/map.js +16 -86
  75. package/dist/cjs/node_modules/lib0/math.js +3 -32
  76. package/dist/cjs/node_modules/lib0/number.js +4 -15
  77. package/dist/cjs/node_modules/lib0/object.js +14 -88
  78. package/dist/cjs/node_modules/lib0/observable.js +38 -78
  79. package/dist/cjs/node_modules/lib0/pair.js +3 -20
  80. package/dist/cjs/node_modules/lib0/prng.js +8 -77
  81. package/dist/cjs/node_modules/lib0/promise.js +6 -30
  82. package/dist/cjs/node_modules/lib0/random.js +8 -23
  83. package/dist/cjs/node_modules/lib0/schema.js +313 -687
  84. package/dist/cjs/node_modules/lib0/set.js +3 -10
  85. package/dist/cjs/node_modules/lib0/storage.js +20 -55
  86. package/dist/cjs/node_modules/lib0/string.js +17 -72
  87. package/dist/cjs/node_modules/lib0/symbol.js +2 -12
  88. package/dist/cjs/node_modules/lib0/time.js +2 -15
  89. package/dist/cjs/node_modules/lib0/trait/equality.js +3 -28
  90. package/dist/cjs/node_modules/lib0/url.js +4 -18
  91. package/dist/cjs/node_modules/lib0/webcrypto.js +2 -5
  92. package/dist/cjs/node_modules/y-protocols/auth.js +5 -19
  93. package/dist/cjs/node_modules/y-protocols/awareness.js +53 -116
  94. package/dist/cjs/node_modules/y-protocols/sync.js +16 -103
  95. package/dist/cjs/node_modules/y-websocket/src/y-websocket.js +77 -219
  96. package/dist/cjs/node_modules/yjs/dist/yjs.js +2128 -4868
  97. package/dist/cjs/photo/FilterThumb.js +37 -25
  98. package/dist/cjs/photo/NiceAITools.js +679 -467
  99. package/dist/cjs/photo/PhotoEditor.js +34 -20
  100. package/dist/cjs/photo/PhotoEditor.module.css.js +296 -5
  101. package/dist/cjs/photo/PhotoEditorCanvas.js +250 -91
  102. package/dist/cjs/photo/PhotoEditorLeftPanel.js +1086 -224
  103. package/dist/cjs/photo/PhotoEditorToolbar.js +135 -19
  104. package/dist/cjs/photo/photoEditorActions.js +89 -72
  105. package/dist/cjs/photo/photoEditorTypes.js +29 -32
  106. package/dist/cjs/photo/photoFilters.js +959 -985
  107. package/dist/cjs/photo/photoOverlays.js +899 -945
  108. package/dist/cjs/photo/photoSelection.js +427 -499
  109. package/dist/cjs/photo/usePhotoEditor.js +739 -741
  110. package/dist/cjs/pixel/HSVPicker.js +191 -175
  111. package/dist/cjs/pixel/PixelEditor.js +159 -48
  112. package/dist/cjs/pixel/PixelEditor.module.css.js +209 -5
  113. package/dist/cjs/pixel/PixelEditorCanvas.js +29 -16
  114. package/dist/cjs/pixel/PixelEditorMenuBar.js +160 -162
  115. package/dist/cjs/pixel/PixelEditorRightPanel.js +377 -45
  116. package/dist/cjs/pixel/PixelEditorStatusBar.js +47 -20
  117. package/dist/cjs/pixel/PixelEditorTimeline.js +106 -23
  118. package/dist/cjs/pixel/PixelEditorToolbar.js +99 -21
  119. package/dist/cjs/pixel/SpriteGeneratorPanel.js +483 -303
  120. package/dist/cjs/pixel/animalGeneratorPresets.js +776 -601
  121. package/dist/cjs/pixel/asepriteFormat.js +511 -560
  122. package/dist/cjs/pixel/buildingGeneratorPresets.js +693 -520
  123. package/dist/cjs/pixel/characterGeneratorPresets.js +644 -544
  124. package/dist/cjs/pixel/pixel-pro.js +52 -63
  125. package/dist/cjs/pixel/pixelEditorExports.js +388 -428
  126. package/dist/cjs/pixel/pixelEditorTypes.js +91 -108
  127. package/dist/cjs/pixel/pixelEditorUtils.js +519 -561
  128. package/dist/cjs/pixel/spriteGeneratorCore.js +206 -257
  129. package/dist/cjs/pixel/usePixelEditor.js +1321 -1343
  130. package/dist/cjs/texture/Nice3DTexturePainter.js +707 -440
  131. package/dist/cjs/ui/NiceUIDesigner.js +954 -527
  132. package/dist/cjs/vector/NiceSvgToCode.js +586 -446
  133. package/dist/cjs/vector/VectorEditor.js +290 -117
  134. package/dist/cjs/vector/VectorEditor.module.css.js +86 -5
  135. package/dist/cjs/vector/VectorEditorMenuBar.js +333 -275
  136. package/dist/cjs/vector/VectorEditorRightPanel.js +1094 -371
  137. package/dist/cjs/vector/VectorEditorShapeRenderer.js +198 -60
  138. package/dist/cjs/vector/VectorEditorStatusBar.js +39 -13
  139. package/dist/cjs/vector/useVectorEditor.js +848 -889
  140. package/dist/cjs/vector/vectorAnimation.js +110 -137
  141. package/dist/cjs/vector/vectorBooleanOps.js +398 -485
  142. package/dist/cjs/vector/vectorEditorExport.js +201 -203
  143. package/dist/cjs/vector/vectorEditorImport.js +173 -189
  144. package/dist/cjs/vector/vectorEditorTypes.js +42 -49
  145. package/dist/cjs/vector/vectorGradients.js +151 -174
  146. package/dist/cjs/vector/vectorIconExport.js +52 -59
  147. package/dist/cjs/vector/vectorIconImport.js +169 -219
  148. package/dist/esm/animation/AnimatedPerson.js +470 -141
  149. package/dist/esm/animation/AnimationEditor.js +290 -100
  150. package/dist/esm/animation/Audience.js +83 -59
  151. package/dist/esm/animation/BodyRenderer.js +71 -25
  152. package/dist/esm/animation/animationHelper.js +284 -287
  153. package/dist/esm/animation/characterTypes.js +204 -202
  154. package/dist/esm/animation/choreoDSL.js +212 -218
  155. package/dist/esm/animation/choreography.js +8 -31
  156. package/dist/esm/animation/karaokeIntegration.js +74 -76
  157. package/dist/esm/animation/lottieExport.js +112 -113
  158. package/dist/esm/animation/motionPath.js +235 -247
  159. package/dist/esm/animation/physics.js +235 -253
  160. package/dist/esm/animation/rig/RigPlayer.js +53 -54
  161. package/dist/esm/animation/rig/RiggedBody.js +71 -24
  162. package/dist/esm/animation/rig/choreoRigBridge.js +74 -76
  163. package/dist/esm/animation/rig/ik.js +14 -17
  164. package/dist/esm/animation/rig/poseOps.js +37 -40
  165. package/dist/esm/animation/rig/presets.js +70 -71
  166. package/dist/esm/animation/rig/rigMath.js +28 -31
  167. package/dist/esm/animation/rig/rigTypes.js +62 -62
  168. package/dist/esm/animation/shapes/arms.js +32 -12
  169. package/dist/esm/animation/shapes/eyes.js +135 -46
  170. package/dist/esm/animation/shapes/hair.js +80 -44
  171. package/dist/esm/animation/shapes/heads.js +140 -56
  172. package/dist/esm/animation/shapes/headwear.js +54 -37
  173. package/dist/esm/animation/shapes/legs.js +32 -12
  174. package/dist/esm/animation/shapes/mouths.js +127 -43
  175. package/dist/esm/animation/shapes/noses.js +87 -24
  176. package/dist/esm/animation/shapes/outfits.js +106 -38
  177. package/dist/esm/animation/shapes/torsos.js +16 -10
  178. package/dist/esm/animation/spineExport.js +106 -120
  179. package/dist/esm/core/LocalUI.js +111 -42
  180. package/dist/esm/core/NiceBrushEngine.js +761 -551
  181. package/dist/esm/core/collaboration.js +255 -250
  182. package/dist/esm/core/colorBlindness.js +106 -114
  183. package/dist/esm/core/dragDrop.js +157 -157
  184. package/dist/esm/core/editorAsk.js +53 -40
  185. package/dist/esm/core/fixes.js +145 -170
  186. package/dist/esm/core/gridGuides.js +178 -200
  187. package/dist/esm/core/historyVisual.js +158 -134
  188. package/dist/esm/core/i18n.js +15 -13
  189. package/dist/esm/core/integrations.js +244 -248
  190. package/dist/esm/core/minimap.js +151 -150
  191. package/dist/esm/core/plugins.js +102 -112
  192. package/dist/esm/core/rtl.js +77 -82
  193. package/dist/esm/core/shortcuts.js +200 -224
  194. package/dist/esm/core/theme.js +150 -148
  195. package/dist/esm/core/yjsCollaboration.js +240 -292
  196. package/dist/esm/design/design-tools.js +13 -21
  197. package/dist/esm/font/NiceFontEditor.js +880 -315
  198. package/dist/esm/font/fontFormatUtils.js +394 -447
  199. package/dist/esm/game/GameAsset2dEditor.js +1202 -391
  200. package/dist/esm/game/GameAsset2dEditor.module.css.js +146 -3
  201. package/dist/esm/game/gameAssetTypes.js +8 -15
  202. package/dist/esm/game/gameAssetUtils.js +408 -412
  203. package/dist/esm/game/useGameAssetEditor.js +821 -762
  204. package/dist/esm/index.js +332 -69
  205. package/dist/esm/nice2dev-ui-graphics.css +1 -1
  206. package/dist/esm/node_modules/lib0/array.js +15 -69
  207. package/dist/esm/node_modules/lib0/binary.js +14 -19
  208. package/dist/esm/node_modules/lib0/broadcastchannel.js +27 -85
  209. package/dist/esm/node_modules/lib0/buffer.js +20 -80
  210. package/dist/esm/node_modules/lib0/conditions.js +4 -15
  211. package/dist/esm/node_modules/lib0/decoding.js +128 -286
  212. package/dist/esm/node_modules/lib0/dom.js +20 -43
  213. package/dist/esm/node_modules/lib0/encoding.js +90 -415
  214. package/dist/esm/node_modules/lib0/environment.js +33 -91
  215. package/dist/esm/node_modules/lib0/error.js +8 -28
  216. package/dist/esm/node_modules/lib0/function.js +31 -65
  217. package/dist/esm/node_modules/lib0/iterator.js +11 -32
  218. package/dist/esm/node_modules/lib0/logging.common.js +22 -21
  219. package/dist/esm/node_modules/lib0/logging.js +44 -70
  220. package/dist/esm/node_modules/lib0/map.js +21 -85
  221. package/dist/esm/node_modules/lib0/math.js +9 -31
  222. package/dist/esm/node_modules/lib0/number.js +7 -14
  223. package/dist/esm/node_modules/lib0/object.js +26 -87
  224. package/dist/esm/node_modules/lib0/observable.js +40 -77
  225. package/dist/esm/node_modules/lib0/pair.js +5 -19
  226. package/dist/esm/node_modules/lib0/prng.js +15 -76
  227. package/dist/esm/node_modules/lib0/promise.js +7 -29
  228. package/dist/esm/node_modules/lib0/random.js +10 -22
  229. package/dist/esm/node_modules/lib0/schema.js +367 -686
  230. package/dist/esm/node_modules/lib0/set.js +4 -9
  231. package/dist/esm/node_modules/lib0/storage.js +23 -54
  232. package/dist/esm/node_modules/lib0/string.js +26 -71
  233. package/dist/esm/node_modules/lib0/symbol.js +3 -11
  234. package/dist/esm/node_modules/lib0/time.js +3 -14
  235. package/dist/esm/node_modules/lib0/trait/equality.js +5 -27
  236. package/dist/esm/node_modules/lib0/url.js +5 -17
  237. package/dist/esm/node_modules/lib0/webcrypto.js +3 -4
  238. package/dist/esm/node_modules/y-protocols/auth.js +7 -18
  239. package/dist/esm/node_modules/y-protocols/awareness.js +58 -115
  240. package/dist/esm/node_modules/y-protocols/sync.js +26 -102
  241. package/dist/esm/node_modules/y-websocket/src/y-websocket.js +82 -218
  242. package/dist/esm/node_modules/yjs/dist/yjs.js +2263 -4899
  243. package/dist/esm/photo/FilterThumb.js +37 -23
  244. package/dist/esm/photo/NiceAITools.js +685 -466
  245. package/dist/esm/photo/PhotoEditor.js +34 -18
  246. package/dist/esm/photo/PhotoEditor.module.css.js +296 -3
  247. package/dist/esm/photo/PhotoEditorCanvas.js +250 -89
  248. package/dist/esm/photo/PhotoEditorLeftPanel.js +1086 -222
  249. package/dist/esm/photo/PhotoEditorToolbar.js +135 -17
  250. package/dist/esm/photo/photoEditorActions.js +93 -71
  251. package/dist/esm/photo/photoEditorTypes.js +32 -31
  252. package/dist/esm/photo/photoFilters.js +967 -984
  253. package/dist/esm/photo/photoOverlays.js +913 -944
  254. package/dist/esm/photo/photoSelection.js +438 -498
  255. package/dist/esm/photo/usePhotoEditor.js +740 -740
  256. package/dist/esm/pixel/HSVPicker.js +192 -174
  257. package/dist/esm/pixel/PixelEditor.js +160 -47
  258. package/dist/esm/pixel/PixelEditor.module.css.js +209 -3
  259. package/dist/esm/pixel/PixelEditorCanvas.js +29 -14
  260. package/dist/esm/pixel/PixelEditorMenuBar.js +160 -160
  261. package/dist/esm/pixel/PixelEditorRightPanel.js +377 -43
  262. package/dist/esm/pixel/PixelEditorStatusBar.js +47 -18
  263. package/dist/esm/pixel/PixelEditorTimeline.js +106 -21
  264. package/dist/esm/pixel/PixelEditorToolbar.js +99 -19
  265. package/dist/esm/pixel/SpriteGeneratorPanel.js +483 -301
  266. package/dist/esm/pixel/animalGeneratorPresets.js +782 -600
  267. package/dist/esm/pixel/asepriteFormat.js +517 -559
  268. package/dist/esm/pixel/buildingGeneratorPresets.js +699 -519
  269. package/dist/esm/pixel/characterGeneratorPresets.js +650 -543
  270. package/dist/esm/pixel/pixel-pro.js +53 -62
  271. package/dist/esm/pixel/pixelEditorExports.js +397 -427
  272. package/dist/esm/pixel/pixelEditorTypes.js +103 -107
  273. package/dist/esm/pixel/pixelEditorUtils.js +551 -560
  274. package/dist/esm/pixel/spriteGeneratorCore.js +223 -256
  275. package/dist/esm/pixel/usePixelEditor.js +1322 -1342
  276. package/dist/esm/texture/Nice3DTexturePainter.js +709 -439
  277. package/dist/esm/ui/NiceUIDesigner.js +957 -526
  278. package/dist/esm/vector/NiceSvgToCode.js +589 -445
  279. package/dist/esm/vector/VectorEditor.js +291 -116
  280. package/dist/esm/vector/VectorEditor.module.css.js +86 -3
  281. package/dist/esm/vector/VectorEditorMenuBar.js +333 -273
  282. package/dist/esm/vector/VectorEditorRightPanel.js +1094 -369
  283. package/dist/esm/vector/VectorEditorShapeRenderer.js +199 -59
  284. package/dist/esm/vector/VectorEditorStatusBar.js +39 -11
  285. package/dist/esm/vector/useVectorEditor.js +849 -888
  286. package/dist/esm/vector/vectorAnimation.js +115 -136
  287. package/dist/esm/vector/vectorBooleanOps.js +401 -484
  288. package/dist/esm/vector/vectorEditorExport.js +207 -202
  289. package/dist/esm/vector/vectorEditorImport.js +174 -188
  290. package/dist/esm/vector/vectorEditorTypes.js +50 -48
  291. package/dist/esm/vector/vectorGradients.js +158 -173
  292. package/dist/esm/vector/vectorIconExport.js +53 -58
  293. package/dist/esm/vector/vectorIconImport.js +171 -218
  294. package/package.json +9 -15
@@ -1,570 +1,498 @@
1
- 'use strict';
2
-
3
- var uiCore = require('@nice2dev/ui-core');
4
-
5
- /* ────────────────────────────────────────────────────────────────
6
- SELECTION MASK UTILITIES
7
- ──────────────────────────────────────────────────────────────── */
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const uiCore = require("@nice2dev/ui-core");
8
4
  function createEmptyMask(width, height) {
9
- return {
10
- width,
11
- height,
12
- data: new Uint8Array(width * height),
13
- };
5
+ return {
6
+ width,
7
+ height,
8
+ data: new Uint8Array(width * height)
9
+ };
14
10
  }
15
11
  function invertMask(mask) {
16
- const result = createEmptyMask(mask.width, mask.height);
17
- for (let i = 0; i < mask.data.length; i++) {
18
- result.data[i] = 255 - mask.data[i];
19
- }
20
- return result;
12
+ const result = createEmptyMask(mask.width, mask.height);
13
+ for (let i = 0; i < mask.data.length; i++) {
14
+ result.data[i] = 255 - mask.data[i];
15
+ }
16
+ return result;
21
17
  }
22
18
  function combineMasks(maskA, maskB, mode) {
23
- if (maskA.width !== maskB.width || maskA.height !== maskB.height) {
24
- throw new Error('Mask dimensions must match');
25
- }
26
- const result = createEmptyMask(maskA.width, maskA.height);
27
- for (let i = 0; i < maskA.data.length; i++) {
28
- const a = maskA.data[i];
29
- const b = maskB.data[i];
30
- switch (mode) {
31
- case 'replace':
32
- result.data[i] = b;
33
- break;
34
- case 'add':
35
- result.data[i] = Math.min(255, a + b);
36
- break;
37
- case 'subtract':
38
- result.data[i] = Math.max(0, a - b);
39
- break;
40
- case 'intersect':
41
- result.data[i] = Math.min(a, b);
42
- break;
43
- }
19
+ if (maskA.width !== maskB.width || maskA.height !== maskB.height) {
20
+ throw new Error("Mask dimensions must match");
21
+ }
22
+ const result = createEmptyMask(maskA.width, maskA.height);
23
+ for (let i = 0; i < maskA.data.length; i++) {
24
+ const a = maskA.data[i];
25
+ const b = maskB.data[i];
26
+ switch (mode) {
27
+ case "replace":
28
+ result.data[i] = b;
29
+ break;
30
+ case "add":
31
+ result.data[i] = Math.min(255, a + b);
32
+ break;
33
+ case "subtract":
34
+ result.data[i] = Math.max(0, a - b);
35
+ break;
36
+ case "intersect":
37
+ result.data[i] = Math.min(a, b);
38
+ break;
44
39
  }
45
- return result;
40
+ }
41
+ return result;
46
42
  }
47
43
  function featherMask(mask, radius) {
48
- if (radius <= 0) {
49
- return mask;
50
- }
51
- const result = createEmptyMask(mask.width, mask.height);
52
- const kernel = createGaussianKernel(radius);
53
- const kernelSize = kernel.length;
54
- const halfKernel = Math.floor(kernelSize / 2);
55
- for (let y = 0; y < mask.height; y++) {
56
- for (let x = 0; x < mask.width; x++) {
57
- let sum = 0;
58
- let weightSum = 0;
59
- for (let ky = 0; ky < kernelSize; ky++) {
60
- for (let kx = 0; kx < kernelSize; kx++) {
61
- const sx = x + kx - halfKernel;
62
- const sy = y + ky - halfKernel;
63
- if (sx >= 0 && sx < mask.width && sy >= 0 && sy < mask.height) {
64
- const weight = kernel[ky][kx];
65
- sum += mask.data[sy * mask.width + sx] * weight;
66
- weightSum += weight;
67
- }
68
- }
69
- }
70
- result.data[y * mask.width + x] = Math.round(sum / weightSum);
44
+ if (radius <= 0) {
45
+ return mask;
46
+ }
47
+ const result = createEmptyMask(mask.width, mask.height);
48
+ const kernel = createGaussianKernel(radius);
49
+ const kernelSize = kernel.length;
50
+ const halfKernel = Math.floor(kernelSize / 2);
51
+ for (let y = 0; y < mask.height; y++) {
52
+ for (let x = 0; x < mask.width; x++) {
53
+ let sum = 0;
54
+ let weightSum = 0;
55
+ for (let ky = 0; ky < kernelSize; ky++) {
56
+ for (let kx = 0; kx < kernelSize; kx++) {
57
+ const sx = x + kx - halfKernel;
58
+ const sy = y + ky - halfKernel;
59
+ if (sx >= 0 && sx < mask.width && sy >= 0 && sy < mask.height) {
60
+ const weight = kernel[ky][kx];
61
+ sum += mask.data[sy * mask.width + sx] * weight;
62
+ weightSum += weight;
63
+ }
71
64
  }
65
+ }
66
+ result.data[y * mask.width + x] = Math.round(sum / weightSum);
72
67
  }
73
- return result;
68
+ }
69
+ return result;
74
70
  }
75
71
  function createGaussianKernel(radius) {
76
- const size = radius * 2 + 1;
77
- const kernel = [];
78
- const sigma = radius / 3;
79
- const twoSigmaSq = 2 * sigma * sigma;
80
- for (let y = 0; y < size; y++) {
81
- kernel[y] = [];
82
- for (let x = 0; x < size; x++) {
83
- const dx = x - radius;
84
- const dy = y - radius;
85
- kernel[y][x] = Math.exp(-(dx * dx + dy * dy) / twoSigmaSq);
86
- }
72
+ const size = radius * 2 + 1;
73
+ const kernel = [];
74
+ const sigma = radius / 3;
75
+ const twoSigmaSq = 2 * sigma * sigma;
76
+ for (let y = 0; y < size; y++) {
77
+ kernel[y] = [];
78
+ for (let x = 0; x < size; x++) {
79
+ const dx = x - radius;
80
+ const dy = y - radius;
81
+ kernel[y][x] = Math.exp(-(dx * dx + dy * dy) / twoSigmaSq);
87
82
  }
88
- return kernel;
83
+ }
84
+ return kernel;
89
85
  }
90
86
  const DEFAULT_WAND_OPTIONS = {
91
- tolerance: 32,
92
- contiguous: true,
93
- antiAlias: true,
94
- feather: 0,
87
+ tolerance: 32,
88
+ contiguous: true,
89
+ antiAlias: true,
90
+ feather: 0
95
91
  };
96
92
  function magicWandSelect(imageData, startX, startY, options = {}) {
97
- const opts = { ...DEFAULT_WAND_OPTIONS, ...options };
98
- const { width, height, data } = imageData;
99
- const mask = createEmptyMask(width, height);
100
- // Get target color
101
- const startIdx = (startY * width + startX) * 4;
102
- const targetR = data[startIdx];
103
- const targetG = data[startIdx + 1];
104
- const targetB = data[startIdx + 2];
105
- // Color similarity function
106
- const isColorSimilar = (idx) => {
107
- const r = data[idx];
108
- const g = data[idx + 1];
109
- const b = data[idx + 2];
110
- const diff = Math.abs(r - targetR) + Math.abs(g - targetG) + Math.abs(b - targetB);
111
- return diff <= opts.tolerance * 3;
112
- };
113
- // For anti-aliasing, calculate how similar a pixel is (0-255)
114
- const getSimilarity = (idx) => {
115
- const r = data[idx];
116
- const g = data[idx + 1];
117
- const b = data[idx + 2];
118
- const diff = Math.abs(r - targetR) + Math.abs(g - targetG) + Math.abs(b - targetB);
119
- const maxDiff = opts.tolerance * 3;
120
- if (diff >= maxDiff) {
121
- return 0;
122
- }
123
- return Math.round(255 * (1 - diff / maxDiff));
124
- };
125
- if (opts.contiguous) {
126
- // Flood fill algorithm
127
- const visited = new Uint8Array(width * height);
128
- const stack = [startX, startY];
129
- while (stack.length > 0) {
130
- const y = stack.pop();
131
- const x = stack.pop();
132
- if (x < 0 || x >= width || y < 0 || y >= height) {
133
- continue;
134
- }
135
- const pixelIdx = y * width + x;
136
- if (visited[pixelIdx]) {
137
- continue;
138
- }
139
- visited[pixelIdx] = 1;
140
- const colorIdx = pixelIdx * 4;
141
- if (!isColorSimilar(colorIdx)) {
142
- continue;
143
- }
144
- mask.data[pixelIdx] = opts.antiAlias ? getSimilarity(colorIdx) : 255;
145
- // Add neighbors
146
- stack.push(x - 1, y);
147
- stack.push(x + 1, y);
148
- stack.push(x, y - 1);
149
- stack.push(x, y + 1);
150
- }
93
+ const opts = { ...DEFAULT_WAND_OPTIONS, ...options };
94
+ const { width, height, data } = imageData;
95
+ const mask = createEmptyMask(width, height);
96
+ const startIdx = (startY * width + startX) * 4;
97
+ const targetR = data[startIdx];
98
+ const targetG = data[startIdx + 1];
99
+ const targetB = data[startIdx + 2];
100
+ const isColorSimilar = (idx) => {
101
+ const r = data[idx];
102
+ const g = data[idx + 1];
103
+ const b = data[idx + 2];
104
+ const diff = Math.abs(r - targetR) + Math.abs(g - targetG) + Math.abs(b - targetB);
105
+ return diff <= opts.tolerance * 3;
106
+ };
107
+ const getSimilarity = (idx) => {
108
+ const r = data[idx];
109
+ const g = data[idx + 1];
110
+ const b = data[idx + 2];
111
+ const diff = Math.abs(r - targetR) + Math.abs(g - targetG) + Math.abs(b - targetB);
112
+ const maxDiff = opts.tolerance * 3;
113
+ if (diff >= maxDiff) {
114
+ return 0;
151
115
  }
152
- else {
153
- // Select all similar colors regardless of position
154
- for (let i = 0; i < width * height; i++) {
155
- const colorIdx = i * 4;
156
- if (isColorSimilar(colorIdx)) {
157
- mask.data[i] = opts.antiAlias ? getSimilarity(colorIdx) : 255;
158
- }
159
- }
116
+ return Math.round(255 * (1 - diff / maxDiff));
117
+ };
118
+ if (opts.contiguous) {
119
+ const visited = new Uint8Array(width * height);
120
+ const stack = [startX, startY];
121
+ while (stack.length > 0) {
122
+ const y = stack.pop();
123
+ const x = stack.pop();
124
+ if (x < 0 || x >= width || y < 0 || y >= height) {
125
+ continue;
126
+ }
127
+ const pixelIdx = y * width + x;
128
+ if (visited[pixelIdx]) {
129
+ continue;
130
+ }
131
+ visited[pixelIdx] = 1;
132
+ const colorIdx = pixelIdx * 4;
133
+ if (!isColorSimilar(colorIdx)) {
134
+ continue;
135
+ }
136
+ mask.data[pixelIdx] = opts.antiAlias ? getSimilarity(colorIdx) : 255;
137
+ stack.push(x - 1, y);
138
+ stack.push(x + 1, y);
139
+ stack.push(x, y - 1);
140
+ stack.push(x, y + 1);
160
141
  }
161
- // Apply feather if requested
162
- if (opts.feather > 0) {
163
- return featherMask(mask, opts.feather);
142
+ } else {
143
+ for (let i = 0; i < width * height; i++) {
144
+ const colorIdx = i * 4;
145
+ if (isColorSimilar(colorIdx)) {
146
+ mask.data[i] = opts.antiAlias ? getSimilarity(colorIdx) : 255;
147
+ }
164
148
  }
165
- return mask;
149
+ }
150
+ if (opts.feather > 0) {
151
+ return featherMask(mask, opts.feather);
152
+ }
153
+ return mask;
166
154
  }
167
155
  const DEFAULT_LASSO_OPTIONS = {
168
- feather: 0,
169
- antiAlias: true,
156
+ feather: 0,
157
+ antiAlias: true
170
158
  };
171
159
  function lassoSelect(width, height, points, options = {}) {
172
- const opts = { ...DEFAULT_LASSO_OPTIONS, ...options };
173
- const mask = createEmptyMask(width, height);
174
- if (points.length < 3) {
175
- return mask;
176
- }
177
- // Use scanline fill algorithm
178
- const minY = Math.max(0, Math.floor(Math.min(...points.map((p) => p.y))));
179
- const maxY = Math.min(height - 1, Math.ceil(Math.max(...points.map((p) => p.y))));
180
- for (let y = minY; y <= maxY; y++) {
181
- // Find intersections with polygon edges
182
- const intersections = [];
183
- for (let i = 0; i < points.length; i++) {
184
- const p1 = points[i];
185
- const p2 = points[(i + 1) % points.length];
186
- // Check if edge crosses this scanline
187
- if ((p1.y <= y && p2.y > y) || (p2.y <= y && p1.y > y)) {
188
- // Calculate x intersection
189
- const x = p1.x + ((y - p1.y) / (p2.y - p1.y)) * (p2.x - p1.x);
190
- intersections.push(x);
191
- }
192
- }
193
- // Sort intersections
194
- intersections.sort((a, b) => a - b);
195
- // Fill between pairs of intersections
196
- for (let i = 0; i < intersections.length - 1; i += 2) {
197
- const x1 = Math.max(0, Math.floor(intersections[i]));
198
- const x2 = Math.min(width - 1, Math.ceil(intersections[i + 1]));
199
- for (let x = x1; x <= x2; x++) {
200
- mask.data[y * width + x] = 255;
201
- }
202
- }
160
+ const opts = { ...DEFAULT_LASSO_OPTIONS, ...options };
161
+ const mask = createEmptyMask(width, height);
162
+ if (points.length < 3) {
163
+ return mask;
164
+ }
165
+ const minY = Math.max(0, Math.floor(Math.min(...points.map((p) => p.y))));
166
+ const maxY = Math.min(height - 1, Math.ceil(Math.max(...points.map((p) => p.y))));
167
+ for (let y = minY; y <= maxY; y++) {
168
+ const intersections = [];
169
+ for (let i = 0; i < points.length; i++) {
170
+ const p1 = points[i];
171
+ const p2 = points[(i + 1) % points.length];
172
+ if (p1.y <= y && p2.y > y || p2.y <= y && p1.y > y) {
173
+ const x = p1.x + (y - p1.y) / (p2.y - p1.y) * (p2.x - p1.x);
174
+ intersections.push(x);
175
+ }
203
176
  }
204
- // Apply feather if requested
205
- if (opts.feather > 0) {
206
- return featherMask(mask, opts.feather);
177
+ intersections.sort((a, b) => a - b);
178
+ for (let i = 0; i < intersections.length - 1; i += 2) {
179
+ const x1 = Math.max(0, Math.floor(intersections[i]));
180
+ const x2 = Math.min(width - 1, Math.ceil(intersections[i + 1]));
181
+ for (let x = x1; x <= x2; x++) {
182
+ mask.data[y * width + x] = 255;
183
+ }
207
184
  }
208
- return mask;
185
+ }
186
+ if (opts.feather > 0) {
187
+ return featherMask(mask, opts.feather);
188
+ }
189
+ return mask;
209
190
  }
210
- /** Magnetic lasso - snaps to edges in image */
211
191
  function magneticLassoPoint(imageData, x, y, searchRadius = 10) {
212
- const { width, height, data } = imageData;
213
- // Find the point with highest edge strength within radius
214
- let bestX = x;
215
- let bestY = y;
216
- let bestEdge = 0;
217
- for (let dy = -searchRadius; dy <= searchRadius; dy++) {
218
- for (let dx = -searchRadius; dx <= searchRadius; dx++) {
219
- const nx = Math.round(x + dx);
220
- const ny = Math.round(y + dy);
221
- if (nx < 1 || nx >= width - 1 || ny < 1 || ny >= height - 1) {
222
- continue;
223
- }
224
- // Calculate edge strength using Sobel operator
225
- const edge = getEdgeStrength(data, width, nx, ny);
226
- // Weight by distance from cursor
227
- const dist = Math.sqrt(dx * dx + dy * dy);
228
- const weightedEdge = edge / (1 + dist * 0.2);
229
- if (weightedEdge > bestEdge) {
230
- bestEdge = weightedEdge;
231
- bestX = nx;
232
- bestY = ny;
233
- }
234
- }
192
+ const { width, height, data } = imageData;
193
+ let bestX = x;
194
+ let bestY = y;
195
+ let bestEdge = 0;
196
+ for (let dy = -searchRadius; dy <= searchRadius; dy++) {
197
+ for (let dx = -searchRadius; dx <= searchRadius; dx++) {
198
+ const nx = Math.round(x + dx);
199
+ const ny = Math.round(y + dy);
200
+ if (nx < 1 || nx >= width - 1 || ny < 1 || ny >= height - 1) {
201
+ continue;
202
+ }
203
+ const edge = getEdgeStrength(data, width, nx, ny);
204
+ const dist = Math.sqrt(dx * dx + dy * dy);
205
+ const weightedEdge = edge / (1 + dist * 0.2);
206
+ if (weightedEdge > bestEdge) {
207
+ bestEdge = weightedEdge;
208
+ bestX = nx;
209
+ bestY = ny;
210
+ }
235
211
  }
236
- return { x: bestX, y: bestY };
212
+ }
213
+ return { x: bestX, y: bestY };
237
214
  }
238
215
  function getEdgeStrength(data, width, x, y) {
239
- // Sobel operator
240
- const getGray = (px, py) => {
241
- const i = (py * width + px) * 4;
242
- return (data[i] + data[i + 1] + data[i + 2]) / 3;
243
- };
244
- const gx = -getGray(x - 1, y - 1) -
245
- 2 * getGray(x - 1, y) -
246
- getGray(x - 1, y + 1) +
247
- getGray(x + 1, y - 1) +
248
- 2 * getGray(x + 1, y) +
249
- getGray(x + 1, y + 1);
250
- const gy = -getGray(x - 1, y - 1) -
251
- 2 * getGray(x, y - 1) -
252
- getGray(x + 1, y - 1) +
253
- getGray(x - 1, y + 1) +
254
- 2 * getGray(x, y + 1) +
255
- getGray(x + 1, y + 1);
256
- return Math.sqrt(gx * gx + gy * gy);
216
+ const getGray = (px, py) => {
217
+ const i = (py * width + px) * 4;
218
+ return (data[i] + data[i + 1] + data[i + 2]) / 3;
219
+ };
220
+ const gx = -getGray(x - 1, y - 1) - 2 * getGray(x - 1, y) - getGray(x - 1, y + 1) + getGray(x + 1, y - 1) + 2 * getGray(x + 1, y) + getGray(x + 1, y + 1);
221
+ const gy = -getGray(x - 1, y - 1) - 2 * getGray(x, y - 1) - getGray(x + 1, y - 1) + getGray(x - 1, y + 1) + 2 * getGray(x, y + 1) + getGray(x + 1, y + 1);
222
+ return Math.sqrt(gx * gx + gy * gy);
257
223
  }
258
224
  function selectColorRange(imageData, targetColor, options = {}) {
259
- const opts = { fuzziness: 40, feather: 0, ...options };
260
- const { width, height, data } = imageData;
261
- const mask = createEmptyMask(width, height);
262
- for (let i = 0; i < width * height; i++) {
263
- const idx = i * 4;
264
- const r = data[idx];
265
- const g = data[idx + 1];
266
- const b = data[idx + 2];
267
- // Calculate color difference in LAB-like space for perceptual accuracy
268
- const diff = Math.sqrt(Math.pow(r - targetColor.r, 2) +
269
- Math.pow(g - targetColor.g, 2) +
270
- Math.pow(b - targetColor.b, 2));
271
- const maxDiff = opts.fuzziness * 2;
272
- if (diff < maxDiff) {
273
- mask.data[i] = Math.round(255 * (1 - diff / maxDiff));
274
- }
225
+ const opts = { fuzziness: 40, feather: 0, ...options };
226
+ const { width, height, data } = imageData;
227
+ const mask = createEmptyMask(width, height);
228
+ for (let i = 0; i < width * height; i++) {
229
+ const idx = i * 4;
230
+ const r = data[idx];
231
+ const g = data[idx + 1];
232
+ const b = data[idx + 2];
233
+ const diff = Math.sqrt(
234
+ Math.pow(r - targetColor.r, 2) + Math.pow(g - targetColor.g, 2) + Math.pow(b - targetColor.b, 2)
235
+ );
236
+ const maxDiff = opts.fuzziness * 2;
237
+ if (diff < maxDiff) {
238
+ mask.data[i] = Math.round(255 * (1 - diff / maxDiff));
275
239
  }
276
- if (opts.feather > 0) {
277
- return featherMask(mask, opts.feather);
278
- }
279
- return mask;
240
+ }
241
+ if (opts.feather > 0) {
242
+ return featherMask(mask, opts.feather);
243
+ }
244
+ return mask;
280
245
  }
281
246
  const DEFAULT_HEALING_OPTIONS = {
282
- radius: 20,
283
- hardness: 0.5,
284
- opacity: 1,
285
- mode: 'heal',
247
+ radius: 20,
248
+ hardness: 0.5,
249
+ opacity: 1,
250
+ mode: "heal"
286
251
  };
287
- /** Apply healing brush at destination using source area */
288
252
  function applyHealingBrush(imageData, srcX, srcY, dstX, dstY, options = {}) {
289
- const opts = { ...DEFAULT_HEALING_OPTIONS, ...options };
290
- const { width, height, data } = imageData;
291
- const result = new ImageData(new Uint8ClampedArray(data), width, height);
292
- const r = opts.radius;
293
- const r2 = r * r;
294
- // Calculate average color at destination for healing mode
295
- let avgR = 0, avgG = 0, avgB = 0, count = 0;
296
- if (opts.mode === 'heal') {
297
- for (let dy = -r; dy <= r; dy++) {
298
- for (let dx = -r; dx <= r; dx++) {
299
- if (dx * dx + dy * dy > r2) {
300
- continue;
301
- }
302
- const px = dstX + dx;
303
- const py = dstY + dy;
304
- if (px < 0 || px >= width || py < 0 || py >= height) {
305
- continue;
306
- }
307
- const idx = (py * width + px) * 4;
308
- avgR += data[idx];
309
- avgG += data[idx + 1];
310
- avgB += data[idx + 2];
311
- count++;
312
- }
253
+ const opts = { ...DEFAULT_HEALING_OPTIONS, ...options };
254
+ const { width, height, data } = imageData;
255
+ const result = new ImageData(new Uint8ClampedArray(data), width, height);
256
+ const r = opts.radius;
257
+ const r2 = r * r;
258
+ let avgR = 0, avgG = 0, avgB = 0, count = 0;
259
+ if (opts.mode === "heal") {
260
+ for (let dy = -r; dy <= r; dy++) {
261
+ for (let dx = -r; dx <= r; dx++) {
262
+ if (dx * dx + dy * dy > r2) {
263
+ continue;
313
264
  }
314
- if (count > 0) {
315
- avgR /= count;
316
- avgG /= count;
317
- avgB /= count;
265
+ const px = dstX + dx;
266
+ const py = dstY + dy;
267
+ if (px < 0 || px >= width || py < 0 || py >= height) {
268
+ continue;
318
269
  }
270
+ const idx = (py * width + px) * 4;
271
+ avgR += data[idx];
272
+ avgG += data[idx + 1];
273
+ avgB += data[idx + 2];
274
+ count++;
275
+ }
319
276
  }
320
- // Copy/heal pixels
321
- for (let dy = -r; dy <= r; dy++) {
322
- for (let dx = -r; dx <= r; dx++) {
323
- const distSq = dx * dx + dy * dy;
324
- if (distSq > r2) {
325
- continue;
326
- }
327
- const spx = srcX + dx;
328
- const spy = srcY + dy;
329
- const dpx = dstX + dx;
330
- const dpy = dstY + dy;
331
- if (spx < 0 || spx >= width || spy < 0 || spy >= height) {
332
- continue;
333
- }
334
- if (dpx < 0 || dpx >= width || dpy < 0 || dpy >= height) {
335
- continue;
336
- }
337
- const srcIdx = (spy * width + spx) * 4;
338
- const dstIdx = (dpy * width + dpx) * 4;
339
- // Calculate brush falloff
340
- const dist = Math.sqrt(distSq);
341
- const falloff = Math.min(1, Math.max(0, (r - dist) / (r * (1 - opts.hardness + 0.01))));
342
- const alpha = opts.opacity * falloff;
343
- if (opts.mode === 'clone') {
344
- // Simple clone
345
- result.data[dstIdx] = lerp(data[dstIdx], data[srcIdx], alpha);
346
- result.data[dstIdx + 1] = lerp(data[dstIdx + 1], data[srcIdx + 1], alpha);
347
- result.data[dstIdx + 2] = lerp(data[dstIdx + 2], data[srcIdx + 2], alpha);
277
+ if (count > 0) {
278
+ avgR /= count;
279
+ avgG /= count;
280
+ avgB /= count;
281
+ }
282
+ }
283
+ for (let dy = -r; dy <= r; dy++) {
284
+ for (let dx = -r; dx <= r; dx++) {
285
+ const distSq = dx * dx + dy * dy;
286
+ if (distSq > r2) {
287
+ continue;
288
+ }
289
+ const spx = srcX + dx;
290
+ const spy = srcY + dy;
291
+ const dpx = dstX + dx;
292
+ const dpy = dstY + dy;
293
+ if (spx < 0 || spx >= width || spy < 0 || spy >= height) {
294
+ continue;
295
+ }
296
+ if (dpx < 0 || dpx >= width || dpy < 0 || dpy >= height) {
297
+ continue;
298
+ }
299
+ const srcIdx = (spy * width + spx) * 4;
300
+ const dstIdx = (dpy * width + dpx) * 4;
301
+ const dist = Math.sqrt(distSq);
302
+ const falloff = Math.min(1, Math.max(0, (r - dist) / (r * (1 - opts.hardness + 0.01))));
303
+ const alpha = opts.opacity * falloff;
304
+ if (opts.mode === "clone") {
305
+ result.data[dstIdx] = lerp(data[dstIdx], data[srcIdx], alpha);
306
+ result.data[dstIdx + 1] = lerp(data[dstIdx + 1], data[srcIdx + 1], alpha);
307
+ result.data[dstIdx + 2] = lerp(data[dstIdx + 2], data[srcIdx + 2], alpha);
308
+ } else {
309
+ const srcR = data[srcIdx];
310
+ const srcG = data[srcIdx + 1];
311
+ const srcB = data[srcIdx + 2];
312
+ const srcAvgR = avgR;
313
+ const srcAvgG = avgG;
314
+ const srcAvgB = avgB;
315
+ let sAvgR = 0, sAvgG = 0, sAvgB = 0, sCount = 0;
316
+ for (let sy = -r; sy <= r; sy++) {
317
+ for (let sx = -r; sx <= r; sx++) {
318
+ if (sx * sx + sy * sy > r2) {
319
+ continue;
348
320
  }
349
- else {
350
- // Healing mode: match destination luminance
351
- const srcR = data[srcIdx];
352
- const srcG = data[srcIdx + 1];
353
- const srcB = data[srcIdx + 2];
354
- // Adjust source to match destination average
355
- const srcAvgR = avgR;
356
- const srcAvgG = avgG;
357
- const srcAvgB = avgB;
358
- // Calculate source average in the brush area
359
- let sAvgR = 0, sAvgG = 0, sAvgB = 0, sCount = 0;
360
- for (let sy = -r; sy <= r; sy++) {
361
- for (let sx = -r; sx <= r; sx++) {
362
- if (sx * sx + sy * sy > r2) {
363
- continue;
364
- }
365
- const px = srcX + sx;
366
- const py = srcY + sy;
367
- if (px < 0 || px >= width || py < 0 || py >= height) {
368
- continue;
369
- }
370
- const idx = (py * width + px) * 4;
371
- sAvgR += data[idx];
372
- sAvgG += data[idx + 1];
373
- sAvgB += data[idx + 2];
374
- sCount++;
375
- }
376
- }
377
- if (sCount > 0) {
378
- sAvgR /= sCount;
379
- sAvgG /= sCount;
380
- sAvgB /= sCount;
381
- }
382
- // Adjust source color to match destination
383
- const adjustedR = srcR + (srcAvgR - sAvgR);
384
- const adjustedG = srcG + (srcAvgG - sAvgG);
385
- const adjustedB = srcB + (srcAvgB - sAvgB);
386
- result.data[dstIdx] = lerp(data[dstIdx], clamp(adjustedR, 0, 255), alpha);
387
- result.data[dstIdx + 1] = lerp(data[dstIdx + 1], clamp(adjustedG, 0, 255), alpha);
388
- result.data[dstIdx + 2] = lerp(data[dstIdx + 2], clamp(adjustedB, 0, 255), alpha);
321
+ const px = srcX + sx;
322
+ const py = srcY + sy;
323
+ if (px < 0 || px >= width || py < 0 || py >= height) {
324
+ continue;
389
325
  }
326
+ const idx = (py * width + px) * 4;
327
+ sAvgR += data[idx];
328
+ sAvgG += data[idx + 1];
329
+ sAvgB += data[idx + 2];
330
+ sCount++;
331
+ }
390
332
  }
333
+ if (sCount > 0) {
334
+ sAvgR /= sCount;
335
+ sAvgG /= sCount;
336
+ sAvgB /= sCount;
337
+ }
338
+ const adjustedR = srcR + (srcAvgR - sAvgR);
339
+ const adjustedG = srcG + (srcAvgG - sAvgG);
340
+ const adjustedB = srcB + (srcAvgB - sAvgB);
341
+ result.data[dstIdx] = lerp(data[dstIdx], clamp(adjustedR, 0, 255), alpha);
342
+ result.data[dstIdx + 1] = lerp(data[dstIdx + 1], clamp(adjustedG, 0, 255), alpha);
343
+ result.data[dstIdx + 2] = lerp(data[dstIdx + 2], clamp(adjustedB, 0, 255), alpha);
344
+ }
391
345
  }
392
- return result;
346
+ }
347
+ return result;
393
348
  }
394
349
  function lerp(a, b, t) {
395
- return a + (b - a) * t;
350
+ return a + (b - a) * t;
396
351
  }
397
352
  function clamp(val, min, max) {
398
- return Math.max(min, Math.min(max, val));
353
+ return Math.max(min, Math.min(max, val));
399
354
  }
400
- /* ────────────────────────────────────────────────────────────────
401
- CONTENT-AWARE FILL (simplified)
402
- ──────────────────────────────────────────────────────────────── */
403
- /** Simple content-aware fill using surrounding pixels */
404
355
  function contentAwareFill(imageData, mask) {
405
- const { width, height, data } = imageData;
406
- const result = new ImageData(new Uint8ClampedArray(data), width, height);
407
- // Find selected pixels
408
- const selected = [];
409
- for (let i = 0; i < mask.data.length; i++) {
410
- if (mask.data[i] > 128) {
411
- selected.push(i);
412
- }
356
+ const { width, height, data } = imageData;
357
+ const result = new ImageData(new Uint8ClampedArray(data), width, height);
358
+ const selected = [];
359
+ for (let i = 0; i < mask.data.length; i++) {
360
+ if (mask.data[i] > 128) {
361
+ selected.push(i);
413
362
  }
414
- // Sort by distance from edge (process edge pixels first)
415
- selected.sort((a, b) => {
416
- const ax = a % width, ay = Math.floor(a / width);
417
- const bx = b % width, by = Math.floor(b / width);
418
- return distFromEdge(ax, ay, mask) - distFromEdge(bx, by, mask);
419
- });
420
- // Fill each pixel using nearby unselected pixels
421
- for (const pixelIdx of selected) {
422
- const x = pixelIdx % width;
423
- const y = Math.floor(pixelIdx / width);
424
- // Find nearby unselected pixels
425
- const sample = findBestMatch(result, mask, x, y, 15);
426
- if (sample) {
427
- const dstIdx = pixelIdx * 4;
428
- const srcIdx = (sample.y * width + sample.x) * 4;
429
- result.data[dstIdx] = result.data[srcIdx];
430
- result.data[dstIdx + 1] = result.data[srcIdx + 1];
431
- result.data[dstIdx + 2] = result.data[srcIdx + 2];
432
- }
363
+ }
364
+ selected.sort((a, b) => {
365
+ const ax = a % width, ay = Math.floor(a / width);
366
+ const bx = b % width, by = Math.floor(b / width);
367
+ return distFromEdge(ax, ay, mask) - distFromEdge(bx, by, mask);
368
+ });
369
+ for (const pixelIdx of selected) {
370
+ const x = pixelIdx % width;
371
+ const y = Math.floor(pixelIdx / width);
372
+ const sample = findBestMatch(result, mask, x, y, 15);
373
+ if (sample) {
374
+ const dstIdx = pixelIdx * 4;
375
+ const srcIdx = (sample.y * width + sample.x) * 4;
376
+ result.data[dstIdx] = result.data[srcIdx];
377
+ result.data[dstIdx + 1] = result.data[srcIdx + 1];
378
+ result.data[dstIdx + 2] = result.data[srcIdx + 2];
433
379
  }
434
- return result;
380
+ }
381
+ return result;
435
382
  }
436
383
  function distFromEdge(x, y, mask) {
437
- const { width, height } = mask;
438
- const minDist = Infinity;
439
- for (let r = 1; r < 50; r++) {
440
- for (let dy = -r; dy <= r; dy++) {
441
- for (let dx = -r; dx <= r; dx++) {
442
- if (Math.abs(dx) !== r && Math.abs(dy) !== r) {
443
- continue;
444
- }
445
- const nx = x + dx, ny = y + dy;
446
- if (nx < 0 || nx >= width || ny < 0 || ny >= height) {
447
- continue;
448
- }
449
- if (mask.data[ny * width + nx] < 128) {
450
- return Math.sqrt(dx * dx + dy * dy);
451
- }
452
- }
384
+ const { width, height } = mask;
385
+ const minDist = Infinity;
386
+ for (let r = 1; r < 50; r++) {
387
+ for (let dy = -r; dy <= r; dy++) {
388
+ for (let dx = -r; dx <= r; dx++) {
389
+ if (Math.abs(dx) !== r && Math.abs(dy) !== r) {
390
+ continue;
453
391
  }
392
+ const nx = x + dx, ny = y + dy;
393
+ if (nx < 0 || nx >= width || ny < 0 || ny >= height) {
394
+ continue;
395
+ }
396
+ if (mask.data[ny * width + nx] < 128) {
397
+ return Math.sqrt(dx * dx + dy * dy);
398
+ }
399
+ }
454
400
  }
455
- return minDist;
401
+ }
402
+ return minDist;
456
403
  }
457
404
  function findBestMatch(imageData, mask, x, y, radius) {
458
- const { width, height, data } = imageData;
459
- const candidates = [];
460
- // Collect unselected candidate pixels
461
- for (let dy = -radius; dy <= radius; dy++) {
462
- for (let dx = -radius; dx <= radius; dx++) {
463
- const nx = x + dx, ny = y + dy;
464
- if (nx < 0 || nx >= width || ny < 0 || ny >= height) {
465
- continue;
466
- }
467
- if (mask.data[ny * width + nx] < 128) {
468
- candidates.push({ x: nx, y: ny });
469
- }
470
- }
471
- }
472
- if (candidates.length === 0) {
473
- return null;
405
+ const { width, height, data } = imageData;
406
+ const candidates = [];
407
+ for (let dy = -radius; dy <= radius; dy++) {
408
+ for (let dx = -radius; dx <= radius; dx++) {
409
+ const nx = x + dx, ny = y + dy;
410
+ if (nx < 0 || nx >= width || ny < 0 || ny >= height) {
411
+ continue;
412
+ }
413
+ if (mask.data[ny * width + nx] < 128) {
414
+ candidates.push({ x: nx, y: ny });
415
+ }
474
416
  }
475
- // Find candidate with most similar surroundings
476
- let best = null;
477
- let bestScore = Infinity;
478
- const patchSize = 3;
479
- for (const cand of candidates) {
480
- let score = 0;
481
- let count = 0;
482
- for (let py = -patchSize; py <= patchSize; py++) {
483
- for (let px = -patchSize; px <= patchSize; px++) {
484
- const srcX = x + px, srcY = y + py;
485
- const candX = cand.x + px, candY = cand.y + py;
486
- if (srcX < 0 || srcX >= width || srcY < 0 || srcY >= height) {
487
- continue;
488
- }
489
- if (candX < 0 || candX >= width || candY < 0 || candY >= height) {
490
- continue;
491
- }
492
- // Only compare unselected pixels
493
- if (mask.data[srcY * width + srcX] > 128) {
494
- continue;
495
- }
496
- const srcIdx = (srcY * width + srcX) * 4;
497
- const candIdx = (candY * width + candX) * 4;
498
- const dr = data[srcIdx] - data[candIdx];
499
- const dg = data[srcIdx + 1] - data[candIdx + 1];
500
- const db = data[srcIdx + 2] - data[candIdx + 2];
501
- score += dr * dr + dg * dg + db * db;
502
- count++;
503
- }
417
+ }
418
+ if (candidates.length === 0) {
419
+ return null;
420
+ }
421
+ let best = null;
422
+ let bestScore = Infinity;
423
+ const patchSize = 3;
424
+ for (const cand of candidates) {
425
+ let score = 0;
426
+ let count = 0;
427
+ for (let py = -patchSize; py <= patchSize; py++) {
428
+ for (let px = -patchSize; px <= patchSize; px++) {
429
+ const srcX = x + px, srcY = y + py;
430
+ const candX = cand.x + px, candY = cand.y + py;
431
+ if (srcX < 0 || srcX >= width || srcY < 0 || srcY >= height) {
432
+ continue;
504
433
  }
505
- if (count > 0 && score / count < bestScore) {
506
- bestScore = score / count;
507
- best = cand;
434
+ if (candX < 0 || candX >= width || candY < 0 || candY >= height) {
435
+ continue;
508
436
  }
437
+ if (mask.data[srcY * width + srcX] > 128) {
438
+ continue;
439
+ }
440
+ const srcIdx = (srcY * width + srcX) * 4;
441
+ const candIdx = (candY * width + candX) * 4;
442
+ const dr = data[srcIdx] - data[candIdx];
443
+ const dg = data[srcIdx + 1] - data[candIdx + 1];
444
+ const db = data[srcIdx + 2] - data[candIdx + 2];
445
+ score += dr * dr + dg * dg + db * db;
446
+ count++;
447
+ }
448
+ }
449
+ if (count > 0 && score / count < bestScore) {
450
+ bestScore = score / count;
451
+ best = cand;
509
452
  }
510
- return best;
453
+ }
454
+ return best;
511
455
  }
512
- /* ────────────────────────────────────────────────────────────────
513
- SELECTION RENDERING
514
- ──────────────────────────────────────────────────────────────── */
515
- /** Render marching ants selection outline */
516
456
  function renderSelectionOutline(ctx, mask, offset = 0) {
517
- const { width, height, data } = mask;
518
- // Find edge pixels
519
- ctx.strokeStyle = uiCore.resolveCssColor('var(--nice-bg, #fff)', ctx.canvas);
520
- ctx.lineWidth = 1;
521
- ctx.setLineDash([4, 4]);
522
- ctx.lineDashOffset = offset;
523
- ctx.beginPath();
524
- for (let y = 0; y < height; y++) {
525
- for (let x = 0; x < width; x++) {
526
- const idx = y * width + x;
527
- if (data[idx] < 128) {
528
- continue;
529
- }
530
- // Check if this is an edge pixel
531
- const isEdge = x === 0 ||
532
- data[idx - 1] < 128 ||
533
- x === width - 1 ||
534
- data[idx + 1] < 128 ||
535
- y === 0 ||
536
- data[idx - width] < 128 ||
537
- y === height - 1 ||
538
- data[idx + width] < 128;
539
- if (isEdge) {
540
- // Draw pixel outline
541
- if (x === 0 || data[idx - 1] < 128) {
542
- ctx.moveTo(x, y);
543
- ctx.lineTo(x, y + 1);
544
- }
545
- if (x === width - 1 || data[idx + 1] < 128) {
546
- ctx.moveTo(x + 1, y);
547
- ctx.lineTo(x + 1, y + 1);
548
- }
549
- if (y === 0 || data[idx - width] < 128) {
550
- ctx.moveTo(x, y);
551
- ctx.lineTo(x + 1, y);
552
- }
553
- if (y === height - 1 || data[idx + width] < 128) {
554
- ctx.moveTo(x, y + 1);
555
- ctx.lineTo(x + 1, y + 1);
556
- }
557
- }
457
+ const { width, height, data } = mask;
458
+ ctx.strokeStyle = uiCore.resolveCssColor("var(--nice-bg, #fff)", ctx.canvas);
459
+ ctx.lineWidth = 1;
460
+ ctx.setLineDash([4, 4]);
461
+ ctx.lineDashOffset = offset;
462
+ ctx.beginPath();
463
+ for (let y = 0; y < height; y++) {
464
+ for (let x = 0; x < width; x++) {
465
+ const idx = y * width + x;
466
+ if (data[idx] < 128) {
467
+ continue;
468
+ }
469
+ const isEdge = x === 0 || data[idx - 1] < 128 || x === width - 1 || data[idx + 1] < 128 || y === 0 || data[idx - width] < 128 || y === height - 1 || data[idx + width] < 128;
470
+ if (isEdge) {
471
+ if (x === 0 || data[idx - 1] < 128) {
472
+ ctx.moveTo(x, y);
473
+ ctx.lineTo(x, y + 1);
474
+ }
475
+ if (x === width - 1 || data[idx + 1] < 128) {
476
+ ctx.moveTo(x + 1, y);
477
+ ctx.lineTo(x + 1, y + 1);
478
+ }
479
+ if (y === 0 || data[idx - width] < 128) {
480
+ ctx.moveTo(x, y);
481
+ ctx.lineTo(x + 1, y);
482
+ }
483
+ if (y === height - 1 || data[idx + width] < 128) {
484
+ ctx.moveTo(x, y + 1);
485
+ ctx.lineTo(x + 1, y + 1);
558
486
  }
487
+ }
559
488
  }
560
- ctx.stroke();
561
- // Draw again with black for contrast
562
- ctx.strokeStyle = uiCore.resolveCssColor('var(--nice-text, #000000)', ctx.canvas);
563
- ctx.lineDashOffset = offset + 4;
564
- ctx.stroke();
565
- ctx.setLineDash([]);
489
+ }
490
+ ctx.stroke();
491
+ ctx.strokeStyle = uiCore.resolveCssColor("var(--nice-text, #000000)", ctx.canvas);
492
+ ctx.lineDashOffset = offset + 4;
493
+ ctx.stroke();
494
+ ctx.setLineDash([]);
566
495
  }
567
-
568
496
  exports.applyHealingBrush = applyHealingBrush;
569
497
  exports.combineMasks = combineMasks;
570
498
  exports.contentAwareFill = contentAwareFill;