2

There's a way in browsers to query if the user prefers a dark or light theme so that a website developer could adapt the website's colours according to user preference.

Is there also a way to detect that on the command line? Is there a command that outputs light or dark (or some equivalent boolean-valued output indicating light or dark?)

Kusalananda
  • 320,670
  • 36
  • 633
  • 936
Peeyush Kushwaha
  • 514
  • 4
  • 13

1 Answers1

2

I use the following command to detect the current color scheme, as I use a color scheme switching extension with Gnome:

#!/bin/sh
# org.freedesktop.appearance color-scheme
#
# Indicates the system's preferred color scheme.
# Supported values are:
#
#   0: No preference
#   1: Prefer dark appearance
#   2: Prefer light appearance
#
# Unknown values should be treated as 0 (no preference).

scheme=$(
  gdbus call --session --timeout=1000 \
             --dest=org.freedesktop.portal.Desktop \
             --object-path /org/freedesktop/portal/desktop \
             --method org.freedesktop.portal.Settings.Read org.freedesktop.appearance color-scheme
)

case $scheme in
  ( '(<<uint32 1>>,)' ) exit 1;;
  ( '(<<uint32 2>>,)' ) exit 2;;
  ( *                 ) exit 0;;
esac

This works with Fedora 37 and Ubuntu 22.04 (running inside Fedora via distrobox, so YMMV).

René
  • 186
  • 1
  • 7