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:
Bjørn Erik Pedersen
2022-09-21 16:24:54 +02:00
parent 08f0984f91
commit a4028112e3
10 changed files with 90 additions and 0 deletions

View File

@@ -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, "#")

View File

@@ -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)

View File

@@ -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)
}