summaryrefslogtreecommitdiffstats
path: root/resources/images/filters.go
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2024-04-15 10:09:25 +0200
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2024-04-16 10:02:46 +0200
commite197c7b29d8814d098bd53e9e7efd97c70f8de5f (patch)
treeb91bd66bb2eb27d2b1dc07637b66bcdd34951b93 /resources/images/filters.go
parent74e9129568e3b506a34205f11d024400f833a907 (diff)
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/images/filters.go')
-rw-r--r--resources/images/filters.go24
1 files changed, 14 insertions, 10 deletions
diff --git a/resources/images/filters.go b/resources/images/filters.go
index 53818c97d..0a620716d 100644
--- a/resources/images/filters.go
+++ b/resources/images/filters.go
@@ -1,4 +1,4 @@
-// Copyright 2019 The Hugo Authors. All rights reserved.
+// Copyright 2024 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -65,7 +65,7 @@ func (*Filters) Opacity(opacity any) gift.Filter {
func (*Filters) Text(text string, options ...any) gift.Filter {
tf := textFilter{
text: text,
- color: "#ffffff",
+ color: color.White,
size: 20,
x: 10,
y: 10,
@@ -78,7 +78,9 @@ func (*Filters) Text(text string, options ...any) gift.Filter {
for option, v := range opt {
switch option {
case "color":
- tf.color = cast.ToString(v)
+ if color, ok, _ := toColorGo(v); ok {
+ tf.color = color
+ }
case "size":
tf.size = cast.ToFloat64(v)
case "x":
@@ -128,15 +130,14 @@ func (*Filters) Padding(args ...any) gift.Filter {
var top, right, bottom, left int
var ccolor color.Color = color.White // canvas color
- var err error
_args := args // preserve original args for most stable hash
- if vcs, ok := (args[len(args)-1]).(string); ok {
- ccolor, err = hexStringToColor(vcs)
+ if vcs, ok, err := toColorGo(args[len(args)-1]); ok || err != nil {
if err != nil {
panic("invalid canvas color: specify RGB or RGBA using hex notation")
}
+ ccolor = vcs
args = args[:len(args)-1]
if len(args) == 0 {
panic("not enough arguments: provide one or more padding values using the CSS shorthand property syntax")
@@ -180,12 +181,11 @@ func (*Filters) Padding(args ...any) gift.Filter {
// Dither creates a filter that dithers an image.
func (*Filters) Dither(options ...any) gift.Filter {
ditherOptions := struct {
- Colors []string
+ Colors []any
Method string
Serpentine bool
Strength float32
}{
- Colors: []string{"000000ff", "ffffffff"},
Method: "floydsteinberg",
Serpentine: true,
Strength: 1.0,
@@ -198,14 +198,18 @@ func (*Filters) Dither(options ...any) gift.Filter {
}
}
+ if len(ditherOptions.Colors) == 0 {
+ ditherOptions.Colors = []any{"000000ff", "ffffffff"}
+ }
+
if len(ditherOptions.Colors) < 2 {
panic("palette must have at least two colors")
}
var palette []color.Color
for _, c := range ditherOptions.Colors {
- cc, err := hexStringToColor(c)
- if err != nil {
+ cc, ok, err := toColorGo(c)
+ if !ok || err != nil {
panic(fmt.Sprintf("%q is an invalid color: specify RGB or RGBA using hexadecimal notation", c))
}
palette = append(palette, cc)