2026-05-24 21:05:21 +07:00
|
|
|
import { describe, expect, it } from "vitest";
|
|
|
|
|
import { buildBrushDabs, clampBrushOptions, hexToRgb } from "./brush-painter";
|
|
|
|
|
|
|
|
|
|
describe("brush painter pure helpers", () => {
|
|
|
|
|
it("parses valid hex colors and falls back to black", () => {
|
|
|
|
|
expect(hexToRgb("#ff8040")).toEqual({ r: 255, g: 128, b: 64 });
|
|
|
|
|
expect(hexToRgb("00aaee")).toEqual({ r: 0, g: 170, b: 238 });
|
|
|
|
|
expect(hexToRgb("nope")).toEqual({ r: 0, g: 0, b: 0 });
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("clamps invalid brush options", () => {
|
2026-05-31 03:03:49 +07:00
|
|
|
expect(
|
|
|
|
|
clampBrushOptions({ color: "#fff", size: 0, opacity: 2, hardness: -1 }),
|
|
|
|
|
).toEqual({
|
2026-05-24 21:05:21 +07:00
|
|
|
color: "#fff",
|
|
|
|
|
size: 1,
|
|
|
|
|
opacity: 1,
|
|
|
|
|
hardness: 0,
|
|
|
|
|
});
|
2026-05-31 03:03:49 +07:00
|
|
|
expect(
|
|
|
|
|
clampBrushOptions({
|
|
|
|
|
color: "#000",
|
|
|
|
|
size: Number.NaN,
|
|
|
|
|
opacity: Number.NaN,
|
|
|
|
|
hardness: Number.NaN,
|
|
|
|
|
}),
|
|
|
|
|
).toEqual({
|
2026-05-24 21:05:21 +07:00
|
|
|
color: "#000",
|
|
|
|
|
size: 1,
|
|
|
|
|
opacity: 1,
|
|
|
|
|
hardness: 1,
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("interpolates brush dabs including endpoints", () => {
|
|
|
|
|
expect(buildBrushDabs(0, 0, 4, 0, 4)).toEqual([
|
|
|
|
|
{ x: 0, y: 0 },
|
|
|
|
|
{ x: 1, y: 0 },
|
|
|
|
|
{ x: 2, y: 0 },
|
|
|
|
|
{ x: 3, y: 0 },
|
|
|
|
|
{ x: 4, y: 0 },
|
|
|
|
|
]);
|
2026-05-31 03:03:49 +07:00
|
|
|
expect(buildBrushDabs(2, 3, 2, 3, 10)).toEqual([
|
|
|
|
|
{ x: 2, y: 3 },
|
|
|
|
|
{ x: 2, y: 3 },
|
|
|
|
|
]);
|
2026-05-24 21:05:21 +07:00
|
|
|
});
|
|
|
|
|
});
|