optimized color palette generation
This commit is contained in:
parent
d550414b7c
commit
f8a12261ea
3 changed files with 51 additions and 24 deletions
16
.vscode/launch.json
vendored
16
.vscode/launch.json
vendored
|
@ -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"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in a new issue