From 82419df7b1aa55141684b734a3ee25c2cb573276 Mon Sep 17 00:00:00 2001 From: Chuck Dries Date: Sat, 18 Jun 2022 00:11:34 -0700 Subject: [PATCH] sort gallery by Vibrant hue --- gatsby-node.js | 16 ++++++++++++---- src/components/GalleryImage/GalleryImage.js | 2 +- src/components/MasonryGallery.js | 3 +-- src/pages/photogallery.js | 17 +++++++++++------ 4 files changed, 25 insertions(+), 13 deletions(-) diff --git a/gatsby-node.js b/gatsby-node.js index 18b9754..7dd5fe7 100644 --- a/gatsby-node.js +++ b/gatsby-node.js @@ -4,7 +4,7 @@ const chroma = require("chroma-js"); const chalk = require("chalk"); const R = require("ramda"); const exifr = require("exifr"); -const sharp = require('sharp'); +const sharp = require("sharp"); const badContrast = (color1, color2) => chroma.contrast(color1, color2) < 4.5; @@ -102,11 +102,13 @@ function processColors(vibrantData, imagePath) { function transformMetaToNodeData(metaData, vibrantData, imagePath) { const vibrant = vibrantData ? processColors(vibrantData, imagePath) : null; + let dominantHue = vibrantData.Vibrant.getHsl()[0] * 360; return { dateTaken: metaData.DateTimeOriginal, meta: metaData, vibrant, + dominantHue, }; } @@ -118,13 +120,15 @@ exports.onCreateNode = async function ({ node, actions }) { xmp: true, // icc: true }); + const resizedImage = await sharp(node.absolutePath) .resize({ width: 3000, height: 3000, - fit: 'inside' + fit: "inside", }) - .toBuffer() + .toBuffer(); + const vibrantData = await Vibrant.from(resizedImage) .quality(1) .getPalette(); @@ -132,7 +136,11 @@ exports.onCreateNode = async function ({ node, actions }) { createNodeField({ node, name: "imageMeta", - value: transformMetaToNodeData(metaData, vibrantData, node.absolutePath), + value: transformMetaToNodeData( + metaData, + vibrantData, + node.absolutePath + ), }); } }; diff --git a/src/components/GalleryImage/GalleryImage.js b/src/components/GalleryImage/GalleryImage.js index 853f34d..65eb972 100644 --- a/src/components/GalleryImage/GalleryImage.js +++ b/src/components/GalleryImage/GalleryImage.js @@ -145,7 +145,7 @@ const GalleryImage = ({ data, pageContext }) => { { if (!window.location.hash) { @@ -34,7 +33,7 @@ const MasonryGallery = ({ // hacky but it works for now setTimeout(() => { scrollIntoView(); - }, 100) + }, 100); }, [scrollIntoView]); const aspectRatios = React.useMemo( diff --git a/src/pages/photogallery.js b/src/pages/photogallery.js index 2375d13..7bb6e72 100644 --- a/src/pages/photogallery.js +++ b/src/pages/photogallery.js @@ -45,10 +45,10 @@ const GalleryPage = ({ data }) => {
@@ -62,7 +62,7 @@ export const query = graphql` query GalleryPageQuery { allFile( filter: { sourceInstanceName: { eq: "gallery" } } - sort: { order: DESC, fields: fields___imageMeta___dateTaken } + sort: { fields: fields___imageMeta___dominantHue, order: DESC } ) { edges { node { @@ -72,12 +72,17 @@ export const query = graphql` fluid { aspectRatio } - gatsbyImageData(layout: CONSTRAINED, height: 550) + gatsbyImageData( + layout: CONSTRAINED + height: 550 + placeholder: DOMINANT_COLOR + ) } fields { imageMeta { + dominantHue dateTaken - meta{ + meta { ObjectName } }