@nkzw/palette-swap 4.0.0 → 4.0.1

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/README.md CHANGED
@@ -71,6 +71,8 @@ for (const canvas of paletteSwap(image, variants).values()) {
71
71
 
72
72
  Modern bundlers select the `browser` export automatically. The browser fallback also supports bundlers using the `browser` field. Each entry point has matching TypeScript declarations. With TypeScript's `bundler` module resolution, set `customConditions: ["browser"]` to select the browser declarations explicitly.
73
73
 
74
+ The browser build also accepts `ImageData`. Read pixels once with `getImageData()` and reuse them across calls when generating variants in batches.
75
+
74
76
  ### Migrating from node-canvas
75
77
 
76
78
  Browser callers keep the same API. Node.js callers should create and load images using `@napi-rs/canvas` instead of `canvas`, and use `canvas.toBuffer('image/png')` to encode results. Returned Node canvases are `@napi-rs/canvas` objects; native objects from the old `canvas` package are not interchangeable.
package/lib/browser.d.ts CHANGED
@@ -1,9 +1,7 @@
1
- import { n as HSL, r as VariantName, t as HEX } from "./palette-swap-BQyCssvL.js";
2
- //#region src/browser.d.ts
1
+ import { n as HSL, r as VariantName, t as HEX } from "./palette-swap-CpUFcX2w.js";
3
2
  export type Image = HTMLImageElement | HTMLCanvasElement | ImageBitmap | OffscreenCanvas;
4
- declare const _default: (image: Image, inputVariants: ReadonlyMap<VariantName, ReadonlyMap<HEX, HEX> | number>, staticColors?: Set<HEX> | null, images?: ReadonlyMap<VariantName, Image> | null | undefined, options?: {
3
+ declare const _default: (image: ImageData | Image, inputVariants: ReadonlyMap<VariantName, ReadonlyMap<HEX, HEX> | number>, staticColors?: Set<HEX> | null, images?: ReadonlyMap<VariantName, ImageData | Image> | null | undefined, options?: {
5
4
  imageName?: string;
6
5
  ignoreMissing?: boolean;
7
6
  } | undefined) => ReadonlyMap<VariantName, HTMLCanvasElement>;
8
- //#endregion
9
7
  export { type HEX, type HSL, type VariantName, _default as default };
package/lib/browser.js CHANGED
@@ -1,14 +1,22 @@
1
- import { t as createPaletteSwap } from "./palette-swap-D-ZznABu.js";
2
- //#region src/browser.ts
1
+ import { t as createPaletteSwap } from "./palette-swap-DnkKCKpC.js";
2
+ // src/browser.ts
3
3
  var browser_default = createPaletteSwap({
4
4
  readImage(image, width = image.width, height = image.height) {
5
+ const isImageData = image instanceof ImageData;
6
+ if (isImageData && image.width === width && image.height === height && image.colorSpace === "srgb" && image.data.BYTES_PER_ELEMENT === 1) return image;
5
7
  const canvas = document.createElement("canvas");
6
8
  canvas.width = image.width;
7
9
  canvas.height = image.height;
8
- const context = canvas.getContext("2d");
9
- if (!context) throw new Error("paletteSwap: Could not create a canvas context.");
10
- context.drawImage(image, 0, 0);
11
- return context.getImageData(0, 0, width, height);
10
+ try {
11
+ const context = canvas.getContext("2d");
12
+ if (!context) throw new Error("paletteSwap: Could not create a canvas context.");
13
+ if (isImageData) context.putImageData(image, 0, 0);
14
+ else context.drawImage(image, 0, 0);
15
+ return context.getImageData(0, 0, width, height);
16
+ } finally {
17
+ canvas.width = 0;
18
+ canvas.height = 0;
19
+ }
12
20
  },
13
21
  writeImage({ data, height, width }) {
14
22
  const canvas = document.createElement("canvas");
@@ -20,5 +28,4 @@ var browser_default = createPaletteSwap({
20
28
  return canvas;
21
29
  }
22
30
  });
23
- //#endregion
24
31
  export { browser_default as default };
package/lib/node.d.ts CHANGED
@@ -1,10 +1,8 @@
1
- import { n as HSL, r as VariantName, t as HEX } from "./palette-swap-BQyCssvL.js";
1
+ import { n as HSL, r as VariantName, t as HEX } from "./palette-swap-CpUFcX2w.js";
2
2
  import { Canvas, Image as Image$1 } from "@napi-rs/canvas";
3
- //#region src/node.d.ts
4
3
  export type Image = Image$1 | Canvas;
5
4
  declare const _default: (image: Image, inputVariants: ReadonlyMap<VariantName, ReadonlyMap<HEX, HEX> | number>, staticColors?: Set<HEX> | null, images?: ReadonlyMap<VariantName, Image> | null | undefined, options?: {
6
5
  imageName?: string;
7
6
  ignoreMissing?: boolean;
8
7
  } | undefined) => ReadonlyMap<VariantName, Canvas>;
9
- //#endregion
10
8
  export { type HEX, type HSL, type VariantName, _default as default };
package/lib/node.js CHANGED
@@ -1,6 +1,6 @@
1
- import { t as createPaletteSwap } from "./palette-swap-D-ZznABu.js";
1
+ import { t as createPaletteSwap } from "./palette-swap-DnkKCKpC.js";
2
2
  import { ImageData, createCanvas } from "@napi-rs/canvas";
3
- //#region src/node.ts
3
+ // src/node.ts
4
4
  var node_default = createPaletteSwap({
5
5
  readImage(image, width = image.width, height = image.height) {
6
6
  const context = createCanvas(image.width, image.height).getContext("2d");
@@ -13,5 +13,4 @@ var node_default = createPaletteSwap({
13
13
  return canvas;
14
14
  }
15
15
  });
16
- //#endregion
17
16
  export { node_default as default };
@@ -1,6 +1,4 @@
1
- //#region src/palette-swap.d.ts
2
1
  type HEX = `#${string}`;
3
2
  type HSL = [hue: number, saturation: number, lightness: number];
4
3
  type VariantName = number | string;
5
- //#endregion
6
4
  export { HSL as n, VariantName as r, HEX as t };
@@ -1,4 +1,4 @@
1
- //#region src/palette-swap.ts
1
+ // src/palette-swap.ts
2
2
  const hexToUint32LE = (hex) => {
3
3
  const n = Number.parseInt(hex.slice(1), 16) & 16777215;
4
4
  const [r, g, b] = int32ToRgb(n);
@@ -195,5 +195,4 @@ function createPaletteSwap({ readImage, writeImage }) {
195
195
  return results;
196
196
  };
197
197
  }
198
- //#endregion
199
198
  export { createPaletteSwap as t };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nkzw/palette-swap",
3
- "version": "4.0.0",
3
+ "version": "4.0.1",
4
4
  "description": "Image palette swapping and color replacement tool based on canvas for Node.js and browsers.",
5
5
  "keywords": [
6
6
  "canvas",
@@ -31,14 +31,21 @@
31
31
  }
32
32
  },
33
33
  "dependencies": {
34
- "@napi-rs/canvas": "^1.0.9"
34
+ "@napi-rs/canvas": "^1.1.0"
35
35
  },
36
36
  "devDependencies": {
37
- "@types/node": "^25.9.7",
38
- "playwright": "^1.63.0",
39
- "tsdown": "^0.23.0",
37
+ "@types/node": "^26.6.6",
38
+ "playwright": "^1.65.0",
39
+ "tsdown": "^0.23.7",
40
40
  "typescript": "^7.0.2",
41
- "vitest": "^5.0.1"
41
+ "vitest": "^5.1.0"
42
+ },
43
+ "devEngines": {
44
+ "packageManager": {
45
+ "name": "pnpm",
46
+ "version": "12.18.0",
47
+ "onFail": "download"
48
+ }
42
49
  },
43
50
  "engines": {
44
51
  "node": ">=22.0.0"
package/.gitignore DELETED
@@ -1,2 +0,0 @@
1
- lib
2
- node_modules
package/.npmignore DELETED
@@ -1,6 +0,0 @@
1
- example
2
- lib/tsconfig.tsbuildinfo
3
- pnpm-lock.yaml
4
- src
5
- tsconfig.json
6
- tsdown.config.ts