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 /common | |
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 'common')
-rw-r--r-- | common/hstrings/strings.go | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/common/hstrings/strings.go b/common/hstrings/strings.go index d9426ab5d..1232eee37 100644 --- a/common/hstrings/strings.go +++ b/common/hstrings/strings.go @@ -123,6 +123,20 @@ func InSlicEqualFold(arr []string, el string) bool { return false } +// ToString converts the given value to a string. +// Note that this is a more strict version compared to cast.ToString, +// as it will not try to convert numeric values to strings, +// but only accept strings or fmt.Stringer. +func ToString(v any) (string, bool) { + switch vv := v.(type) { + case string: + return vv, true + case fmt.Stringer: + return vv.String(), true + } + return "", false +} + type Tuple struct { First string Second string |