All files / src/lib/tiles readFileToPalettes.ts

80.95% Statements 17/21
0% Branches 0/1
80% Functions 4/5
78.95% Lines 15/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 463x 3x 3x 3x         3x   3x     2x 2x     3x                                 3x 2x 2x 2x 2x     2x        
import { readFile } from "fs-extra";
import { readFileToIndexedImage } from "lib/tiles/readFileToTiles";
import { PNG } from "pngjs";
import {
  AutoPaletteResult,
  autoPalette,
  autoPaletteUsingTiles,
} from "shared/lib/tiles/autoColor";
import { tileDataIndexFn } from "shared/lib/tiles/tileData";
 
export const readFileToPalettes = async (
  filename: string
): Promise<AutoPaletteResult> => {
  const colorPNG = await readPNG(filename);
  return autoPalette(colorPNG.width, colorPNG.height, colorPNG.data);
};
 
export const readFileToPalettesUsingTiles = async (
  filename: string,
  tilesFileName: string
): Promise<AutoPaletteResult> => {
  const colorPNG = await readPNG(filename);
  const indexedImage = await readFileToIndexedImage(
    tilesFileName,
    tileDataIndexFn
  );
  return autoPaletteUsingTiles(
    colorPNG.width,
    colorPNG.height,
    colorPNG.data,
    indexedImage
  );
};
 
const readPNG = async (filename: string): Promise<PNG> => {
  const fileData = await readFile(filename);
  return new Promise((resolve, reject) => {
    new PNG().parse(fileData, (err, data) => {
      Iif (err) {
        return reject(err);
      }
      resolve(data);
    });
  });
};