pixelkiln 0.142.2 → 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/index.cjs CHANGED
@@ -1772,6 +1772,16 @@ var StyleObjectSchema = import_zod.z.object({
1772
1772
  * chocolate bar.
1773
1773
  */
1774
1774
  palette: import_zod.z.array(HexColorSchema).default([]),
1775
+ /**
1776
+ * The palette as a file instead of inline colours, relative to the
1777
+ * manifest: a Lospec `.hex`, a GIMP `.gpl`, or a Paint.NET `.txt`.
1778
+ * `loadManifest` reads it into `palette` before validation, so a file
1779
+ * palette hashes the same as the same colours written inline, and editing
1780
+ * the file makes the style's assets stale. Not with `palette` on the same
1781
+ * style; a child style's own `palette` or `paletteFile` replaces its
1782
+ * parent's.
1783
+ */
1784
+ paletteFile: import_zod.z.string().min(1).optional(),
1775
1785
  /** `target_palette.name` ("Game Boy", "PICO-8"), which PixelLab uses to help the look. Needs `palette`; generators in `TARGET_PALETTE_GENERATORS` only. */
1776
1786
  paletteName: import_zod.z.string().min(1).max(100).optional(),
1777
1787
  /** `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. */
@@ -8116,6 +8126,38 @@ function quantize(png, palette) {
8116
8126
  function quantizeToPalette(png, palette) {
8117
8127
  return quantize(png, palette).png;
8118
8128
  }
8129
+ function parsePaletteFile(text, fileName) {
8130
+ const ext = fileName.toLowerCase().split(".").pop();
8131
+ const lines = text.replace(/^/, "").split(/\r?\n/);
8132
+ const colors = [];
8133
+ const fail = (line, why) => {
8134
+ throw new Error(`${fileName}:${line}: ${why}`);
8135
+ };
8136
+ if (ext === "gpl") {
8137
+ if (!/^GIMP Palette\s*$/.test(lines[0]?.trim() ?? "")) fail(1, 'a .gpl palette starts with "GIMP Palette"');
8138
+ lines.slice(1).forEach((raw, index) => {
8139
+ const line = raw.trim();
8140
+ if (!line || line.startsWith("#") || /^(Name|Columns):/i.test(line)) return;
8141
+ const match = /^(\d{1,3})\s+(\d{1,3})\s+(\d{1,3})(\s|$)/.exec(line);
8142
+ if (!match) return fail(index + 2, `expected "R G B", got "${line}"`);
8143
+ const channels = match.slice(1, 4).map(Number);
8144
+ if (channels.some((value) => value > 255)) fail(index + 2, `channel above 255 in "${line}"`);
8145
+ colors.push(`#${channels.map((value) => value.toString(16).padStart(2, "0")).join("")}`);
8146
+ });
8147
+ } else if (ext === "hex" || ext === "txt") {
8148
+ lines.forEach((raw, index) => {
8149
+ const line = raw.trim();
8150
+ if (!line || ext === "txt" && line.startsWith(";")) return;
8151
+ const match = ext === "hex" ? /^#?([0-9a-f]{6})$/i.exec(line) : /^[0-9a-f]{2}([0-9a-f]{6})$/i.exec(line);
8152
+ if (!match) fail(index + 1, `expected ${ext === "hex" ? "rrggbb" : "aarrggbb"}, got "${line}"`);
8153
+ colors.push(`#${match[1].toLowerCase()}`);
8154
+ });
8155
+ } else {
8156
+ throw new Error(`${fileName}: palette files are .hex, .gpl, or .txt (Paint.NET)`);
8157
+ }
8158
+ if (!colors.length) throw new Error(`${fileName}: no colours found`);
8159
+ return colors;
8160
+ }
8119
8161
 
8120
8162
  // src/pipeline/postprocess.ts
8121
8163
  function postprocessFor(spec) {
@@ -14055,8 +14097,20 @@ async function loadManifest(manifestPath, opts = {}) {
14055
14097
  throw new ProjectError(`Manifest at ${abs} is invalid:
14056
14098
  ${expansion.issues.map((i) => ` ${i}`).join("\n")}`);
14057
14099
  }
14058
- if (!opts.lenient) return loadExpanded(abs, expansion);
14100
+ const paletteIssues = await readPaletteFiles(expansion.raw, import_node_path11.default.dirname(abs));
14101
+ if (!opts.lenient) {
14102
+ if (paletteIssues.length) {
14103
+ throw new ProjectError(
14104
+ `Manifest at ${abs} is invalid:
14105
+ ${paletteIssues.flatMap((entry) => entry.issues).map((i) => ` ${i}`).join("\n")}`
14106
+ );
14107
+ }
14108
+ return loadExpanded(abs, expansion);
14109
+ }
14059
14110
  const invalid = [];
14111
+ for (const entry of paletteIssues) {
14112
+ if (dropEntry(expansion.raw, entry)) invalid.push(entry);
14113
+ }
14060
14114
  const raw = pruneInvalidEntries(expansion.raw, invalid);
14061
14115
  for (let round2 = 0; ; round2++) {
14062
14116
  try {
@@ -14070,6 +14124,37 @@ ${expansion.issues.map((i) => ` ${i}`).join("\n")}`);
14070
14124
  }
14071
14125
  }
14072
14126
  }
14127
+ async function readPaletteFiles(raw, manifestDir) {
14128
+ const styles = entryRecord(raw, "style");
14129
+ if (!styles) return [];
14130
+ const issues = [];
14131
+ for (const [id, value] of Object.entries(styles)) {
14132
+ if (!value || typeof value !== "object" || Array.isArray(value)) continue;
14133
+ const style = value;
14134
+ const file = style.paletteFile;
14135
+ if (file === void 0) continue;
14136
+ const fail = (why) => issues.push({ kind: "style", id, issues: [`styles.${id}.paletteFile: ${why}`] });
14137
+ if (typeof file !== "string" || !file) {
14138
+ fail("expected a path to a .hex, .gpl, or .txt palette");
14139
+ continue;
14140
+ }
14141
+ if (style.palette !== void 0) {
14142
+ fail("set palette or paletteFile, not both");
14143
+ continue;
14144
+ }
14145
+ const abs = import_node_path11.default.resolve(manifestDir, file);
14146
+ if (!(0, import_node_fs11.existsSync)(abs)) {
14147
+ fail(`no file at ${file}`);
14148
+ continue;
14149
+ }
14150
+ try {
14151
+ style.palette = parsePaletteFile(await (0, import_promises8.readFile)(abs, "utf8"), file);
14152
+ } catch (error) {
14153
+ fail(error instanceof Error ? error.message : String(error));
14154
+ }
14155
+ }
14156
+ return issues;
14157
+ }
14073
14158
  function entryRecord(raw, kind) {
14074
14159
  const record = raw?.[kind === "style" ? "styles" : "assets"];
14075
14160
  return record && typeof record === "object" && !Array.isArray(record) ? record : null;