Skip to content

Commit 4748b47

Browse files
committed
Add the dominant colors in the background
This needs Hugo >= 0.104.0. Also add a few settings: ```toml [params] [params.gallerydeluxe] # Shuffle the images in the gallery to give the impression of a new gallery each time. shuffle = false # Reverse the order of the images in the gallery. reverse = true ```
1 parent d2454fa commit 4748b47

File tree

7 files changed

+77
-18
lines changed

7 files changed

+77
-18
lines changed

‎README.md‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,27 @@
1-
# Gallery Deluxe
1+
* [Configuration](#configuration)
2+
* [Credits](#credits)
23

34
A Hugo Module to show a photo gallery. It's very fast/effective, especially if you have lots of images on display.
45

5-
See the annotated [index.html](exampleSite/layouts/index.html) for a brief explanation about how to set this up. Note that we currently only support 1 gallery per page. **Note** that the `exampleSite` is currently configured to load a [directory from bep's MacBook](https://github.com/bep/gallerydeluxe/blob/main/exampleSite/config.toml#L38). If you want to take this for a spin, modify that so it points to a directory with some JPEGs on your PC.
6-
76
This theme is what you see on [staticbattery.com](https://staticbattery.com/) which, at the time of writing this, [scores 100](https://pagespeed.web.dev/report?url=https%3A%2F%2Fstaticbattery.com%2F&form_factor=mobile) at Google PageSpeed for both mobile and desktop.
87

98
[<img src="https://raw.githubusercontent.com/bep/gallerydeluxe/main/images/tn.jpg">](https://staticbattery.com/)
109

10+
## Configuration
11+
12+
```toml
13+
[params]
14+
[params.gallerydeluxe]
15+
# Shuffle the images in the gallery to give the impression of a new gallery each time.
16+
shuffle = false
17+
18+
# Reverse the order of the images in the gallery.
19+
reverse = true
20+
```
21+
22+
Also See the annotated [index.html](exampleSite/layouts/index.html) for a brief explanation about how to set this up. Note that we currently only support 1 gallery per page. **Note** that the `exampleSite` is currently configured to load a [directory from bep's MacBook](https://github.com/bep/gallerydeluxe/blob/main/exampleSite/config.toml#L38). If you want to take this for a spin, modify that so it points to a directory with some JPEGs on your PC.
23+
24+
25+
## Credits
26+
1127
Credit to Dan Schlosser for the [Pig](https://github.com/schlosser/pig.js) JS library.

‎assets/css/gallerydeluxe/styles.css‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
.gd-figure {
2-
background-color: #565657 !important;
3-
}
4-
51
.gd-figure:hover {
62
filter: brightness(1.25);
73
cursor: pointer;

‎assets/js/gallerydeluxe/src/index.js‎

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
//import * as params from '@params';
3+
import * as params from '@params';
44
import { Pig } from './pig';
55
import { newSwiper } from './helpers';
66

@@ -153,11 +153,15 @@ let GalleryDeluxe = {
153153
// Load the gallery.
154154
let images = await (await fetch(dataUrl)).json();
155155

156-
// Shuffle them to make it more interesting.
157-
images = images
158-
.map((value) => ({ value, sort: Math.random() }))
159-
.sort((a, b) => a.sort - b.sort)
160-
.map(({ value }) => value);
156+
if (params.shuffle) {
157+
// Shuffle them to make it more interesting.
158+
images = images
159+
.map((value) => ({ value, sort: Math.random() }))
160+
.sort((a, b) => a.sort - b.sort)
161+
.map(({ value }) => value);
162+
} else if (params.reverse) {
163+
images = images.reverse();
164+
}
161165

162166
let imagesMap = new Map();
163167
let imageData = [];
@@ -183,6 +187,21 @@ let GalleryDeluxe = {
183187
urlForSize: function (filename, size) {
184188
return imagesMap.get(filename)[size];
185189
},
190+
styleForElement: function (filename) {
191+
let image = imagesMap.get(filename);
192+
if (!image || image.colors.size < 1) {
193+
return '';
194+
}
195+
let colors = image.colors;
196+
let first = colors[0];
197+
let second = '#ccc';
198+
// Some images have only one dominant color.
199+
if (colors.length > 1) {
200+
second = colors[1];
201+
}
202+
203+
return ` background: linear-gradient(15deg, ${first}, ${second});`;
204+
},
186205
};
187206

188207
new Pig(imageData, options).enable();

‎assets/js/gallerydeluxe/src/pig.js‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,15 @@ export function Pig(imageData, options) {
293293
return '/img/' + size.toString(10) + '/' + filename;
294294
},
295295

296+
/**
297+
* Get the a custom style for the container element.
298+
*
299+
* * @param {string} filename - The filename of the image.
300+
*/
301+
styleForElement: function (filename) {
302+
return '';
303+
},
304+
296305
/**
297306
* Get a callback with the filename of the image
298307
* which was clicked.
@@ -810,6 +819,7 @@ ProgressiveImage.prototype.load = function () {
810819
// Show thumbnail
811820
if (!this.thumbnail) {
812821
this.thumbnail = new Image();
822+
813823
this.thumbnail.src = this.pig.settings.urlForSize(this.filename, this.pig.settings.thumbnailSize);
814824
this.thumbnail.className = this.classNames.thumbnail;
815825
this.thumbnail.onload = function () {
@@ -885,6 +895,12 @@ ProgressiveImage.prototype.getElement = function () {
885895
if (!this.element) {
886896
this.element = document.createElement(this.pig.settings.figureTagName);
887897
this.element.className = this.classNames.figure;
898+
899+
let style = this.pig.settings.styleForElement(this.filename);
900+
if (this.style) {
901+
this.element.style = style;
902+
}
903+
888904
if (this.pig.settings.onClickHandler !== null) {
889905
this.element.addEventListener(
890906
'click',

‎exampleSite/assets/css/styles.css‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ body {
33
height: 100%;
44
margin: 0;
55
padding: 0;
6-
background-color: #000;
6+
background-color: #101010;
77
}
88

99
.logo {

‎exampleSite/config.toml‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@ baseURL = "https://www.staticbattery.com/"
22
title = "staticbattery.com"
33
disableKinds = ["section", "taxonomy", "term"]
44

5+
[params]
6+
[params.gallerydeluxe]
7+
# Shuffle the images in the gallery to give the impression of a new gallery each page load.
8+
shuffle = false
9+
10+
# Reverse the order of the images in the gallery.
11+
reverse = true
12+
513
[caches]
614
[caches.images]
715
dir = ':cacheDir/gallerydeluxe'
@@ -18,9 +26,6 @@ disableKinds = ["section", "taxonomy", "term"]
1826
[server.headers.values]
1927
Referrer-Policy = 'strict-origin-when-cross-origin'
2028

21-
[params]
22-
[params.gallerydeluxe]
23-
2429
[module]
2530
[[module.mounts]]
2631
source = "assets"

‎layouts/partials/gallerydeluxe/init.html‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,19 @@
66
{{ $images := $gallery.Resources.Match "**.jpg" }}
77
{{ range $images }}
88
{{ $thumbs := partial "gallerydeluxe/create-thumbs.html" . }}
9+
{{ $20 := (index $thumbs "20") }}
10+
{{ $colors := slice }}
11+
{{ if (ge hugo.Version "0.104") }}
12+
{{/* .Colors method was added in Hugo 0.104.0 */}}
13+
{{ $colors = $20.Colors }}
14+
{{ end }}
915
{{ $m := dict
1016
"name" .Name
1117
"full" .RelPermalink
1218
"width" .Width
1319
"height" .Height
14-
"20" (index $thumbs "20").RelPermalink
20+
"colors" $colors
21+
"20" $20.RelPermalink
1522
"100" (index $thumbs "100").RelPermalink
1623
"250" (index $thumbs "250").RelPermalink
1724
"500" (index $thumbs "500").RelPermalink

0 commit comments

Comments
 (0)