You can get path of the wallpaper using commands like gsettings get org.gnome.desktop.background picture-uri (there are similar commands for cinnamon and mate - look at the bash script below).
For KDE, the wallpaper path is encoded in the file $HOME/.config/plasma-org.kde.plasma.desktop-appletsrc as Image=/path/to/wallpaper/top/directory
or Image=/path/to/wallpaper/file.
Once you know the file location, use imagemagick to convert it to resize pixel image, and get that pixel's color.
The following bash script does the job.
#!/bin/bash
Function to get the current wallpaper path based on the desktop environment
get_wallpaper_path() {
local de=$(echo "$XDG_CURRENT_DESKTOP" | tr '[:upper:]' '[:lower:]')
local wallpaper=""
case "$de" in
"gnome")
wallpaper=$(gsettings get org.gnome.desktop.background picture-uri | sed -e "s|'file://\(.*\)'|\1|")
;;
"cinnamon")
wallpaper=$(gsettings get org.cinnamon.desktop.background picture-uri | sed -e "s|'file://\(.*\)'|\1|")
;;
"mate")
wallpaper=$(gsettings get org.mate.desktop.background picture-uri | sed -e "s|'file://\(.*\)'|\1|")
;;
"kde")
local config_file="$HOME/.config/plasma-org.kde.plasma.desktop-appletsrc"
if [[ -f "$config_file" ]]; then
wallpaper=$(grep -m1 '^Image=' "$config_file" | cut -d'=' -f2)
# If the wallpaper path is a directory, find a valid image file
if [[ -d "$wallpaper" ]]; then
echo "KDE: Wallpaper path is a directory, searching for an image..."
for file in "$wallpaper"/contents/images/*.{jpg,png}; do
if [[ -f "$file" ]]; then
wallpaper="$file"
break
fi
done
fi
else
echo "KDE configuration file not found." >&2
exit 1
fi
;;
*)
echo "Unsupported desktop environment: $de" >&2
exit 1
;;
esac
echo "$wallpaper"
}
wallpaper_path=$(get_wallpaper_path)
Check if the retrieved wallpaper file actually exists
if [[ -z "$wallpaper_path" || ! -f "$wallpaper_path" ]]; then
echo "Error: Could not find a valid wallpaper file!" >&2
exit 1
fi
echo "Current wallpaper: $wallpaper_path"
Extract the dominant color using ImageMagick
dominant_color=$(convert "$wallpaper_path" -resize 1x1 txt:- | awk 'NR==2 {print $3}')
echo "Extracted dominant color: $dominant_color"
Declaration: I took help from chatgpt in putting everything together in the bash script (using the ideas I described above), but I have tested it and read the code to ensure that it works.