0

In GNOME based environments, under X11, the average background color of the wallpaper can be obtained with

xprop -root | grep _GNOME_BACKGROUND_REPRESENTATIVE_COLORS

It gives an output like

_GNOME_BACKGROUND_REPRESENTATIVE_COLORS(STRING) = "rgb(65,70,61)"

This is the method Unity used to make the dash and launcher have the same color as the wallpaper.

However, xprop probably does not work in Wayland, and _GNOME_BACKGROUND_REPRESENTATIVE_COLORS does not work in KDE. How to get this color in all environments?

How to get accent color in modern GNOME versions? And how to do this in KDE and other DEs?

I want to use this color to make an app have "chameleonic" background color.

1 Answers1

0

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.