Skip to content

Commit af8374a

Browse files
committed
🔧 fix gif colormap
1 parent 651a2bc commit af8374a

File tree

2 files changed

+48
-18
lines changed

2 files changed

+48
-18
lines changed

‎packages/gif-creator/__tests__/createGif.spec.ts‎

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
1-
import * as fs from "fs";
2-
import * as path from "path";
3-
import { it, expect } from "bun:test";
4-
import { type AnimationOptions, type DrawOptions, createGif } from "..";
1+
import { expect, it } from "bun:test";
2+
import { getBestRoute } from "@snk/solver/getBestRoute";
53
import * as grids from "@snk/types/__fixtures__/grid";
64
import { snake3 as snake } from "@snk/types/__fixtures__/snake";
75
import { createSnakeFromCells, nextSnake } from "@snk/types/snake";
8-
import { getBestRoute } from "@snk/solver/getBestRoute";
6+
import * as fs from "fs";
7+
import * as path from "path";
8+
import { basePalettes } from "../../action/palettes";
9+
import { type AnimationOptions, createGif, type DrawOptions } from "..";
910

1011
const upscale = 1;
1112
const drawOptions: DrawOptions = {
1213
sizeDotBorderRadius: 2 * upscale,
1314
sizeCell: 16 * upscale,
1415
sizeDot: 12 * upscale,
15-
colorBackground: "#ffffff",
16-
colorDotBorder: "#1b1f230a",
17-
colorDots: { 1: "#9be9a8", 2: "#40c463", 3: "#30a14e", 4: "#216e39" },
18-
colorEmpty: "#ebedf0",
19-
colorSnake: "purple",
16+
...basePalettes["github-dark"],
2017
};
2118

2219
const animationOptions: AnimationOptions = {

‎packages/gif-creator/index.ts‎

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
import fs from "node:fs";
2-
import path from "node:path";
31
import { execFileSync } from "node:child_process";
2+
import fs from "node:fs";
43
import { tmpdir } from "node:os";
5-
import { createCanvas } from "canvas";
6-
import { Grid, copyGrid, Color } from "@snk/types/grid";
7-
import { Snake } from "@snk/types/snake";
4+
import path from "node:path";
85
import {
96
type Options as DrawOptions,
107
drawLerpWorld,
118
getCanvasWorldSize,
129
} from "@snk/draw/drawWorld";
13-
import type { Point } from "@snk/types/point";
1410
import { step } from "@snk/solver/step";
15-
import gifsicle from "gifsicle";
11+
import { Color, copyGrid, Grid } from "@snk/types/grid";
12+
import type { Point } from "@snk/types/point";
13+
import { Snake } from "@snk/types/snake";
14+
import { createCanvas } from "canvas";
1615
// @ts-ignore
1716
import GIFEncoder from "gif-encoder-2";
17+
import gifsicle from "gifsicle";
1818

1919
export type { Options as DrawOptions } from "@snk/draw/drawWorld";
2020

@@ -78,6 +78,38 @@ export const createGif = async (
7878
const outFileName = path.join(dir, "out.gif");
7979
const optimizedFileName = path.join(dir, "out.optimized.gif");
8080

81+
// generate palette file
82+
const paletteFileName = path.join(dir, "palette.txt");
83+
{
84+
const colors = [
85+
drawOptions.colorBackground,
86+
drawOptions.colorEmpty,
87+
drawOptions.colorSnake,
88+
drawOptions.colorDotBorder,
89+
...Object.values(drawOptions.colorDots),
90+
].filter(Boolean) as string[];
91+
92+
const canvas = createCanvas(colors.length, 1);
93+
const ctx = canvas.getContext("2d") as any as CanvasRenderingContext2D;
94+
for (let i = colors.length; i--; ) {
95+
ctx.fillStyle = colors[i];
96+
ctx.fillRect(i, 0, 1, 1);
97+
}
98+
99+
const imgData = ctx.getImageData(0, 0, colors.length, 1);
100+
101+
fs.writeFileSync(
102+
paletteFileName,
103+
Array.from({ length: colors.length }, (_, i) =>
104+
[
105+
imgData.data[i * 4 + 0],
106+
imgData.data[i * 4 + 1],
107+
imgData.data[i * 4 + 2],
108+
].join(" "),
109+
).join("\n"),
110+
);
111+
}
112+
81113
encoder.finish();
82114
fs.writeFileSync(outFileName, encoder.out.getData());
83115

@@ -87,7 +119,8 @@ export const createGif = async (
87119
//
88120
"--optimize=3",
89121
"--color-method=diversity",
90-
"--colors=16",
122+
`--use-colormap=${paletteFileName}`,
123+
// "--colors=16",
91124
outFileName,
92125
["--output", optimizedFileName],
93126
].flat(),

0 commit comments

Comments
 (0)