From f8a12261ea342d73a61a6308b2a34034d36eaaa3 Mon Sep 17 00:00:00 2001 From: velvettear Date: Tue, 21 Nov 2023 14:38:41 +0100 Subject: [PATCH] optimized color palette generation --- .vscode/launch.json | 16 +++++++++- internal/slideshow/palette.go | 52 +++++++++++++++++++-------------- internal/slideshow/slideshow.go | 7 ++++- 3 files changed, 51 insertions(+), 24 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 881313e..549a66e 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -2,7 +2,7 @@ "version": "0.0.1", "configurations": [ { - "name": "slideshow", + "name": "slideshow-scaled", "type": "go", "request": "launch", "mode": "auto", @@ -15,6 +15,20 @@ "SLIDESHOW_PALETTE": "/tmp/slideshow.palette" }, "console": "integratedTerminal" + }, + { + "name": "slideshow-unscaled", + "type": "go", + "request": "launch", + "mode": "auto", + "program": "${workspaceFolder}/main.go", + "env":{ + "SLIDESHOW_INTERVAL": "10", + "SLIDESHOW_DIRECTORY": "/home/velvettear/images", + "SLIDESHOW_LOGLEVEL": "debug", + "SLIDESHOW_PALETTE": "/tmp/slideshow.palette" + }, + "console": "integratedTerminal" } ] } diff --git a/internal/slideshow/palette.go b/internal/slideshow/palette.go index cd499af..ff139a6 100644 --- a/internal/slideshow/palette.go +++ b/internal/slideshow/palette.go @@ -1,7 +1,9 @@ package slideshow import ( + "bytes" "fmt" + "image" "image/color" "os" "strconv" @@ -13,16 +15,11 @@ import ( ) // write the base16 color palette to a file -func exportPalette(image string) { - if !config.IsPaletteSet() { +func exportPalette(colors []color.Color) { + if len(colors) == 0 || !config.IsPaletteSet() { return } var palette string - colors, error := getColorPalette(image, 16) - if error != nil { - loggo.Error("encountered an error getting the color palette for image", "image: "+image, error.Error()) - return - } for index, color := range colors { if index > 0 { palette += "\n" @@ -34,33 +31,44 @@ func exportPalette(image string) { tmp = "SLIDESHOW_COLOR" + tmp palette += tmp + "=\"" + rgbToHex(color) + "\"" } - error = os.WriteFile(config.Palette, []byte(palette), 0775) + error := os.WriteFile(config.Palette, []byte(palette), 0775) if error != nil { - loggo.Error("encountered an error exporting the color palette for image", "image: "+image, error.Error()) + loggo.Error("encountered an error exporting a color palette", error.Error()) } } -// get the base16 color palette from an image -func GetBase16Colors(image string) (string, error) { - var base16 string - colors, error := getColorPalette(image, 16) +// extract the given amount of dominant colors of raw image bytes +func getColorPaletteRaw(data []byte, amount int) ([]color.Color, error) { + var colors []color.Color + if !config.IsPaletteSet() { + return colors, nil + } + timestamp := time.Now().UnixMilli() + img, _, error := image.Decode(bytes.NewReader(data)) if error != nil { - return base16, error + loggo.ErrorTimed("encountered an error decoding the provided raw image bytes to an image", timestamp, error.Error()) + return colors, error } - for index, color := range colors { - if index > 0 { - base16 += "\n" - } - base16 += rgbToHex(color) + colors, error = color_thief.GetPalette(img, amount, 0) + if error != nil { + loggo.ErrorTimed("encountered an error generating the color palette from raw image bytes", timestamp, "colors: "+strconv.Itoa(amount)) + } else { + loggo.DebugTimed("generated color palette from raw image bytes", timestamp, "colors: "+strconv.Itoa(amount)) } - return base16, nil + return colors, error } -// extract the given amount of dominant colors an image +// extract the given amount of dominant colors of an image func getColorPalette(image string, amount int) ([]color.Color, error) { + var colors []color.Color + if !config.IsPaletteSet() { + return colors, nil + } timestamp := time.Now().UnixMilli() colors, error := color_thief.GetPaletteFromFile(image, amount, 0) - if error == nil { + if error != nil { + loggo.ErrorTimed("encountered an error generating the color palette from image", timestamp, "image: "+image, "colors: "+strconv.Itoa(amount)) + } else { loggo.DebugTimed("generated color palette from image", timestamp, "image: "+image, "colors: "+strconv.Itoa(amount)) } return colors, error diff --git a/internal/slideshow/slideshow.go b/internal/slideshow/slideshow.go index 5c4a453..ddf0d15 100644 --- a/internal/slideshow/slideshow.go +++ b/internal/slideshow/slideshow.go @@ -2,6 +2,7 @@ package slideshow import ( "errors" + "image/color" "io" "os/exec" "strconv" @@ -24,6 +25,7 @@ func Start() { scaleImages := config.IsResolutionSet() for { var image string + var palette []color.Color var data []byte for { image = watcher.GetRandomImage() @@ -40,12 +42,15 @@ func Start() { } data = tmp scaleTime = time.Since(scaleTimestamp) + palette, _ = getColorPaletteRaw(data, 16) + } else { + palette, _ = getColorPalette(image, 16) } if sleepTime > 0 { loggo.Debug("sleeping for " + strconv.FormatInt(sleepTime.Milliseconds(), 10) + "ms") time.Sleep(sleepTime) } - go exportPalette(image) + go exportPalette(palette) if scaleImages { error := setBackgroundPiped(data) if error != nil {