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
This commit is contained in:
Bjørn Erik Pedersen
2024-04-15 10:09:25 +02:00
parent 74e9129568
commit e197c7b29d
12 changed files with 204 additions and 39 deletions

View File

@@ -15,6 +15,7 @@ package images
import (
"image"
"image/color"
"image/draw"
"io"
"strings"
@@ -31,7 +32,8 @@ import (
var _ gift.Filter = (*textFilter)(nil)
type textFilter struct {
text, color string
text string
color color.Color
x, y int
size float64
linespacing int
@@ -39,11 +41,6 @@ type textFilter struct {
}
func (f textFilter) Draw(dst draw.Image, src image.Image, options *gift.Options) {
color, err := hexStringToColor(f.color)
if err != nil {
panic(err)
}
// Load and parse font
ttf := goregular.TTF
if f.fontSource != nil {
@@ -74,7 +71,7 @@ func (f textFilter) Draw(dst draw.Image, src image.Image, options *gift.Options)
d := font.Drawer{
Dst: dst,
Src: image.NewUniform(color),
Src: image.NewUniform(f.color),
Face: face,
}