-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(page): generate color on server side
- Loading branch information
1 parent
4e2895d
commit d539ec5
Showing
6 changed files
with
214 additions
and
236 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
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
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,44 +1,26 @@ | ||
import ColorThief from 'colorthief'; | ||
import fetch from 'node-fetch'; | ||
import { extractColors } from 'extract-colors'; | ||
import getPixels from 'get-pixels'; | ||
import { type rgbColor } from '../types/color.types'; | ||
|
||
// const createImage = (src: string): Promise<HTMLImageElement> => { | ||
// const image = document.createElement('img'); | ||
// image.src = src; | ||
// image.style.display = 'none'; | ||
// globalThis.window.document.body.appendChild(image); | ||
|
||
// return new Promise((resolve, reject) => { | ||
// if (image.complete) { | ||
// return resolve(image); | ||
// } | ||
|
||
// image.onload = () => { | ||
// resolve(image); | ||
// }; | ||
|
||
// image.onerror = () => { | ||
// reject(); | ||
// } | ||
// }); | ||
// } | ||
|
||
export const getImageColors = async (src: string): Promise<{ primaryColor: rgbColor; complementaryColor: rgbColor; } | null> => { | ||
const image = await fetch(src) | ||
.then(response => { | ||
if (!response.ok) { | ||
return null; | ||
} | ||
|
||
return response.buffer(); | ||
}).catch(() => null); | ||
|
||
if (!image) { | ||
return {primaryColor: null, complementaryColor: null } | ||
} | ||
|
||
const [primaryColor, complementaryColor] = ColorThief.getPalette(image); | ||
|
||
return {primaryColor, complementaryColor }; | ||
} | ||
|
||
export const getImageColors = async ( | ||
src: string | ||
): Promise<{ primaryColor: rgbColor | null; complementaryColor: rgbColor | null }> => | ||
new Promise((resolve) => { | ||
getPixels(src, async (err, pixels) => { | ||
if (err) { | ||
return resolve({ primaryColor: null, complementaryColor: null }); | ||
} | ||
|
||
const data = [...pixels.data]; | ||
const [width, height] = pixels.shape; | ||
|
||
const { primaryColor, complementaryColor } = await extractColors({ data, width, height }) | ||
.then(([primary, secondary]) => ({ | ||
primaryColor: [primary.red, primary.green, primary.blue] as rgbColor, | ||
complementaryColor: [secondary.red, secondary.green, secondary.blue] as rgbColor, | ||
})) | ||
.catch(() => ({ primaryColor: null, complementaryColor: null })); | ||
|
||
return resolve({ primaryColor, complementaryColor }); | ||
}); | ||
}); |
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
Oops, something went wrong.