Skip to content

Commit

Permalink
feat(page): generate color on server side
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-heimbuch committed Dec 8, 2024
1 parent 4e2895d commit d539ec5
Show file tree
Hide file tree
Showing 6 changed files with 214 additions and 236 deletions.
7 changes: 4 additions & 3 deletions apps/page/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,14 @@
"reselect": "4.1.8",
"scroll-into-view-if-needed": "3.1.0",
"vue-i18n": "9.13.1",
"@picocss/pico": "2.0.6",
"@colour-extractor/colour-extractor": "1.0.1"
"extract-colors": "4.1.1",
"get-pixels": "3.3.3"
},
"devDependencies": {
"@types/lodash-es": "4.17.12",
"@types/redux-actions": "2.6.5",
"tailwindcss": "3.4.13",
"typescript": "5.3.2"
"typescript": "5.3.2",
"@types/get-pixels": "3.3.4"
}
}
25 changes: 0 additions & 25 deletions apps/page/src/features/PageCover.vue

This file was deleted.

4 changes: 1 addition & 3 deletions apps/page/src/layouts/Layout.astro
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import Search from '../features/Search.vue';
import PageFooter from '../features/PageFooter.vue';
import LoadingBar from '../features/LoadingBar.vue';
import Subscribe from '../features/subscribe/Subscribe.vue';
import PageCover from '../features/PageCover.vue';
import Colors from '../features/Colors.vue';
import { store } from '../logic';
import { getLanguage } from '../i18n';
Expand All @@ -24,15 +23,14 @@ const lang = getLanguage();
<meta name="description" content="Astro description" />
<meta name="viewport" content="width=device-width" />
<link rel="icon" type="image/svg+xml" href={favicon} />
<link rel="manifest" href="/manifest.json">
<meta name="generator" content={Astro.generator} />
<title>{title}</title>
<meta name="description" content={description} />
<script is:inline define:vars={{ state }}>window.REDUX_STATE = state;</script>
<ViewTransitions />
</head>
<body>
<Colors client:only="vue" />
<Colors />
<LoadingBar client:only="vue" />
<PageHeader client:idle transition:persist="header" />
<main transition:animate="fade">
Expand Down
66 changes: 24 additions & 42 deletions apps/page/src/lib/color.ts
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 });
});
});
3 changes: 1 addition & 2 deletions apps/page/src/logic/sagas/layout.sagas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ export default function ({
return;
}

const {primaryColor, complementaryColor} = yield getImageColors(poster);
console.log('call!', {primaryColor, complementaryColor});
const { primaryColor, complementaryColor } = yield getImageColors(poster);
const primary = tailwindColorTokens(primaryColor);
const complementary = tailwindColorTokens(complementaryColor);

Expand Down
Loading

0 comments on commit d539ec5

Please sign in to comment.