generated from egoist/ts-lib-starter
-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(deps): replace colorthief with fast-average-color
- Loading branch information
1 parent
91365b0
commit 6ea8597
Showing
6 changed files
with
28 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); | ||
} |