mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-30 22:39:58 +02:00
resources/images: Add $image.Colors
Which returns the most dominant colors of an image using a simple histogram method. Fixes #10307
This commit is contained in:
@@ -45,6 +45,14 @@ func ReplaceColorInPalette(c color.Color, p color.Palette) {
|
||||
p[p.Index(c)] = c
|
||||
}
|
||||
|
||||
// ColorToHexString converts a color to a hex string.
|
||||
func ColorToHexString(c color.Color) string {
|
||||
r, g, b, a := c.RGBA()
|
||||
rgba := color.RGBA{uint8(r), uint8(g), uint8(b), uint8(a)}
|
||||
return fmt.Sprintf("#%.2x%.2x%.2x", rgba.R, rgba.G, rgba.B)
|
||||
|
||||
}
|
||||
|
||||
func hexStringToColor(s string) (color.Color, error) {
|
||||
s = strings.TrimPrefix(s, "#")
|
||||
|
||||
|
@@ -60,6 +60,30 @@ func TestHexStringToColor(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestColorToHexString(t *testing.T) {
|
||||
c := qt.New(t)
|
||||
|
||||
for _, test := range []struct {
|
||||
arg color.Color
|
||||
expect string
|
||||
}{
|
||||
{color.White, "#ffffff"},
|
||||
{color.Black, "#000000"},
|
||||
{color.RGBA{R: 0x42, G: 0x87, B: 0xf5, A: 0xff}, "#4287f5"},
|
||||
} {
|
||||
|
||||
test := test
|
||||
c.Run(test.expect, func(c *qt.C) {
|
||||
c.Parallel()
|
||||
|
||||
result := ColorToHexString(test.arg)
|
||||
|
||||
c.Assert(result, qt.Equals, test.expect)
|
||||
})
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
func TestAddColorToPalette(t *testing.T) {
|
||||
c := qt.New(t)
|
||||
|
||||
|
@@ -48,6 +48,10 @@ type ImageResourceOps interface {
|
||||
// Exif returns an ExifInfo object containing Image metadata.
|
||||
Exif() *exif.ExifInfo
|
||||
|
||||
// Colors returns a slice of the most dominant colors in an image
|
||||
// using a simple histogram method.
|
||||
Colors() ([]string, error)
|
||||
|
||||
// Internal
|
||||
DecodeImage() (image.Image, error)
|
||||
}
|
||||
|
Reference in New Issue
Block a user