pixelkiln 0.142.1 → 0.143.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.
package/dist/cli.js CHANGED
@@ -3046,6 +3046,16 @@ var StyleObjectSchema = z.object({
3046
3046
  * chocolate bar.
3047
3047
  */
3048
3048
  palette: z.array(HexColorSchema).default([]),
3049
+ /**
3050
+ * The palette as a file instead of inline colours, relative to the
3051
+ * manifest: a Lospec `.hex`, a GIMP `.gpl`, or a Paint.NET `.txt`.
3052
+ * `loadManifest` reads it into `palette` before validation, so a file
3053
+ * palette hashes the same as the same colours written inline, and editing
3054
+ * the file makes the style's assets stale. Not with `palette` on the same
3055
+ * style; a child style's own `palette` or `paletteFile` replaces its
3056
+ * parent's.
3057
+ */
3058
+ paletteFile: z.string().min(1).optional(),
3049
3059
  /** `target_palette.name` ("Game Boy", "PICO-8"), which PixelLab uses to help the look. Needs `palette`; generators in `TARGET_PALETTE_GENERATORS` only. */
3050
3060
  paletteName: z.string().min(1).max(100).optional(),
3051
3061
  /** `target_palette.max_colors`: at most this many colours in each image, 2 to 64 (drawn from `palette` when one is set). Generators in `TARGET_PALETTE_GENERATORS` only. */
@@ -4448,6 +4458,38 @@ function quantize(png, palette) {
4448
4458
  function quantizeToPalette(png, palette) {
4449
4459
  return quantize(png, palette).png;
4450
4460
  }
4461
+ function parsePaletteFile(text, fileName) {
4462
+ const ext = fileName.toLowerCase().split(".").pop();
4463
+ const lines = text.replace(/^/, "").split(/\r?\n/);
4464
+ const colors = [];
4465
+ const fail = (line, why) => {
4466
+ throw new Error(`${fileName}:${line}: ${why}`);
4467
+ };
4468
+ if (ext === "gpl") {
4469
+ if (!/^GIMP Palette\s*$/.test(lines[0]?.trim() ?? "")) fail(1, 'a .gpl palette starts with "GIMP Palette"');
4470
+ lines.slice(1).forEach((raw, index) => {
4471
+ const line = raw.trim();
4472
+ if (!line || line.startsWith("#") || /^(Name|Columns):/i.test(line)) return;
4473
+ const match = /^(\d{1,3})\s+(\d{1,3})\s+(\d{1,3})(\s|$)/.exec(line);
4474
+ if (!match) return fail(index + 2, `expected "R G B", got "${line}"`);
4475
+ const channels = match.slice(1, 4).map(Number);
4476
+ if (channels.some((value) => value > 255)) fail(index + 2, `channel above 255 in "${line}"`);
4477
+ colors.push(`#${channels.map((value) => value.toString(16).padStart(2, "0")).join("")}`);
4478
+ });
4479
+ } else if (ext === "hex" || ext === "txt") {
4480
+ lines.forEach((raw, index) => {
4481
+ const line = raw.trim();
4482
+ if (!line || ext === "txt" && line.startsWith(";")) return;
4483
+ const match = ext === "hex" ? /^#?([0-9a-f]{6})$/i.exec(line) : /^[0-9a-f]{2}([0-9a-f]{6})$/i.exec(line);
4484
+ if (!match) fail(index + 1, `expected ${ext === "hex" ? "rrggbb" : "aarrggbb"}, got "${line}"`);
4485
+ colors.push(`#${match[1].toLowerCase()}`);
4486
+ });
4487
+ } else {
4488
+ throw new Error(`${fileName}: palette files are .hex, .gpl, or .txt (Paint.NET)`);
4489
+ }
4490
+ if (!colors.length) throw new Error(`${fileName}: no colours found`);
4491
+ return colors;
4492
+ }
4451
4493
 
4452
4494
  // src/paperdoll.ts
4453
4495
  var at = (image, x, y) => (y * image.width + x) * 4;
@@ -16471,8 +16513,20 @@ async function loadManifest(manifestPath, opts = {}) {
16471
16513
  throw new ProjectError(`Manifest at ${abs} is invalid:
16472
16514
  ${expansion.issues.map((i) => ` ${i}`).join("\n")}`);
16473
16515
  }
16474
- if (!opts.lenient) return loadExpanded(abs, expansion);
16516
+ const paletteIssues = await readPaletteFiles(expansion.raw, path19.dirname(abs));
16517
+ if (!opts.lenient) {
16518
+ if (paletteIssues.length) {
16519
+ throw new ProjectError(
16520
+ `Manifest at ${abs} is invalid:
16521
+ ${paletteIssues.flatMap((entry) => entry.issues).map((i) => ` ${i}`).join("\n")}`
16522
+ );
16523
+ }
16524
+ return loadExpanded(abs, expansion);
16525
+ }
16475
16526
  const invalid = [];
16527
+ for (const entry of paletteIssues) {
16528
+ if (dropEntry(expansion.raw, entry)) invalid.push(entry);
16529
+ }
16476
16530
  const raw = pruneInvalidEntries(expansion.raw, invalid);
16477
16531
  for (let round2 = 0; ; round2++) {
16478
16532
  try {
@@ -16486,6 +16540,37 @@ ${expansion.issues.map((i) => ` ${i}`).join("\n")}`);
16486
16540
  }
16487
16541
  }
16488
16542
  }
16543
+ async function readPaletteFiles(raw, manifestDir) {
16544
+ const styles = entryRecord(raw, "style");
16545
+ if (!styles) return [];
16546
+ const issues = [];
16547
+ for (const [id, value] of Object.entries(styles)) {
16548
+ if (!value || typeof value !== "object" || Array.isArray(value)) continue;
16549
+ const style = value;
16550
+ const file = style.paletteFile;
16551
+ if (file === void 0) continue;
16552
+ const fail = (why) => issues.push({ kind: "style", id, issues: [`styles.${id}.paletteFile: ${why}`] });
16553
+ if (typeof file !== "string" || !file) {
16554
+ fail("expected a path to a .hex, .gpl, or .txt palette");
16555
+ continue;
16556
+ }
16557
+ if (style.palette !== void 0) {
16558
+ fail("set palette or paletteFile, not both");
16559
+ continue;
16560
+ }
16561
+ const abs = path19.resolve(manifestDir, file);
16562
+ if (!existsSync14(abs)) {
16563
+ fail(`no file at ${file}`);
16564
+ continue;
16565
+ }
16566
+ try {
16567
+ style.palette = parsePaletteFile(await readFile10(abs, "utf8"), file);
16568
+ } catch (error) {
16569
+ fail(error instanceof Error ? error.message : String(error));
16570
+ }
16571
+ }
16572
+ return issues;
16573
+ }
16489
16574
  function entryRecord(raw, kind) {
16490
16575
  const record = raw?.[kind === "style" ? "styles" : "assets"];
16491
16576
  return record && typeof record === "object" && !Array.isArray(record) ? record : null;