Skip to content

Commit

Permalink
perf(deps): replace colorthief with fast-average-color
Browse files Browse the repository at this point in the history
  • Loading branch information
SevenOutman committed Jan 6, 2023
1 parent 91365b0 commit 6ea8597
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 129 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@
"react": "^18.2.0"
},
"dependencies": {
"@pioug/colorthief": "^3.0.1",
"clsx": "^1.2.1",
"fast-average-color": "^9.3.0",
"use-sync-external-store": "^1.2.0"
}
}
104 changes: 7 additions & 97 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions src/hooks/useThemeColor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { useThemeColor } from "./useThemeColor";

vi.mock("../utils/getImageColor", () => {
return {
getImageColor: vi.fn(() => Promise.resolve(["r", "g", "b"])),
getImageColor: vi.fn(() => Promise.resolve("calculated")),
};
});

Expand All @@ -28,7 +28,7 @@ describe("`song.theme` is not specified", () => {
expect(result.current).toBe(defaultThemeColor);

await waitFor(() => {
expect(result.current).toBe("rgb(r,g,b)");
expect(result.current).toBe("calculated");
});
});
});
Expand Down Expand Up @@ -64,7 +64,7 @@ describe('`song.theme` is "auto"', () => {
expect(result.current).toBe("green");

await waitFor(() => {
expect(result.current).toBe("rgb(r,g,b)");
expect(result.current).toBe("calculated");
});
});

Expand All @@ -74,7 +74,7 @@ describe('`song.theme` is "auto"', () => {
expect(result.current).toBe(defaultThemeColor);

await waitFor(() => {
expect(result.current).toBe("rgb(r,g,b)");
expect(result.current).toBe("calculated");
});
});
});
4 changes: 2 additions & 2 deletions src/hooks/useThemeColor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ export function useThemeColor(
if (shouldUseColorThief(song, fallback)) {
const coverUrl = song!.cover!;

getImageColor(coverUrl).then(([r, g, b]) => {
getImageColor(coverUrl).then((hex) => {
setCoverColorMap((prev) => ({
...prev,
[coverUrl]: `rgb(${r},${g},${b})`,
[coverUrl]: hex,
}));
});
}
Expand Down
9 changes: 0 additions & 9 deletions src/pioug__colorthief.d.ts

This file was deleted.

30 changes: 14 additions & 16 deletions src/utils/getImageColor.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
import ColorThief from "@pioug/colorthief";
import { FastAverageColor } from "fast-average-color";

let colorThiefSingleton: ColorThief;
let facSingleton: FastAverageColor;

export function getImageColor(
imageUrl: string
): Promise<[r: number, g: number, b: number]> {
const colorThief =
colorThiefSingleton ?? (colorThiefSingleton = new ColorThief());
/**
* @returns hex color
*/
export function getImageColor(imageUrl: string): Promise<string> {
const fac = facSingleton ?? (facSingleton = new FastAverageColor());

return new Promise((resolve) => {
const img = new Image();

img.onload = () => {
resolve(colorThief.getColor(img));
};
img.crossOrigin = "anonymous";
img.src = imageUrl;
});
return fac
.getColorAsync(imageUrl, {
ignoredColor: [255, 255, 255, 255], // Ignore white because the default background color of APlayer is white
})
.then((color) => {
return color.hex;
});
}

0 comments on commit 6ea8597

Please sign in to comment.