Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139import { describe, expect, it } from "vitest";import { MmcqQuantizer, extractPalette, rgbToOklch, validateOptions } from "colorthief/internals";
import { deriveAccent, pickAccent, type Swatch } from "./accent";
/** * The picker against colourthief's real quantizer, with pixels in one end and a colour * out the other. * * `accent.test.ts` judges the picker on hand-written swatches, which is the only way to * ask "what would it do with a colour no cover has ever produced" — and the only way to * be quietly wrong, too, because a fixture asserts only what its author thought to write * down. These four covers are here for the assumptions the fixtures cannot make visible: * that `proportion` is the share of the sampled pixels rather than a count, that a colour * held by 4% of a 96px image still survives twenty boxes, and that the wall is the colour * that comes back largest rather than the one that comes back first. * * The quantizer is driven directly rather than through `getPalette` because a real cover * gets here through a fetch, a decode and a canvas, none of which exist under jsdom. */
const OPTIONS = validateOptions({ // The same four the wiring asks the real thing for. If they drift apart, these tests // are describing a palette the app never sees. colorCount: 20, quality: 2, colorSpace: "oklch", ignoreWhite: true,});
/** A swatch as the quantizer hands it over: the picker's two members, plus the hex. */interface Quantized extends Swatch { hex(): string;}
function cover(fill: (x: number, y: number) => [number, number, number], size = 96): Quantized[] { const data = new Uint8ClampedArray(size * size * 4); for (let y = 0; y < size; y++) { for (let x = 0; x < size; x++) { const [r, g, b] = fill(x, y); const at = (y * size + x) * 4; data[at] = r; data[at + 1] = g; data[at + 2] = b; data[at + 3] = 255; } } const palette = extractPalette(data, size, size, OPTIONS, new MmcqQuantizer()); if (!palette) throw new Error("the quantizer returned nothing for a synthetic cover"); return palette;}
/** * A swatch the picker returned, quoted in the hex the quantizer gave it. * * By identity rather than by cast: the picker's `Swatch` does not claim `hex()` — it is * the seam the hand-written fixtures are poured through, and a method on it would be a * method the fixtures have to fake — so the palette this file built is consulted for the * colour it says the returned swatch is. */const quoted = (palette: Quantized[], swatch: Swatch | undefined): string | undefined => palette.find((color) => color === swatch)?.hex();
const hueOf = (hex: string): number => { const n = Number.parseInt(hex.slice(1), 16); return rgbToOklch((n >> 16) & 255, (n >> 8) & 255, n & 255).h;};
describe("the picker against the quantizer", () => { it("still sees the small print on a mostly-dark sleeve", () => { // 96² pixels, of which 400 are the logo: two in twenty survive MMCQ's boxes, and the // whole feature rests on them doing so. A picker that only ever saw the wall would // paint the app the colour of a dark sleeve and never change. const palette = cover((x, y) => x > 60 && x < 80 && y > 60 && y < 80 ? [232, 88, 12] : [16, 18, 30], ); expect(palette.length).toBeGreaterThanOrEqual(2); const pick = pickAccent(palette); expect(quoted(palette, pick?.color)).toBe("#e8580c"); expect(deriveAccent(pick!).tokens.accent).toMatch(/^#f/); // the orange, lifted to ink });
it("names as the wall the colour that is largest, not the one that is first", () => { const palette = cover((x, y) => x > 20 && x < 60 && y > 20 && y < 60 ? [20, 140, 140] : [110, 110, 112], ); const pick = pickAccent(palette); // The grey is 84% of the cover and the teal is the accent; the other way round would // mean the app wears a flat grey because it was the first box the quantizer filled. expect(quoted(palette, pick?.wall)).toBe("#6e6e70"); expect(quoted(palette, pick?.color)).toBe("#148c8c"); });
it("finds the wall by count, because the palette is not promised in that order", () => { // Two unique colours, so colourthief takes its short-circuit path: under `colorCount` // distinct colours it returns them in the order they were first seen, not the priority // queue's order by population (`internals.browser.js`, the `uniqueColors.length <= // opts.colorCount` branch). Here the scarlet stripe is a third of the cover and the // navy is two thirds, and the scarlet is seen first — so `palette[0]` is the minority, // and a picker that called it the wall would call a third of the sleeve the wall. const palette = cover((x) => (x % 3 === 0 ? [200, 40, 40] : [30, 40, 60])); expect(palette).toHaveLength(2); expect(palette[0]!.proportion).toBeLessThan(palette[1]!.proportion);
const pick = pickAccent(palette); expect(quoted(palette, pick?.wall)).toBe("#1e283c"); // the navy, the larger of the two expect(quoted(palette, pick?.color)).toBe("#c82828"); });
it("refuses a monochrome sleeve outright", () => { // Real greys from a real quantizer, not the two hand-written ones in the fixtures: // this is the case where the boxes pick up a little chroma from dithering and the // chroma floor has to be set above it. const palette = cover((x, y) => { const v = ((x * 7 + y * 3) % 200) + 20; return [v, v, v + 2]; }); expect(palette.length).toBeGreaterThan(4); expect(pickAccent(palette)).toBeNull(); });
it("will not hand the app the seek bar's own gold", () => { // A gold-leaf sleeve: every colour the quantizer returns is in the gold family, so // the only thing standing between the app and an accent the A–B region disappears // into is this check. const accent = accentOf((x, y) => [200 + ((x + y) % 40), 160 + ((x * y) % 30), 40 + (y % 30)]); expect(accent).not.toBeNull(); expect(accent).not.toBe("#e3b341"); // Hue 85 is the region's gold; an accent within a few degrees of it at that // saturation is the same colour under a different lamp. expect(Math.abs(hueOf(accent!) - 85)).toBeGreaterThan(15); });});
const accentOf = (fill: (x: number, y: number) => [number, number, number]): string | null => { const pick = pickAccent(cover(fill)); return pick ? deriveAccent(pick).tokens.accent : null;};