Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebP contrast issues #102

Open
nicokaiser opened this issue Dec 9, 2024 · 1 comment
Open

WebP contrast issues #102

nicokaiser opened this issue Dec 9, 2024 · 1 comment

Comments

@nicokaiser
Copy link
Owner

Due to a bug in the WebP library used in Hugo (as of version 0.139) black and white levels in WebP images are not handled correctly, which makes the images look more dull/muted.

There seems to be no workaround other than either the bug to be fixed or Hugo moving to another WebP library, so my recommendation is not to use WebP images in Hugo.

@nicokaiser
Copy link
Owner Author

A (very dirty) workaround, if you really need to use WebP images:

--- a/layouts/partials/gallery.html
+++ b/layouts/partials/gallery.html
@@ -25,6 +25,7 @@
         "Date" $date
         "image" $image
         "Params" $image.Params
+        "MediaType" $image.MediaType
         )
       }}
     {{ end }}
@@ -32,6 +33,12 @@
       {{ $image := .image }}
       {{ $thumbnail := $image.Filter (slice images.AutoOrient (images.Process "fit 600x600")) }}
       {{ $full := $image.Filter (slice images.AutoOrient (images.Process "fit 1600x1600")) }}
+      {{ /* Workaround for WebP contrast issues: https://github.com/nicokaiser/hugo-theme-gallery/issues/102 */}}
+      {{ if eq $image.MediaType.SubType "webp" }}
+        {{ $thumbnail = $image.Filter (slice images.AutoOrient (images.Process "fit 600x600") (images.Contrast +13)) }}
+        {{ $full = $image.Filter (slice images.AutoOrient (images.Process "fit 1600x1600") (images.Contrast +13)) }}
+      {{ end }}
       {{ $color := index $thumbnail.Colors 0 | default "transparent" }}
       <a class="gallery-item" href="{{ $image.RelPermalink }}" data-pswp-src="{{ $full.RelPermalink }}" data-pswp-width="{{ $full.Width }}" data-pswp-height="{{ $full.Height }}" data-pswp-target="{{ $image.Name | urlize }}" title="{{ .Title }}" itemscope itemtype="https://schema.org/ImageObject" style="aspect-ratio: {{ $thumbnail.Width }} / {{ $thumbnail.Height }}">
         <figure style="background-color: {{ $color }}; aspect-ratio: {{ $thumbnail.Width }} / {{ $thumbnail.Height }}">

This adds 13% contrast to WebP image (which is roughly the level difference between limited an full color range. Note that this might not work for all WebP images and may result in wrong colors.
Therefore I will not include this into the main branch.

nicokaiser added a commit that referenced this issue Dec 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant