From 3d7ee103282213e3e0da8a10044cc2c970b06669 Mon Sep 17 00:00:00 2001 From: jeffvli Date: Tue, 24 Jun 2025 18:27:11 -0700 Subject: [PATCH] add standalone fast-average-color function --- src/renderer/hooks/use-fast-average-color.tsx | 27 ++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/src/renderer/hooks/use-fast-average-color.tsx b/src/renderer/hooks/use-fast-average-color.tsx index 9afaf6de..4fc710e9 100644 --- a/src/renderer/hooks/use-fast-average-color.tsx +++ b/src/renderer/hooks/use-fast-average-color.tsx @@ -1,16 +1,35 @@ import { FastAverageColor } from 'fast-average-color'; import { useEffect, useRef, useState } from 'react'; +export const getFastAverageColor = async (args: { + algorithm?: 'dominant' | 'simple' | 'sqrt'; + src: string; +}) => { + const fac = new FastAverageColor(); + const background = await fac.getColorAsync(args.src, { + algorithm: args.algorithm || 'dominant', + ignoredColor: [ + [255, 255, 255, 255, 90], // White + [0, 0, 0, 255, 30], // Black + [0, 0, 0, 0, 40], // Transparent + ], + mode: 'speed', + }); + + return background.rgb; +}; + export const useFastAverageColor = (args: { algorithm?: 'dominant' | 'simple' | 'sqrt'; + default?: string; id?: string; src?: null | string; srcLoaded?: boolean; }) => { - const { algorithm, id, src, srcLoaded } = args; + const { algorithm, default: defaultColor, id, src, srcLoaded } = args; const idRef = useRef(id); - const [background, setBackground] = useState(undefined); + const [background, setBackground] = useState(defaultColor); useEffect(() => { const fac = new FastAverageColor(); @@ -23,7 +42,7 @@ export const useFastAverageColor = (args: { [0, 0, 0, 255, 30], // Black [0, 0, 0, 0, 40], // Transparent ], - mode: 'precision', + mode: 'speed', }) .then((color) => { idRef.current = id; @@ -44,5 +63,7 @@ export const useFastAverageColor = (args: { }; }, [algorithm, srcLoaded, src, id]); + console.log('background :>> ', background); + return { background, colorId: idRef.current }; };