pixelkiln 0.141.0 → 0.142.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/dist/cli.js CHANGED
@@ -3536,6 +3536,11 @@ var ParallaxRunSpriteSchema = z.object({
3536
3536
  }
3537
3537
  });
3538
3538
  var RUN_ONLY = ["seed", "step", "gap", "jitter", "width"];
3539
+ var ParallaxTintSchema = z.object({
3540
+ color: HexColorSchema,
3541
+ /** 0 leaves the picture as it is; 1 makes every pixel the colour, snapped. */
3542
+ amount: z.number().min(0).max(1)
3543
+ }).strict();
3539
3544
  var ParallaxLayerSchema = z.object({
3540
3545
  /** Names the layer in exports; defaults to the asset id, the file name, or "sky". */
3541
3546
  name: z.string().regex(/^[A-Za-z][A-Za-z0-9_-]*$/, "letters, digits, - and _, starting with a letter").optional(),
@@ -3579,7 +3584,16 @@ var ParallaxLayerSchema = z.object({
3579
3584
  /** Repeat sideways (the default) so the layer loops. */
3580
3585
  repeat: z.boolean().optional(),
3581
3586
  /** Repeat vertically, for an endless climb. */
3582
- repeatY: z.boolean().optional()
3587
+ repeatY: z.boolean().optional(),
3588
+ /**
3589
+ * Trim the picture to its opaque box, then thin out this fraction of it
3590
+ * from the bottom with a 4x4 ordered dither, so its foot dissolves into
3591
+ * what is behind it. A layer that repeats sideways keeps its width. In a
3592
+ * run, each sprite is trimmed and thinned before it is laid.
3593
+ */
3594
+ foot: z.number().gt(0).max(1).optional(),
3595
+ /** Pull the picture (each sprite of a run) toward a colour and snap it to the set's palette. Applied after `foot`. */
3596
+ tint: ParallaxTintSchema.optional()
3583
3597
  }).strict().superRefine((layer, ctx) => {
3584
3598
  const sources = [layer.asset, layer.file, layer.sky, layer.run].filter((value) => value !== void 0).length;
3585
3599
  if (sources !== 1) {
@@ -3593,6 +3607,14 @@ var ParallaxLayerSchema = z.object({
3593
3607
  if (layer.style !== void 0 && layer.asset === void 0) {
3594
3608
  ctx.addIssue({ code: z.ZodIssueCode.custom, path: ["style"], message: "style only applies to an asset layer" });
3595
3609
  }
3610
+ if (layer.sky !== void 0) {
3611
+ for (const key3 of ["foot", "tint"]) {
3612
+ if (layer[key3] !== void 0) ctx.addIssue({ code: z.ZodIssueCode.custom, path: [key3], message: `${key3} does not apply to a sky layer` });
3613
+ }
3614
+ }
3615
+ if (layer.foot !== void 0 && layer.extend !== void 0) {
3616
+ ctx.addIssue({ code: z.ZodIssueCode.custom, path: ["extend"], message: "extend repeats the bottom row, which foot thins out; use one or the other" });
3617
+ }
3596
3618
  });
3597
3619
  var ParallaxSetSchema = z.object({
3598
3620
  /** The game's view in pixels, [width, height]. */
@@ -3601,6 +3623,12 @@ var ParallaxSetSchema = z.object({
3601
3623
  travel: z.number().int().min(0).max(1e5).optional(),
3602
3624
  /** How far the camera climbs, in pixels. */
3603
3625
  climb: z.number().int().min(0).max(1e5).optional(),
3626
+ /**
3627
+ * What a layer's `tint` snaps to: a style id, for that style's palette, or
3628
+ * a list of colours. Without one, a tint snaps to the colours already in
3629
+ * the picture, so it adds none.
3630
+ */
3631
+ palette: z.union([z.string().min(1), z.array(HexColorSchema).min(2).max(256)]).optional(),
3604
3632
  layers: z.array(ParallaxLayerSchema).min(1).max(32)
3605
3633
  }).strict();
3606
3634
  var ParallaxSetIdSchema = z.string().regex(/^[A-Za-z0-9][A-Za-z0-9_-]{0,63}$/, "a set id is letters, digits, - and _, starting with a letter or digit");
@@ -16620,6 +16648,11 @@ ${issues}`);
16620
16648
  for (const assetId of Object.keys(parsed.data.assets)) visitConnections(assetId);
16621
16649
  for (const [setId, set] of Object.entries(parsed.data.parallax ?? {})) {
16622
16650
  const names = /* @__PURE__ */ new Set();
16651
+ if (typeof set.palette === "string") {
16652
+ const style = parsed.data.styles[set.palette];
16653
+ if (!style) unknownReferences.push(`parallax.${setId}.palette: unknown style "${set.palette}"`);
16654
+ else if (style.palette.length < 2) unknownReferences.push(`parallax.${setId}.palette: style "${set.palette}" has no palette`);
16655
+ }
16623
16656
  set.layers.forEach((layer, index) => {
16624
16657
  const at2 = `parallax.${setId}.layers.${index}`;
16625
16658
  const checkAsset = (where, ref) => {
@@ -35715,11 +35748,15 @@ async function resolveParallaxSet(manifest, manifestDir, lock, setId) {
35715
35748
  const warnings = [];
35716
35749
  const layers = [];
35717
35750
  const taken = /* @__PURE__ */ new Set();
35751
+ const palette = typeof set.palette === "string" ? manifest.styles[set.palette]?.palette : set.palette;
35752
+ if (set.palette !== void 0 && !palette?.length) throw new Error(`parallax ${setId}: style "${set.palette}" has no palette`);
35718
35753
  for (const [index, layer] of set.layers.entries()) {
35719
35754
  let name = layerName(layer, index);
35720
35755
  for (let n = 2; taken.has(name); n++) name = `${layerName(layer, index)}_${n}`;
35721
35756
  taken.add(name);
35722
35757
  const speedY = layer.speedY ?? (climb > 0 ? layer.speed : 1);
35758
+ const repeat = layer.repeat ?? true;
35759
+ const finish = (picture, keepWidth) => finishPicture(picture, { foot: layer.foot, tint: layer.tint, palette, keepWidth }, `parallax ${setId}: ${name}`);
35723
35760
  let png;
35724
35761
  let source;
35725
35762
  if (layer.sky) {
@@ -35730,14 +35767,13 @@ async function resolveParallaxSet(manifest, manifestDir, lock, setId) {
35730
35767
  const seed = layer.seed ?? seedFromName(name);
35731
35768
  const loop = layer.repeat ?? true;
35732
35769
  const width = layer.width ?? Math.max(1, viewW + Math.ceil(travel * layer.speed) - (loop ? 0 : layer.x ?? 0));
35733
- png = layRun(sprites.map((sprite) => sprite.png), width, { seed, step: layer.step, gap: layer.gap, jitter: layer.jitter, loop });
35770
+ png = layRun(sprites.map((sprite) => finish(sprite.png, false)), width, { seed, step: layer.step, gap: layer.gap, jitter: layer.jitter, loop });
35734
35771
  source = { kind: "run", seed, sprites: sprites.map((sprite) => sprite.source) };
35735
35772
  } else {
35736
35773
  const picture = await readPicture(manifest, manifestDir, lock, setId, name, layer, warnings);
35737
- png = picture.png;
35774
+ png = finish(picture.png, repeat);
35738
35775
  source = picture.source;
35739
35776
  }
35740
- const repeat = layer.repeat ?? true;
35741
35777
  let bottom = (layer.bottom ?? viewH) + (layer.run ? layer.jitter ?? 0 : 0);
35742
35778
  if (layer.extend) {
35743
35779
  const rows = layer.extend === true ? Math.max(0, viewH - bottom) : layer.extend;
@@ -35755,6 +35791,7 @@ async function resolveParallaxSet(manifest, manifestDir, lock, setId) {
35755
35791
  repeatY: layer.repeatY ?? false,
35756
35792
  source
35757
35793
  };
35794
+ if (layer.foot !== void 0 || layer.tint) image.finish = { foot: layer.foot, tint: layer.tint };
35758
35795
  if (repeat && source.kind !== "sky" && source.kind !== "run") {
35759
35796
  image.seamRatio = seamRatio(png);
35760
35797
  if (travel * layer.speed > 0 && image.seamRatio > SEAM_WARN_RATIO) {
@@ -35783,7 +35820,104 @@ async function resolveParallaxSet(manifest, manifestDir, lock, setId) {
35783
35820
  }
35784
35821
  layers.push(image);
35785
35822
  }
35786
- return { id: setId, view: [viewW, viewH], travel, climb, layers, warnings };
35823
+ return { id: setId, view: [viewW, viewH], travel, climb, ...palette ? { palette } : {}, layers, warnings };
35824
+ }
35825
+ function finishPicture(png, options, where = "picture") {
35826
+ let out = png;
35827
+ if (options.foot !== void 0) out = ditherFoot(trimOpaque(out, options.keepWidth ?? false, where), options.foot);
35828
+ if (options.tint) out = tintPicture(out, options.tint, options.palette);
35829
+ return out;
35830
+ }
35831
+ function trimOpaque(png, keepWidth, where = "picture") {
35832
+ let top = png.height;
35833
+ let bottom = -1;
35834
+ let left = png.width;
35835
+ let right = -1;
35836
+ for (let y = 0; y < png.height; y++) {
35837
+ for (let x = 0; x < png.width; x++) {
35838
+ if (png.pixels[(y * png.width + x) * 4 + 3] === 0) continue;
35839
+ top = Math.min(top, y);
35840
+ bottom = Math.max(bottom, y);
35841
+ left = Math.min(left, x);
35842
+ right = Math.max(right, x);
35843
+ }
35844
+ }
35845
+ if (bottom < 0) throw new Error(`${where}: the picture has no opaque pixel`);
35846
+ if (keepWidth) {
35847
+ left = 0;
35848
+ right = png.width - 1;
35849
+ }
35850
+ const width = right - left + 1;
35851
+ const height = bottom - top + 1;
35852
+ const pixels = Buffer.alloc(width * height * 4);
35853
+ for (let y = 0; y < height; y++) {
35854
+ png.pixels.copy(pixels, y * width * 4, ((top + y) * png.width + left) * 4, ((top + y) * png.width + right + 1) * 4);
35855
+ }
35856
+ return { width, height, pixels };
35857
+ }
35858
+ function ditherFoot(png, fraction) {
35859
+ const { width, height } = png;
35860
+ const pixels = Buffer.from(png.pixels);
35861
+ const band = Math.max(1, roundHalfEven(height * fraction));
35862
+ for (let y = Math.max(0, height - band); y < height; y++) {
35863
+ const keep = 1 - (y - (height - band) + 1) / (band + 1);
35864
+ for (let x = 0; x < width; x++) {
35865
+ if (BAYER4[y % 4][x % 4] >= keep * 16) pixels[(y * width + x) * 4 + 3] = 0;
35866
+ }
35867
+ }
35868
+ return { width, height, pixels };
35869
+ }
35870
+ function tintPicture(png, tint, palette) {
35871
+ const target = parseHex(tint.color);
35872
+ const t = tint.amount;
35873
+ const pixels = Buffer.from(png.pixels);
35874
+ let colours;
35875
+ if (palette) {
35876
+ colours = palette.map((hex2) => {
35877
+ const { r, g, b } = parseHex(hex2);
35878
+ return [r, g, b];
35879
+ });
35880
+ } else {
35881
+ const seen = /* @__PURE__ */ new Map();
35882
+ for (let p = 0; p < pixels.length; p += 4) {
35883
+ if (pixels[p + 3] !== 0) seen.set(pixels[p] << 16 | pixels[p + 1] << 8 | pixels[p + 2], [pixels[p], pixels[p + 1], pixels[p + 2]]);
35884
+ }
35885
+ colours = [...seen.values()];
35886
+ }
35887
+ colours.sort((a, b) => a[0] - b[0] || a[1] - b[1] || a[2] - b[2]);
35888
+ const snapped = /* @__PURE__ */ new Map();
35889
+ for (let p = 0; p < pixels.length; p += 4) {
35890
+ if (pixels[p + 3] === 0) continue;
35891
+ const mixed = [
35892
+ roundHalfEven(pixels[p] * (1 - t) + target.r * t),
35893
+ roundHalfEven(pixels[p + 1] * (1 - t) + target.g * t),
35894
+ roundHalfEven(pixels[p + 2] * (1 - t) + target.b * t)
35895
+ ];
35896
+ const key3 = mixed[0] << 16 | mixed[1] << 8 | mixed[2];
35897
+ let best = snapped.get(key3);
35898
+ if (!best) {
35899
+ let bestDistance = Infinity;
35900
+ for (const colour of colours) {
35901
+ const d = (mixed[0] - colour[0]) ** 2 + (mixed[1] - colour[1]) ** 2 + (mixed[2] - colour[2]) ** 2;
35902
+ if (d < bestDistance) {
35903
+ bestDistance = d;
35904
+ best = colour;
35905
+ }
35906
+ }
35907
+ snapped.set(key3, best);
35908
+ }
35909
+ pixels[p] = best[0];
35910
+ pixels[p + 1] = best[1];
35911
+ pixels[p + 2] = best[2];
35912
+ }
35913
+ return { width: png.width, height: png.height, pixels };
35914
+ }
35915
+ function roundHalfEven(value) {
35916
+ const floor = Math.floor(value);
35917
+ const rest = value - floor;
35918
+ if (rest > 0.5) return floor + 1;
35919
+ if (rest < 0.5) return floor;
35920
+ return floor % 2 === 0 ? floor : floor + 1;
35787
35921
  }
35788
35922
  async function readPicture(manifest, manifestDir, lock, setId, name, ref, warnings) {
35789
35923
  if (ref.file) {
@@ -36410,8 +36544,10 @@ function setOptions(set) {
36410
36544
  repeat: layer.repeat,
36411
36545
  repeatY: layer.repeatY,
36412
36546
  ...layer.source.kind === "sky" ? { sky: layer.source.colors } : {},
36413
- ...layer.source.kind === "run" ? { run: { seed: layer.source.seed, sprites: layer.source.sprites.length, width: layer.png.width } } : {}
36414
- }))
36547
+ ...layer.source.kind === "run" ? { run: { seed: layer.source.seed, sprites: layer.source.sprites.length, width: layer.png.width } } : {},
36548
+ ...layer.finish ?? {}
36549
+ })),
36550
+ ...set.palette ? { palette: set.palette } : {}
36415
36551
  };
36416
36552
  }
36417
36553