diff options
author | Bjørn Erik Pedersen <[email protected]> | 2024-04-15 10:09:25 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <[email protected]> | 2024-04-16 10:02:46 +0200 |
commit | e197c7b29d8814d098bd53e9e7efd97c70f8de5f (patch) | |
tree | b91bd66bb2eb27d2b1dc07637b66bcdd34951b93 /resources/image_test.go | |
parent | 74e9129568e3b506a34205f11d024400f833a907 (diff) | |
download | hugo-e197c7b29d8814d098bd53e9e7efd97c70f8de5f.tar.gz hugo-e197c7b29d8814d098bd53e9e7efd97c70f8de5f.zip |
Add Luminance to Color
To sort an image's colors from darkest to lightest, you can then do:
```handlebars
{{ {{ $colorsByLuminance := sort $image.Colors "Luminance" }}
```
This uses the formula defined here: https://www.w3.org/TR/WCAG21/#dfn-relative-luminance
Fixes #10450
Diffstat (limited to 'resources/image_test.go')
-rw-r--r-- | resources/image_test.go | 29 |
1 files changed, 27 insertions, 2 deletions
diff --git a/resources/image_test.go b/resources/image_test.go index 231a06453..7e26c1f55 100644 --- a/resources/image_test.go +++ b/resources/image_test.go @@ -85,9 +85,16 @@ func TestImageTransformBasic(t *testing.T) { assertWidthHeight(c, img, w, h) } - colors, err := image.Colors() + gotColors, err := image.Colors() c.Assert(err, qt.IsNil) - c.Assert(colors, qt.DeepEquals, []string{"#2d2f33", "#a49e93", "#d39e59", "#a76936", "#737a84", "#7c838b"}) + expectedColors := images.HexStringsToColors("#2d2f33", "#a49e93", "#d39e59", "#a76936", "#737a84", "#7c838b") + c.Assert(len(gotColors), qt.Equals, len(expectedColors)) + for i := range gotColors { + c1, c2 := gotColors[i], expectedColors[i] + c.Assert(c1.ColorHex(), qt.Equals, c2.ColorHex()) + c.Assert(c1.ColorGo(), qt.DeepEquals, c2.ColorGo()) + c.Assert(c1.Luminance(), qt.Equals, c2.Luminance()) + } c.Assert(image.RelPermalink(), qt.Equals, "/a/sunset.jpg") c.Assert(image.ResourceType(), qt.Equals, "image") @@ -445,6 +452,24 @@ func TestImageExif(t *testing.T) { getAndCheckExif(c, image) } +func TestImageColorsLuminance(t *testing.T) { + c := qt.New(t) + + _, image := fetchSunset(c) + c.Assert(image, qt.Not(qt.IsNil)) + colors, err := image.Colors() + c.Assert(err, qt.IsNil) + c.Assert(len(colors), qt.Equals, 6) + var prevLuminance float64 + for i, color := range colors { + luminance := color.Luminance() + c.Assert(err, qt.IsNil) + c.Assert(luminance > 0, qt.IsTrue) + c.Assert(luminance, qt.Not(qt.Equals), prevLuminance, qt.Commentf("i=%d", i)) + prevLuminance = luminance + } +} + func BenchmarkImageExif(b *testing.B) { getImages := func(c *qt.C, b *testing.B, fs afero.Fs) []images.ImageResource { spec := newTestResourceSpec(specDescriptor{fs: fs, c: c}) |