Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 9 additions & 17 deletions src/common/image/extract_color.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Swatch, Vec3 } from "@vibrant/color";
import { BasicPipeline } from "@vibrant/core/lib/pipeline";
import MMCQ from "@vibrant/quantizer-mmcq";
import Vibrant from "node-vibrant/lib/browser";
// We import the minified bundle because the unminified bundle
// has some quirks that break wds. See #7784 for unminified version.
import Vibrant from "node-vibrant/dist/vibrant";
import type { Swatch, Vec3 } from "@vibrant/color";
import { getRGBContrastRatio } from "../color/rgb";

const CONTRAST_RATIO = 4.5;
Expand Down Expand Up @@ -104,23 +104,15 @@ const customGenerator = (colors: Swatch[]) => {
}

return {
foreground: new Swatch(foregroundColor, 0),
// We can't import Swatch constructor from the minified bundle, take it from background color.
// @ts-expect-error
foreground: new backgroundColor.constructor(foregroundColor, 0),
background: backgroundColor,
};
};

Vibrant.use(
new BasicPipeline().filter
.register(
"default",
(r: number, g: number, b: number, a: number) =>
a >= 125 && !(r > 250 && g > 250 && b > 250)
)
.quantizer.register("mmcq", MMCQ)
// Our generator has different output
// @ts-expect-error
.generator.register("default", customGenerator)
);
// Set our custom generator as the default.
Vibrant._pipeline.generator.register("default", customGenerator);

export const extractColors = (url: string, downsampleColors = 16) =>
new Vibrant(url, {
Expand Down