-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
31 lines (29 loc) · 1.28 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
* Extracts dominant colors from an image source using WebGPU API with various algorithms.
*
* @param {ImageBitmapSource} imageSource - The image source to process.
* @param {number} colorCount - The number of dominant colors to extract.
* @param {string} algorithm - The algorithm to use for color extraction. Possible values are:
* - `wu`: Uses the Wu algorithm.
* - `kmeans`: Uses the K-means algorithm.
* - `celebi`: Uses the Celebi algorithm.
* @returns {Promise<Array<string>>} A promise that resolves to an array of dominant colors.
* @throws {Error} Throws an error if an unknown algorithm is specified.
*/
export async function extractDominantColors(imageSource, colorCount, algorithm) {
let extractFunction;
switch (algorithm) {
case 'wu':
extractFunction = (await import('./wu/index.js')).extractDominantColorsWu;
break;
case 'kmeans':
extractFunction = (await import('./kmeans/index.js')).extractDominantColorsKMeans;
break;
case 'celebi':
extractFunction = (await import('./celebi/index.js')).extractDominantColorsCelebi;
break;
default:
throw new Error(`Unknown algorithm: ${algorithm}`);
}
return extractFunction(imageSource, colorCount);
}