| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- import { describe, expect, it } from "vitest";
- import type { ShaderDef } from "@nengine/core";
- import {
- filterParams,
- BLUR_DEFAULT_STRENGTH,
- GRAYSCALE_DEFAULT_FACTOR,
- BRIGHTNESS_DEFAULT_FACTOR,
- HUE_DEFAULT_DEGREES,
- } from "../src/filters.js";
- const def = (overrides: Partial<ShaderDef> = {}): ShaderDef => ({ id: "fx", filter: "blur", ...overrides });
- describe("filterParams (pure shader-def → numeric/color parameters)", () => {
- it("maps blur with a strength param", () => {
- expect(filterParams(def({ filter: "blur", params: { strength: 4 } }))).toEqual({ kind: "blur", strength: 4 });
- });
- it("defaults blur strength when absent", () => {
- expect(filterParams(def({ filter: "blur" }))).toEqual({ kind: "blur", strength: BLUR_DEFAULT_STRENGTH });
- });
- it("maps tint from a #rrggbb color string", () => {
- expect(filterParams(def({ filter: "tint", params: { color: "#2f3b4c" } }))).toEqual({ kind: "tint", color: "#2f3b4c" });
- });
- it("maps tint from r/g/b components (clamped to 0-255)", () => {
- expect(filterParams(def({ filter: "tint", params: { r: 47, g: 59, b: 76 } }))).toEqual({ kind: "tint", color: "#2f3b4c" });
- expect(filterParams(def({ filter: "tint", params: { r: 300, g: -5, b: 0 } }))).toEqual({ kind: "tint", color: "#ff0000" });
- });
- it("defaults tint to white when no color params are given", () => {
- expect(filterParams(def({ filter: "tint" }))).toEqual({ kind: "tint", color: "#ffffff" });
- });
- it("maps grayscale with a factor", () => {
- expect(filterParams(def({ filter: "grayscale", params: { factor: 0.7 } }))).toEqual({ kind: "grayscale", factor: 0.7 });
- expect(filterParams(def({ filter: "grayscale" }))).toEqual({ kind: "grayscale", factor: GRAYSCALE_DEFAULT_FACTOR });
- });
- it("maps sepia without params", () => {
- expect(filterParams(def({ filter: "sepia" }))).toEqual({ kind: "sepia" });
- });
- it("maps hue with degrees", () => {
- expect(filterParams(def({ filter: "hue", params: { degrees: 120 } }))).toEqual({ kind: "hue", degrees: 120 });
- expect(filterParams(def({ filter: "hue" }))).toEqual({ kind: "hue", degrees: HUE_DEFAULT_DEGREES });
- });
- it("maps brightness with a factor", () => {
- expect(filterParams(def({ filter: "brightness", params: { factor: 1.2 } }))).toEqual({ kind: "brightness", factor: 1.2 });
- expect(filterParams(def({ filter: "brightness" }))).toEqual({ kind: "brightness", factor: BRIGHTNESS_DEFAULT_FACTOR });
- });
- it("ignores non-numeric params (falls back to defaults)", () => {
- expect(filterParams(def({ filter: "blur", params: { strength: "high" } }))).toEqual({ kind: "blur", strength: BLUR_DEFAULT_STRENGTH });
- });
- });
|