@holtjs/craft 0.0.0-stage → 0.1.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/CHANGELOG.md +14 -0
- package/LICENSE +201 -0
- package/NOTICE +10 -0
- package/README.md +63 -2
- package/dist/atlas-Pc12eQsg.js +112 -0
- package/dist/atlas.d.ts +75 -0
- package/dist/atlas.js +1 -0
- package/dist/index.d.ts +154 -0
- package/dist/index.js +14 -0
- package/dist/inspect-NzY8Ac1j.js +466 -0
- package/dist/inspect.d.ts +36 -0
- package/dist/inspect.js +1 -0
- package/dist/pixel-D3jgByB5.js +728 -0
- package/dist/pixel.d.ts +79 -0
- package/dist/pixel.js +1 -0
- package/dist/rig3d-C3i7UL88.js +7257 -0
- package/dist/rig3d-DhTgbTbX.d.ts +605 -0
- package/dist/rig3d.d.ts +2 -0
- package/dist/rig3d.js +2 -0
- package/dist/tilemap-BFH4_JLe.js +266 -0
- package/dist/tilemap.d.ts +61 -0
- package/dist/tilemap.js +1 -0
- package/llms.txt +16 -0
- package/package.json +71 -4
- package/skills/craft/SKILL.md +93 -0
- package/skills/craft/references/subpaths.md +32 -0
|
@@ -0,0 +1,728 @@
|
|
|
1
|
+
// \0rolldown/runtime.js
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __exportAll = (all, flags) => {
|
|
4
|
+
let target = Object.setPrototypeOf({}, null);
|
|
5
|
+
for (var name in all) __defProp(target, name, {
|
|
6
|
+
get: all[name],
|
|
7
|
+
enumerable: true
|
|
8
|
+
});
|
|
9
|
+
if (!(flags & 1)) __defProp(target, Symbol.toStringTag, { value: "Module" });
|
|
10
|
+
return flags & 2 ? target : Object.preventExtensions(target);
|
|
11
|
+
};
|
|
12
|
+
// src/pixel.ts
|
|
13
|
+
var pixel_exports = /* @__PURE__ */ __exportAll({
|
|
14
|
+
PixelCanvas: () => PixelCanvas,
|
|
15
|
+
compilePixelGrid: () => compilePixelGrid,
|
|
16
|
+
derivePaletteVariants: () => derivePaletteVariants,
|
|
17
|
+
derivePixelRamp: () => derivePixelRamp,
|
|
18
|
+
encodePngRgba: () => encodePngRgba,
|
|
19
|
+
mixRgb: () => mixRgb,
|
|
20
|
+
parseHexRgb: () => parseHexRgb,
|
|
21
|
+
rgbToHex: () => rgbToHex
|
|
22
|
+
});
|
|
23
|
+
const mixRgb = (a, b, t) => [
|
|
24
|
+
Math.round(a[0] + (b[0] - a[0]) * t),
|
|
25
|
+
Math.round(a[1] + (b[1] - a[1]) * t),
|
|
26
|
+
Math.round(a[2] + (b[2] - a[2]) * t)
|
|
27
|
+
];
|
|
28
|
+
function parseHexRgb(hex) {
|
|
29
|
+
const s = hex.trim().replace(/^#/, "");
|
|
30
|
+
if (s.length === 3) return [
|
|
31
|
+
parseInt(s[0] + s[0], 16) || 0,
|
|
32
|
+
parseInt(s[1] + s[1], 16) || 0,
|
|
33
|
+
parseInt(s[2] + s[2], 16) || 0
|
|
34
|
+
];
|
|
35
|
+
return [
|
|
36
|
+
parseInt(s.slice(0, 2), 16) || 0,
|
|
37
|
+
parseInt(s.slice(2, 4), 16) || 0,
|
|
38
|
+
parseInt(s.slice(4, 6), 16) || 0
|
|
39
|
+
];
|
|
40
|
+
}
|
|
41
|
+
function rgbToHex(rgb) {
|
|
42
|
+
const h = (n) => Math.max(0, Math.min(255, Math.round(n))).toString(16).padStart(2, "0");
|
|
43
|
+
return `#${h(rgb[0])}${h(rgb[1])}${h(rgb[2])}`;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Derive a 4-shade pixel-art color ramp `[light, base, shade, outline]` with warm sunlight shift
|
|
47
|
+
* in highlights and cool slate-plum shift in shadows & outlines (Graveyard Keeper / Eastward style).
|
|
48
|
+
*/
|
|
49
|
+
function derivePixelRamp(hexOrRgb) {
|
|
50
|
+
const base = typeof hexOrRgb === "string" ? parseHexRgb(hexOrRgb) : hexOrRgb;
|
|
51
|
+
return [
|
|
52
|
+
mixRgb(base, [
|
|
53
|
+
255,
|
|
54
|
+
250,
|
|
55
|
+
230
|
|
56
|
+
], .34),
|
|
57
|
+
base,
|
|
58
|
+
mixRgb(base, [
|
|
59
|
+
48,
|
|
60
|
+
28,
|
|
61
|
+
54
|
|
62
|
+
], .28),
|
|
63
|
+
mixRgb(base, [
|
|
64
|
+
26,
|
|
65
|
+
16,
|
|
66
|
+
28
|
|
67
|
+
], .64)
|
|
68
|
+
];
|
|
69
|
+
}
|
|
70
|
+
function rgbToHsl(c) {
|
|
71
|
+
const r = c[0] / 255, g = c[1] / 255, b = c[2] / 255;
|
|
72
|
+
const max = Math.max(r, g, b), min = Math.min(r, g, b);
|
|
73
|
+
const l = (max + min) * .5;
|
|
74
|
+
if (max === min) return [
|
|
75
|
+
0,
|
|
76
|
+
0,
|
|
77
|
+
l
|
|
78
|
+
];
|
|
79
|
+
const d = max - min;
|
|
80
|
+
const s = l > .5 ? d / (2 - max - min) : d / (max + min);
|
|
81
|
+
let h = 0;
|
|
82
|
+
if (max === r) h = ((g - b) / d + (g < b ? 6 : 0)) / 6;
|
|
83
|
+
else if (max === g) h = ((b - r) / d + 2) / 6;
|
|
84
|
+
else h = ((r - g) / d + 4) / 6;
|
|
85
|
+
return [
|
|
86
|
+
h,
|
|
87
|
+
s,
|
|
88
|
+
l
|
|
89
|
+
];
|
|
90
|
+
}
|
|
91
|
+
function hslToRgb(h, s, l) {
|
|
92
|
+
const hn = (h % 1 + 1) % 1;
|
|
93
|
+
if (s === 0) {
|
|
94
|
+
const v = Math.round(l * 255);
|
|
95
|
+
return [
|
|
96
|
+
v,
|
|
97
|
+
v,
|
|
98
|
+
v
|
|
99
|
+
];
|
|
100
|
+
}
|
|
101
|
+
const q = l < .5 ? l * (1 + s) : l + s - l * s;
|
|
102
|
+
const p = 2 * l - q;
|
|
103
|
+
const hue2rgb = (t) => {
|
|
104
|
+
let tn = t;
|
|
105
|
+
if (tn < 0) tn += 1;
|
|
106
|
+
if (tn > 1) tn -= 1;
|
|
107
|
+
if (tn < 1 / 6) return p + (q - p) * 6 * tn;
|
|
108
|
+
if (tn < .5) return q;
|
|
109
|
+
if (tn < 2 / 3) return p + (q - p) * (2 / 3 - tn) * 6;
|
|
110
|
+
return p;
|
|
111
|
+
};
|
|
112
|
+
return [
|
|
113
|
+
Math.round(hue2rgb(hn + 1 / 3) * 255),
|
|
114
|
+
Math.round(hue2rgb(hn) * 255),
|
|
115
|
+
Math.round(hue2rgb(hn - 1 / 3) * 255)
|
|
116
|
+
];
|
|
117
|
+
}
|
|
118
|
+
/** Generate `count` (default 16) distinct hue/lightness palette variants from a base `#rrggbb` color. */
|
|
119
|
+
function derivePaletteVariants(baseHex, count = 16) {
|
|
120
|
+
const [h0, s0, l0] = rgbToHsl(parseHexRgb(baseHex));
|
|
121
|
+
const out = [];
|
|
122
|
+
for (let i = 0; i < count; i++) {
|
|
123
|
+
const h = h0 + i / count * .92;
|
|
124
|
+
const s = Math.max(.18, Math.min(.88, s0 * (i % 3 === 2 ? .78 : 1.04)));
|
|
125
|
+
const l = Math.max(.24, Math.min(.76, l0 + (i % 4 - 1.5) * .05));
|
|
126
|
+
out.push(derivePixelRamp(hslToRgb(h, s, l)));
|
|
127
|
+
}
|
|
128
|
+
return out;
|
|
129
|
+
}
|
|
130
|
+
const BAYER_4X4 = [
|
|
131
|
+
0,
|
|
132
|
+
8,
|
|
133
|
+
2,
|
|
134
|
+
10,
|
|
135
|
+
12,
|
|
136
|
+
4,
|
|
137
|
+
14,
|
|
138
|
+
6,
|
|
139
|
+
3,
|
|
140
|
+
11,
|
|
141
|
+
1,
|
|
142
|
+
9,
|
|
143
|
+
15,
|
|
144
|
+
7,
|
|
145
|
+
13,
|
|
146
|
+
5
|
|
147
|
+
];
|
|
148
|
+
/** Integer-crisp zero-DOM RGBA pixel canvas for procedural sprites, tiles, and UI icons. */
|
|
149
|
+
var PixelCanvas = class PixelCanvas {
|
|
150
|
+
width;
|
|
151
|
+
height;
|
|
152
|
+
rgba;
|
|
153
|
+
constructor(width, height, rgba) {
|
|
154
|
+
this.width = Math.max(1, Math.floor(width));
|
|
155
|
+
this.height = Math.max(1, Math.floor(height));
|
|
156
|
+
this.rgba = rgba ?? new Uint8ClampedArray(this.width * this.height * 4);
|
|
157
|
+
}
|
|
158
|
+
clone() {
|
|
159
|
+
return new PixelCanvas(this.width, this.height, new Uint8ClampedArray(this.rgba));
|
|
160
|
+
}
|
|
161
|
+
clear(rgb, alpha = 0) {
|
|
162
|
+
if (!rgb && alpha === 0) {
|
|
163
|
+
this.rgba.fill(0);
|
|
164
|
+
return this;
|
|
165
|
+
}
|
|
166
|
+
const c = rgb ?? [
|
|
167
|
+
0,
|
|
168
|
+
0,
|
|
169
|
+
0
|
|
170
|
+
];
|
|
171
|
+
for (let i = 0; i < this.rgba.length; i += 4) {
|
|
172
|
+
this.rgba[i] = c[0];
|
|
173
|
+
this.rgba[i + 1] = c[1];
|
|
174
|
+
this.rgba[i + 2] = c[2];
|
|
175
|
+
this.rgba[i + 3] = alpha;
|
|
176
|
+
}
|
|
177
|
+
return this;
|
|
178
|
+
}
|
|
179
|
+
setPixel(x, y, color, alpha = 255) {
|
|
180
|
+
const ix = Math.floor(x), iy = Math.floor(y);
|
|
181
|
+
if (ix < 0 || iy < 0 || ix >= this.width || iy >= this.height || alpha <= 0) return this;
|
|
182
|
+
const c = typeof color === "string" ? parseHexRgb(color) : color;
|
|
183
|
+
const o = (iy * this.width + ix) * 4;
|
|
184
|
+
if (alpha >= 255 || this.rgba[o + 3] === 0) {
|
|
185
|
+
this.rgba[o] = c[0];
|
|
186
|
+
this.rgba[o + 1] = c[1];
|
|
187
|
+
this.rgba[o + 2] = c[2];
|
|
188
|
+
this.rgba[o + 3] = Math.min(255, Math.round(alpha));
|
|
189
|
+
} else {
|
|
190
|
+
const sa = alpha / 255;
|
|
191
|
+
const da = this.rgba[o + 3] / 255;
|
|
192
|
+
const outA = sa + da * (1 - sa);
|
|
193
|
+
if (outA > 1e-5) {
|
|
194
|
+
this.rgba[o] = Math.round((c[0] * sa + this.rgba[o] * da * (1 - sa)) / outA);
|
|
195
|
+
this.rgba[o + 1] = Math.round((c[1] * sa + this.rgba[o + 1] * da * (1 - sa)) / outA);
|
|
196
|
+
this.rgba[o + 2] = Math.round((c[2] * sa + this.rgba[o + 2] * da * (1 - sa)) / outA);
|
|
197
|
+
this.rgba[o + 3] = Math.round(outA * 255);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
return this;
|
|
201
|
+
}
|
|
202
|
+
getPixel(x, y) {
|
|
203
|
+
const ix = Math.floor(x), iy = Math.floor(y);
|
|
204
|
+
if (ix < 0 || iy < 0 || ix >= this.width || iy >= this.height) return [
|
|
205
|
+
0,
|
|
206
|
+
0,
|
|
207
|
+
0,
|
|
208
|
+
0
|
|
209
|
+
];
|
|
210
|
+
const o = (iy * this.width + ix) * 4;
|
|
211
|
+
return [
|
|
212
|
+
this.rgba[o],
|
|
213
|
+
this.rgba[o + 1],
|
|
214
|
+
this.rgba[o + 2],
|
|
215
|
+
this.rgba[o + 3]
|
|
216
|
+
];
|
|
217
|
+
}
|
|
218
|
+
fillRect(x, y, w, h, color, alpha = 255) {
|
|
219
|
+
const c = typeof color === "string" ? parseHexRgb(color) : color;
|
|
220
|
+
const x0 = Math.max(0, Math.floor(x));
|
|
221
|
+
const y0 = Math.max(0, Math.floor(y));
|
|
222
|
+
const x1 = Math.min(this.width, Math.floor(x + w));
|
|
223
|
+
const y1 = Math.min(this.height, Math.floor(y + h));
|
|
224
|
+
for (let py = y0; py < y1; py++) for (let px = x0; px < x1; px++) this.setPixel(px, py, c, alpha);
|
|
225
|
+
return this;
|
|
226
|
+
}
|
|
227
|
+
strokeRect(x, y, w, h, color, alpha = 255) {
|
|
228
|
+
const c = typeof color === "string" ? parseHexRgb(color) : color;
|
|
229
|
+
this.fillRect(x, y, w, 1, c, alpha);
|
|
230
|
+
this.fillRect(x, y + h - 1, w, 1, c, alpha);
|
|
231
|
+
this.fillRect(x, y, 1, h, c, alpha);
|
|
232
|
+
this.fillRect(x + w - 1, y, 1, h, c, alpha);
|
|
233
|
+
return this;
|
|
234
|
+
}
|
|
235
|
+
fillEllipse(cx, cy, rx, ry, color, alpha = 255) {
|
|
236
|
+
if (rx <= 0 || ry <= 0) return this;
|
|
237
|
+
const c = typeof color === "string" ? parseHexRgb(color) : color;
|
|
238
|
+
const x0 = Math.max(0, Math.floor(cx - rx));
|
|
239
|
+
const x1 = Math.min(this.width - 1, Math.ceil(cx + rx));
|
|
240
|
+
const y0 = Math.max(0, Math.floor(cy - ry));
|
|
241
|
+
const y1 = Math.min(this.height - 1, Math.ceil(cy + ry));
|
|
242
|
+
for (let py = y0; py <= y1; py++) {
|
|
243
|
+
const ny = (py + .5 - cy) / ry;
|
|
244
|
+
for (let px = x0; px <= x1; px++) {
|
|
245
|
+
const nx = (px + .5 - cx) / rx;
|
|
246
|
+
if (nx * nx + ny * ny <= 1) this.setPixel(px, py, c, alpha);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
return this;
|
|
250
|
+
}
|
|
251
|
+
drawLine(x0, y0, x1, y1, color, alpha = 255) {
|
|
252
|
+
const c = typeof color === "string" ? parseHexRgb(color) : color;
|
|
253
|
+
let ix0 = Math.round(x0), iy0 = Math.round(y0);
|
|
254
|
+
const ix1 = Math.round(x1), iy1 = Math.round(y1);
|
|
255
|
+
const dx = Math.abs(ix1 - ix0), sx = ix0 < ix1 ? 1 : -1;
|
|
256
|
+
const dy = -Math.abs(iy1 - iy0), sy = iy0 < iy1 ? 1 : -1;
|
|
257
|
+
let err = dx + dy;
|
|
258
|
+
for (;;) {
|
|
259
|
+
this.setPixel(ix0, iy0, c, alpha);
|
|
260
|
+
if (ix0 === ix1 && iy0 === iy1) break;
|
|
261
|
+
const e2 = 2 * err;
|
|
262
|
+
if (e2 >= dy) {
|
|
263
|
+
err += dy;
|
|
264
|
+
ix0 += sx;
|
|
265
|
+
}
|
|
266
|
+
if (e2 <= dx) {
|
|
267
|
+
err += dx;
|
|
268
|
+
iy0 += sy;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
return this;
|
|
272
|
+
}
|
|
273
|
+
blit(src, dstX, dstY, opts) {
|
|
274
|
+
const ox = Math.round(dstX), oy = Math.round(dstY);
|
|
275
|
+
const behind = opts?.behind === true;
|
|
276
|
+
const fx = opts?.flipX === true;
|
|
277
|
+
for (let sy = 0; sy < src.height; sy++) {
|
|
278
|
+
const ty = oy + sy;
|
|
279
|
+
if (ty < 0 || ty >= this.height) continue;
|
|
280
|
+
for (let sx = 0; sx < src.width; sx++) {
|
|
281
|
+
const tx = ox + (fx ? src.width - 1 - sx : sx);
|
|
282
|
+
if (tx < 0 || tx >= this.width) continue;
|
|
283
|
+
const so = (sy * src.width + sx) * 4;
|
|
284
|
+
const sa = src.rgba[so + 3];
|
|
285
|
+
if (sa === 0) continue;
|
|
286
|
+
const doff = (ty * this.width + tx) * 4;
|
|
287
|
+
if (behind && this.rgba[doff + 3] > 0) continue;
|
|
288
|
+
this.setPixel(tx, ty, [
|
|
289
|
+
src.rgba[so],
|
|
290
|
+
src.rgba[so + 1],
|
|
291
|
+
src.rgba[so + 2]
|
|
292
|
+
], sa);
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
return this;
|
|
296
|
+
}
|
|
297
|
+
flipX() {
|
|
298
|
+
return new PixelCanvas(this.width, this.height).blit(this, 0, 0, { flipX: true });
|
|
299
|
+
}
|
|
300
|
+
/** Apply 4x4 Bayer ordered dithering between `colorA` and `colorB` inside a rectangle (`ratio` in `[0, 1]`). */
|
|
301
|
+
applyOrderedDither(x, y, w, h, colorA, colorB, ratio = .5) {
|
|
302
|
+
const ca = typeof colorA === "string" ? parseHexRgb(colorA) : colorA;
|
|
303
|
+
const cb = typeof colorB === "string" ? parseHexRgb(colorB) : colorB;
|
|
304
|
+
const thresh = Math.max(0, Math.min(16, Math.round(ratio * 16)));
|
|
305
|
+
const x0 = Math.max(0, Math.floor(x)), y0 = Math.max(0, Math.floor(y));
|
|
306
|
+
const x1 = Math.min(this.width, Math.floor(x + w)), y1 = Math.min(this.height, Math.floor(y + h));
|
|
307
|
+
for (let py = y0; py < y1; py++) for (let px = x0; px < x1; px++) {
|
|
308
|
+
const b = BAYER_4X4[(py & 3) << 2 | px & 3];
|
|
309
|
+
this.setPixel(px, py, b < thresh ? cb : ca, 255);
|
|
310
|
+
}
|
|
311
|
+
return this;
|
|
312
|
+
}
|
|
313
|
+
/** Add a 4-connected 1px selective dark-tinted outline around opaque silhouette edges. */
|
|
314
|
+
addSelectiveOutline(fixedColor) {
|
|
315
|
+
const fixed = fixedColor ? typeof fixedColor === "string" ? parseHexRgb(fixedColor) : fixedColor : null;
|
|
316
|
+
const snap = new Uint8ClampedArray(this.rgba);
|
|
317
|
+
const w = this.width, h = this.height;
|
|
318
|
+
const isSolid = (x, y) => x >= 0 && y >= 0 && x < w && y < h && snap[(y * w + x) * 4 + 3] >= 128;
|
|
319
|
+
for (let y = 0; y < h; y++) for (let x = 0; x < w; x++) {
|
|
320
|
+
const o = (y * w + x) * 4;
|
|
321
|
+
if (snap[o + 3] >= 128) continue;
|
|
322
|
+
let nr = 0, ng = 0, nb = 0, cnt = 0;
|
|
323
|
+
for (const [dx, dy] of [
|
|
324
|
+
[1, 0],
|
|
325
|
+
[-1, 0],
|
|
326
|
+
[0, 1],
|
|
327
|
+
[0, -1]
|
|
328
|
+
]) if (isSolid(x + dx, y + dy)) {
|
|
329
|
+
const no = ((y + dy) * w + (x + dx)) * 4;
|
|
330
|
+
nr += snap[no];
|
|
331
|
+
ng += snap[no + 1];
|
|
332
|
+
nb += snap[no + 2];
|
|
333
|
+
cnt++;
|
|
334
|
+
}
|
|
335
|
+
if (cnt > 0) {
|
|
336
|
+
const line = fixed ?? derivePixelRamp([
|
|
337
|
+
Math.round(nr / cnt),
|
|
338
|
+
Math.round(ng / cnt),
|
|
339
|
+
Math.round(nb / cnt)
|
|
340
|
+
])[3];
|
|
341
|
+
this.rgba[o] = line[0];
|
|
342
|
+
this.rgba[o + 1] = line[1];
|
|
343
|
+
this.rgba[o + 2] = line[2];
|
|
344
|
+
this.rgba[o + 3] = 255;
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
return this;
|
|
348
|
+
}
|
|
349
|
+
/** Render a grounded translucent ellipse contact shadow beneath the lowest opaque pixel of the sprite. */
|
|
350
|
+
addContactShadow(opts) {
|
|
351
|
+
let minX = this.width, maxX = -1, maxY = -1;
|
|
352
|
+
for (let y = 0; y < this.height; y++) for (let x = 0; x < this.width; x++) if (this.rgba[(y * this.width + x) * 4 + 3] >= 128) {
|
|
353
|
+
if (x < minX) minX = x;
|
|
354
|
+
if (x > maxX) maxX = x;
|
|
355
|
+
if (y > maxY) maxY = y;
|
|
356
|
+
}
|
|
357
|
+
if (maxY < 0) return this;
|
|
358
|
+
const cx = (minX + maxX + 1) * .5;
|
|
359
|
+
const rx = opts?.rx ?? Math.max(3, (maxX - minX + 1) * .44);
|
|
360
|
+
const ry = opts?.ry ?? Math.max(1.8, rx * .34);
|
|
361
|
+
const cy = Math.min(this.height - 1.5, maxY + (opts?.offsetY ?? 0));
|
|
362
|
+
const shadow = new PixelCanvas(this.width, this.height);
|
|
363
|
+
shadow.fillEllipse(cx, cy, rx, ry, [
|
|
364
|
+
18,
|
|
365
|
+
14,
|
|
366
|
+
24
|
|
367
|
+
], opts?.alpha ?? 96);
|
|
368
|
+
return this.blit(shadow, 0, 0, { behind: true });
|
|
369
|
+
}
|
|
370
|
+
toPng() {
|
|
371
|
+
return encodePngRgba(this.width, this.height, this.rgba);
|
|
372
|
+
}
|
|
373
|
+
};
|
|
374
|
+
/** Compile a declarative ASCII/palette pixel grid (`front`/`side`/`back`) into a crisp `PixelCanvas`. */
|
|
375
|
+
function compilePixelGrid(spec, opts = {}) {
|
|
376
|
+
const pad = Math.max(0, Math.floor(opts.pad ?? (opts.outline || opts.contactShadow ? 2 : 0)));
|
|
377
|
+
const d = opts.dir4 ?? 0;
|
|
378
|
+
const grid = d === 2 && spec.back ? spec.back : (d === 1 || d === 3) && spec.side ? spec.side : spec.front;
|
|
379
|
+
const flipX = d === 3 && Boolean(spec.side);
|
|
380
|
+
const canvas = new PixelCanvas(spec.width + pad * 2, spec.height + pad * 2);
|
|
381
|
+
const ramps = /* @__PURE__ */ new Map();
|
|
382
|
+
for (const [k, hex] of Object.entries(spec.palette)) ramps.set(k, derivePixelRamp(hex));
|
|
383
|
+
const keyGrid = Array.from({ length: spec.height }, (_, y) => {
|
|
384
|
+
const row = grid[y] ?? "";
|
|
385
|
+
return Array.from({ length: spec.width }, (__, x) => {
|
|
386
|
+
const ch = row[flipX ? spec.width - 1 - x : x] ?? ".";
|
|
387
|
+
return ch === "." || ch === " " ? "" : ch;
|
|
388
|
+
});
|
|
389
|
+
});
|
|
390
|
+
for (let y = 0; y < spec.height; y++) for (let x = 0; x < spec.width; x++) {
|
|
391
|
+
const k = keyGrid[y][x];
|
|
392
|
+
if (!k) continue;
|
|
393
|
+
const ramp = ramps.get(k);
|
|
394
|
+
if (!ramp) continue;
|
|
395
|
+
let shadeIdx = 1;
|
|
396
|
+
if (opts.bevel) {
|
|
397
|
+
const same = (nx, ny) => ny >= 0 && ny < spec.height && nx >= 0 && nx < spec.width && keyGrid[ny][nx] === k;
|
|
398
|
+
if (!same(x, y + 1) || !same(x + 1, y)) shadeIdx = 2;
|
|
399
|
+
else if (!same(x, y - 1) || !same(x - 1, y)) shadeIdx = 0;
|
|
400
|
+
}
|
|
401
|
+
canvas.setPixel(x + pad, y + pad, ramp[shadeIdx], 255);
|
|
402
|
+
}
|
|
403
|
+
if (opts.outline) canvas.addSelectiveOutline();
|
|
404
|
+
if (opts.contactShadow) canvas.addContactShadow();
|
|
405
|
+
return canvas;
|
|
406
|
+
}
|
|
407
|
+
const CRC_TABLE = (() => {
|
|
408
|
+
const t = /* @__PURE__ */ new Uint32Array(256);
|
|
409
|
+
for (let i = 0; i < 256; i++) {
|
|
410
|
+
let c = i;
|
|
411
|
+
for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ c >>> 1 : c >>> 1;
|
|
412
|
+
t[i] = c >>> 0;
|
|
413
|
+
}
|
|
414
|
+
return t;
|
|
415
|
+
})();
|
|
416
|
+
function crc32(bytes, start = 0, len = bytes.length - start) {
|
|
417
|
+
let c = 0xffffffff;
|
|
418
|
+
for (let i = start, end = start + len; i < end; i++) c = CRC_TABLE[(c ^ bytes[i]) & 255] ^ c >>> 8;
|
|
419
|
+
return (c ^ 0xffffffff) >>> 0;
|
|
420
|
+
}
|
|
421
|
+
function adler32(bytes) {
|
|
422
|
+
let s1 = 1, s2 = 0;
|
|
423
|
+
const NMAX = 5552;
|
|
424
|
+
let i = 0;
|
|
425
|
+
while (i < bytes.length) {
|
|
426
|
+
const end = Math.min(bytes.length, i + NMAX);
|
|
427
|
+
while (i < end) {
|
|
428
|
+
s1 += bytes[i++];
|
|
429
|
+
s2 += s1;
|
|
430
|
+
}
|
|
431
|
+
s1 %= 65521;
|
|
432
|
+
s2 %= 65521;
|
|
433
|
+
}
|
|
434
|
+
return (s2 << 16 | s1) >>> 0;
|
|
435
|
+
}
|
|
436
|
+
function writeU32BE(buf, off, val) {
|
|
437
|
+
buf[off] = val >>> 24 & 255;
|
|
438
|
+
buf[off + 1] = val >>> 16 & 255;
|
|
439
|
+
buf[off + 2] = val >>> 8 & 255;
|
|
440
|
+
buf[off + 3] = val & 255;
|
|
441
|
+
}
|
|
442
|
+
function reverseBits(val, bits) {
|
|
443
|
+
let r = 0;
|
|
444
|
+
for (let i = 0; i < bits; i++) r = r << 1 | val >>> i & 1;
|
|
445
|
+
return r;
|
|
446
|
+
}
|
|
447
|
+
const FIXED_LIT_CODE = /* @__PURE__ */ new Uint16Array(288);
|
|
448
|
+
const FIXED_LIT_BITS = /* @__PURE__ */ new Uint8Array(288);
|
|
449
|
+
for (let i = 0; i <= 143; i++) {
|
|
450
|
+
FIXED_LIT_CODE[i] = reverseBits(48 + i, 8);
|
|
451
|
+
FIXED_LIT_BITS[i] = 8;
|
|
452
|
+
}
|
|
453
|
+
for (let i = 144; i <= 255; i++) {
|
|
454
|
+
FIXED_LIT_CODE[i] = reverseBits(400 + (i - 144), 9);
|
|
455
|
+
FIXED_LIT_BITS[i] = 9;
|
|
456
|
+
}
|
|
457
|
+
for (let i = 256; i <= 279; i++) {
|
|
458
|
+
FIXED_LIT_CODE[i] = reverseBits(i - 256, 7);
|
|
459
|
+
FIXED_LIT_BITS[i] = 7;
|
|
460
|
+
}
|
|
461
|
+
for (let i = 280; i <= 287; i++) {
|
|
462
|
+
FIXED_LIT_CODE[i] = reverseBits(192 + (i - 280), 8);
|
|
463
|
+
FIXED_LIT_BITS[i] = 8;
|
|
464
|
+
}
|
|
465
|
+
const LEN_BASE = [
|
|
466
|
+
3,
|
|
467
|
+
4,
|
|
468
|
+
5,
|
|
469
|
+
6,
|
|
470
|
+
7,
|
|
471
|
+
8,
|
|
472
|
+
9,
|
|
473
|
+
10,
|
|
474
|
+
11,
|
|
475
|
+
13,
|
|
476
|
+
15,
|
|
477
|
+
17,
|
|
478
|
+
19,
|
|
479
|
+
23,
|
|
480
|
+
27,
|
|
481
|
+
31,
|
|
482
|
+
35,
|
|
483
|
+
43,
|
|
484
|
+
51,
|
|
485
|
+
59,
|
|
486
|
+
67,
|
|
487
|
+
83,
|
|
488
|
+
99,
|
|
489
|
+
115,
|
|
490
|
+
131,
|
|
491
|
+
163,
|
|
492
|
+
195,
|
|
493
|
+
227,
|
|
494
|
+
258
|
|
495
|
+
];
|
|
496
|
+
const LEN_EXTRA = [
|
|
497
|
+
0,
|
|
498
|
+
0,
|
|
499
|
+
0,
|
|
500
|
+
0,
|
|
501
|
+
0,
|
|
502
|
+
0,
|
|
503
|
+
0,
|
|
504
|
+
0,
|
|
505
|
+
1,
|
|
506
|
+
1,
|
|
507
|
+
1,
|
|
508
|
+
1,
|
|
509
|
+
2,
|
|
510
|
+
2,
|
|
511
|
+
2,
|
|
512
|
+
2,
|
|
513
|
+
3,
|
|
514
|
+
3,
|
|
515
|
+
3,
|
|
516
|
+
3,
|
|
517
|
+
4,
|
|
518
|
+
4,
|
|
519
|
+
4,
|
|
520
|
+
4,
|
|
521
|
+
5,
|
|
522
|
+
5,
|
|
523
|
+
5,
|
|
524
|
+
5,
|
|
525
|
+
0
|
|
526
|
+
];
|
|
527
|
+
const DIST_BASE = [
|
|
528
|
+
1,
|
|
529
|
+
2,
|
|
530
|
+
3,
|
|
531
|
+
4,
|
|
532
|
+
5,
|
|
533
|
+
7,
|
|
534
|
+
9,
|
|
535
|
+
13,
|
|
536
|
+
17,
|
|
537
|
+
25,
|
|
538
|
+
33,
|
|
539
|
+
49,
|
|
540
|
+
65,
|
|
541
|
+
97,
|
|
542
|
+
129,
|
|
543
|
+
193,
|
|
544
|
+
257,
|
|
545
|
+
385,
|
|
546
|
+
513,
|
|
547
|
+
769,
|
|
548
|
+
1025,
|
|
549
|
+
1537,
|
|
550
|
+
2049,
|
|
551
|
+
3073,
|
|
552
|
+
4097,
|
|
553
|
+
6145,
|
|
554
|
+
8193,
|
|
555
|
+
12289,
|
|
556
|
+
16385,
|
|
557
|
+
24577
|
|
558
|
+
];
|
|
559
|
+
const DIST_EXTRA = [
|
|
560
|
+
0,
|
|
561
|
+
0,
|
|
562
|
+
0,
|
|
563
|
+
0,
|
|
564
|
+
1,
|
|
565
|
+
1,
|
|
566
|
+
2,
|
|
567
|
+
2,
|
|
568
|
+
3,
|
|
569
|
+
3,
|
|
570
|
+
4,
|
|
571
|
+
4,
|
|
572
|
+
5,
|
|
573
|
+
5,
|
|
574
|
+
6,
|
|
575
|
+
6,
|
|
576
|
+
7,
|
|
577
|
+
7,
|
|
578
|
+
8,
|
|
579
|
+
8,
|
|
580
|
+
9,
|
|
581
|
+
9,
|
|
582
|
+
10,
|
|
583
|
+
10,
|
|
584
|
+
11,
|
|
585
|
+
11,
|
|
586
|
+
12,
|
|
587
|
+
12,
|
|
588
|
+
13,
|
|
589
|
+
13
|
|
590
|
+
];
|
|
591
|
+
function deflateFixedPixelLz77(raw, rowStride) {
|
|
592
|
+
let cap = Math.max(256, (raw.length >>> 2) + 64);
|
|
593
|
+
let out = new Uint8Array(cap);
|
|
594
|
+
out[0] = 120;
|
|
595
|
+
out[1] = 1;
|
|
596
|
+
let pos = 2;
|
|
597
|
+
let bitBuf = 0;
|
|
598
|
+
let bitCnt = 0;
|
|
599
|
+
const ensure = (need) => {
|
|
600
|
+
if (pos + need > cap) {
|
|
601
|
+
while (pos + need > cap) cap <<= 1;
|
|
602
|
+
const next = new Uint8Array(cap);
|
|
603
|
+
next.set(out.subarray(0, pos));
|
|
604
|
+
out = next;
|
|
605
|
+
}
|
|
606
|
+
};
|
|
607
|
+
const putBits = (val, bits) => {
|
|
608
|
+
bitBuf |= val << bitCnt;
|
|
609
|
+
bitCnt += bits;
|
|
610
|
+
ensure(4);
|
|
611
|
+
while (bitCnt >= 8) {
|
|
612
|
+
out[pos++] = bitBuf & 255;
|
|
613
|
+
bitBuf >>>= 8;
|
|
614
|
+
bitCnt -= 8;
|
|
615
|
+
}
|
|
616
|
+
};
|
|
617
|
+
const emitLengthDist = (length, dist) => {
|
|
618
|
+
let li = LEN_BASE.length - 1;
|
|
619
|
+
while (li > 0 && length < LEN_BASE[li]) li--;
|
|
620
|
+
const sym = 257 + li;
|
|
621
|
+
putBits(FIXED_LIT_CODE[sym], FIXED_LIT_BITS[sym]);
|
|
622
|
+
if (LEN_EXTRA[li] > 0) putBits(length - LEN_BASE[li], LEN_EXTRA[li]);
|
|
623
|
+
let di = DIST_BASE.length - 1;
|
|
624
|
+
while (di > 0 && dist < DIST_BASE[di]) di--;
|
|
625
|
+
putBits(reverseBits(di, 5), 5);
|
|
626
|
+
if (DIST_EXTRA[di] > 0) putBits(dist - DIST_BASE[di], DIST_EXTRA[di]);
|
|
627
|
+
};
|
|
628
|
+
putBits(3, 3);
|
|
629
|
+
const candidates = rowStride <= 32768 ? [
|
|
630
|
+
4,
|
|
631
|
+
1,
|
|
632
|
+
rowStride,
|
|
633
|
+
32
|
|
634
|
+
] : [
|
|
635
|
+
4,
|
|
636
|
+
1,
|
|
637
|
+
32
|
|
638
|
+
];
|
|
639
|
+
let i = 0;
|
|
640
|
+
const end = raw.length;
|
|
641
|
+
while (i < end) {
|
|
642
|
+
let bestLen = 0;
|
|
643
|
+
let bestDist = 0;
|
|
644
|
+
const maxLen = Math.min(258, end - i);
|
|
645
|
+
if (maxLen >= 4) for (let ci = 0; ci < candidates.length; ci++) {
|
|
646
|
+
const d = candidates[ci];
|
|
647
|
+
if (d <= 0 || d > i || d > 32768) continue;
|
|
648
|
+
const src = i - d;
|
|
649
|
+
if (raw[src] !== raw[i] || raw[src + 1] !== raw[i + 1] || raw[src + 2] !== raw[i + 2] || raw[src + 3] !== raw[i + 3]) continue;
|
|
650
|
+
let l = 4;
|
|
651
|
+
while (l < maxLen && raw[src + l] === raw[i + l]) l++;
|
|
652
|
+
if (l > bestLen) {
|
|
653
|
+
bestLen = l;
|
|
654
|
+
bestDist = d;
|
|
655
|
+
if (l === maxLen) break;
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
if (bestLen >= 4) {
|
|
659
|
+
emitLengthDist(bestLen, bestDist);
|
|
660
|
+
i += bestLen;
|
|
661
|
+
} else {
|
|
662
|
+
const b = raw[i++];
|
|
663
|
+
putBits(FIXED_LIT_CODE[b], FIXED_LIT_BITS[b]);
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
putBits(FIXED_LIT_CODE[256], FIXED_LIT_BITS[256]);
|
|
667
|
+
if (bitCnt > 0) {
|
|
668
|
+
ensure(1);
|
|
669
|
+
out[pos++] = bitBuf & 255;
|
|
670
|
+
}
|
|
671
|
+
ensure(4);
|
|
672
|
+
writeU32BE(out, pos, adler32(raw));
|
|
673
|
+
pos += 4;
|
|
674
|
+
return out.subarray(0, pos);
|
|
675
|
+
}
|
|
676
|
+
/**
|
|
677
|
+
* Encode raw 8-bit `RGBA` pixels into a compressed standard PNG file (`Uint8Array`) in 100% pure TypeScript
|
|
678
|
+
* with zero runtime or Node built-in dependencies (works identically in Browser, Workers, Deno, Bun, Node).
|
|
679
|
+
*/
|
|
680
|
+
function encodePngRgba(width, height, rgba) {
|
|
681
|
+
const w = Math.max(1, Math.floor(width));
|
|
682
|
+
const h = Math.max(1, Math.floor(height));
|
|
683
|
+
const rowBytes = w * 4 + 1;
|
|
684
|
+
const raw = new Uint8Array(rowBytes * h);
|
|
685
|
+
for (let y = 0; y < h; y++) {
|
|
686
|
+
const dstRow = y * rowBytes;
|
|
687
|
+
raw[dstRow] = 0;
|
|
688
|
+
const srcOff = y * w * 4;
|
|
689
|
+
raw.set(rgba.subarray(srcOff, srcOff + w * 4), dstRow + 1);
|
|
690
|
+
}
|
|
691
|
+
const zlibStream = deflateFixedPixelLz77(raw, rowBytes);
|
|
692
|
+
const totalLen = 33 + (12 + zlibStream.length) + 12;
|
|
693
|
+
const png = new Uint8Array(totalLen);
|
|
694
|
+
png.set([
|
|
695
|
+
137,
|
|
696
|
+
80,
|
|
697
|
+
78,
|
|
698
|
+
71,
|
|
699
|
+
13,
|
|
700
|
+
10,
|
|
701
|
+
26,
|
|
702
|
+
10
|
|
703
|
+
], 0);
|
|
704
|
+
let p = 8;
|
|
705
|
+
const writeChunk = (type4, data) => {
|
|
706
|
+
writeU32BE(png, p, data.length);
|
|
707
|
+
png[p + 4] = type4.charCodeAt(0);
|
|
708
|
+
png[p + 5] = type4.charCodeAt(1);
|
|
709
|
+
png[p + 6] = type4.charCodeAt(2);
|
|
710
|
+
png[p + 7] = type4.charCodeAt(3);
|
|
711
|
+
png.set(data, p + 8);
|
|
712
|
+
writeU32BE(png, p + 8 + data.length, crc32(png, p + 4, 4 + data.length));
|
|
713
|
+
p += 12 + data.length;
|
|
714
|
+
};
|
|
715
|
+
const ihdr = /* @__PURE__ */ new Uint8Array(13);
|
|
716
|
+
writeU32BE(ihdr, 0, w);
|
|
717
|
+
writeU32BE(ihdr, 4, h);
|
|
718
|
+
ihdr[8] = 8;
|
|
719
|
+
ihdr[9] = 6;
|
|
720
|
+
ihdr[10] = 0;
|
|
721
|
+
ihdr[11] = 0;
|
|
722
|
+
ihdr[12] = 0;
|
|
723
|
+
writeChunk("IHDR", ihdr);
|
|
724
|
+
writeChunk("IDAT", zlibStream);
|
|
725
|
+
writeChunk("IEND", /* @__PURE__ */ new Uint8Array(0));
|
|
726
|
+
return png;
|
|
727
|
+
}
|
|
728
|
+
export { encodePngRgba as a, pixel_exports as c, derivePixelRamp as i, rgbToHex as l, compilePixelGrid as n, mixRgb as o, derivePaletteVariants as r, parseHexRgb as s, PixelCanvas as t, __exportAll as u };
|