@city41/gba-convertpng 0.0.28 → 0.0.30
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/background.js +19 -12
- package/dist/canvas.js +6 -4
- package/package.json +1 -1
package/dist/background.js
CHANGED
|
@@ -27,8 +27,8 @@ function convertTileTo15Bit(rawTile) {
|
|
|
27
27
|
return data15;
|
|
28
28
|
}
|
|
29
29
|
function getTileIndex(tilePalette, tile) {
|
|
30
|
-
const index = tilePalette.findIndex(t => {
|
|
31
|
-
return t.join(
|
|
30
|
+
const index = tilePalette.findIndex((t) => {
|
|
31
|
+
return t.join("-") === tile.join("-");
|
|
32
32
|
});
|
|
33
33
|
if (index > -1) {
|
|
34
34
|
return index;
|
|
@@ -36,7 +36,7 @@ function getTileIndex(tilePalette, tile) {
|
|
|
36
36
|
tilePalette.push([...tile]);
|
|
37
37
|
return tilePalette.length - 1;
|
|
38
38
|
}
|
|
39
|
-
// looks through all the palettes and combines multiple palettes into one
|
|
39
|
+
// looks through all the palettes and combines multiple palettes into one
|
|
40
40
|
// based on how much room they have.
|
|
41
41
|
// example, palette-a has 4 colors, palette-b has 6, result is a palette with 10 colors
|
|
42
42
|
// possibly the two palettes share colors, only one copy of each color will be preserved
|
|
@@ -46,12 +46,12 @@ function combinePalettes(palettes) {
|
|
|
46
46
|
if (palettes.length <= 1) {
|
|
47
47
|
return palettes;
|
|
48
48
|
}
|
|
49
|
-
const sortedPalettes = (0, lodash_1.sortBy)(palettes, p => p.length);
|
|
49
|
+
const sortedPalettes = (0, lodash_1.sortBy)(palettes, (p) => p.length);
|
|
50
50
|
let firstPalette = sortedPalettes[0];
|
|
51
51
|
const remainingPalettes = [];
|
|
52
52
|
for (let p = 1; p < sortedPalettes.length; ++p) {
|
|
53
53
|
const otherPalette = sortedPalettes[p];
|
|
54
|
-
const otherPaletteUniqueColors = otherPalette.filter(c => !firstPalette.includes(c));
|
|
54
|
+
const otherPaletteUniqueColors = otherPalette.filter((c) => !firstPalette.includes(c));
|
|
55
55
|
if (firstPalette.length + otherPaletteUniqueColors.length < 16) {
|
|
56
56
|
firstPalette = firstPalette.concat(otherPaletteUniqueColors);
|
|
57
57
|
}
|
|
@@ -69,7 +69,7 @@ function buildMap(tiles, bgWidthPx, bgHeightPx) {
|
|
|
69
69
|
for (let y = 0; y < bgHeightT; ++y) {
|
|
70
70
|
for (let x = 0; x < bgWidthT; ++x) {
|
|
71
71
|
const tile = tiles[y * bgWidthT + x];
|
|
72
|
-
map.push(tile.paletteIndex << 12 | tile.tileIndex);
|
|
72
|
+
map.push((tile.paletteIndex << 12) | tile.tileIndex);
|
|
73
73
|
}
|
|
74
74
|
}
|
|
75
75
|
return map;
|
|
@@ -79,17 +79,17 @@ function getGBATile(data15, palette) {
|
|
|
79
79
|
for (let p = 0; p < data15.length; p += 2) {
|
|
80
80
|
const highNibble = palette.indexOf(data15[p + 1]);
|
|
81
81
|
const lowNibble = palette.indexOf(data15[p]);
|
|
82
|
-
const byte = (highNibble & 0xf) << 4 | (lowNibble & 0xf);
|
|
82
|
+
const byte = ((highNibble & 0xf) << 4) | (lowNibble & 0xf);
|
|
83
83
|
gbaTile.push(byte);
|
|
84
84
|
}
|
|
85
85
|
return gbaTile;
|
|
86
86
|
}
|
|
87
87
|
function findMatchingPalette(data15, palettes) {
|
|
88
|
-
const foundPalette = palettes.find(palette => {
|
|
89
|
-
return data15.every(c => palette.includes(c));
|
|
88
|
+
const foundPalette = palettes.find((palette) => {
|
|
89
|
+
return data15.every((c) => palette.includes(c));
|
|
90
90
|
});
|
|
91
91
|
if (!foundPalette) {
|
|
92
|
-
throw new Error(
|
|
92
|
+
throw new Error("findMatchingPalette: failed to find a palette");
|
|
93
93
|
}
|
|
94
94
|
return foundPalette;
|
|
95
95
|
}
|
|
@@ -101,11 +101,15 @@ function padPalette(palette) {
|
|
|
101
101
|
}
|
|
102
102
|
async function processBackground(bg) {
|
|
103
103
|
let canvas = await (0, canvas_1.createCanvasFromPath)(bg.file);
|
|
104
|
+
const colorCount = (0, palette_1.extractPalette)(canvas, false).length;
|
|
105
|
+
if (bg.forcePalette && colorCount > 15) {
|
|
106
|
+
throw new Error(`Background with forcedPalette set, but image has ${colorCount} colors, ${bg.file}`);
|
|
107
|
+
}
|
|
104
108
|
if (typeof bg.reduceColors === "undefined" || bg.reduceColors === true) {
|
|
105
109
|
canvas = await (0, canvas_1.reduceColors)(canvas, 16);
|
|
106
110
|
}
|
|
107
111
|
canvas = (0, canvas_1.roundUpToTileSize)(canvas);
|
|
108
|
-
const ctx = canvas.getContext(
|
|
112
|
+
const ctx = canvas.getContext("2d");
|
|
109
113
|
const tiles = [];
|
|
110
114
|
const tilePalette = [];
|
|
111
115
|
let palettes = [];
|
|
@@ -144,13 +148,16 @@ async function processBackground(bg) {
|
|
|
144
148
|
const paletteData = palettes.map(padPalette).flat(1);
|
|
145
149
|
const paletteCount = palettes.length;
|
|
146
150
|
const map = buildMap(tiles, canvas.width, canvas.height);
|
|
151
|
+
if (typeof bg.transparentColor === "number") {
|
|
152
|
+
paletteData[0] = bg.transparentColor;
|
|
153
|
+
}
|
|
147
154
|
return {
|
|
148
155
|
background: bg,
|
|
149
156
|
canvas,
|
|
150
157
|
map,
|
|
151
158
|
palette: paletteData,
|
|
152
159
|
paletteCount,
|
|
153
|
-
tiles: tileData
|
|
160
|
+
tiles: tileData,
|
|
154
161
|
};
|
|
155
162
|
}
|
|
156
163
|
//# sourceMappingURL=background.js.map
|
package/dist/canvas.js
CHANGED
|
@@ -108,10 +108,12 @@ function findNearestColor(pixel, palette) {
|
|
|
108
108
|
b: pixel[2],
|
|
109
109
|
};
|
|
110
110
|
const nearestResult = (0, nearest_color_1.default)(pixelInput, colorsInput);
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
111
|
+
const nearestPixel = new Array(4);
|
|
112
|
+
nearestPixel[0] = nearestResult.rgb.r;
|
|
113
|
+
nearestPixel[1] = nearestResult.rgb.g;
|
|
114
|
+
nearestPixel[2] = nearestResult.rgb.b;
|
|
115
|
+
nearestPixel[3] = 255;
|
|
116
|
+
return Uint8ClampedArray.from(nearestPixel);
|
|
115
117
|
}
|
|
116
118
|
function isMagenta(pixel) {
|
|
117
119
|
return pixel[0] === 255 && pixel[1] === 0 && pixel[2] === 255;
|