Skip to content

Instantly share code, notes, and snippets.

@mook
Last active November 23, 2024 18:25
Show Gist options
  • Save mook/faf3677b4ae5402af160265b2f9ef1e4 to your computer and use it in GitHub Desktop.
Save mook/faf3677b4ae5402af160265b2f9ef1e4 to your computer and use it in GitHub Desktop.
KDE Plasma Change Color Scheme on Night Color

This is a script plus a systemd (user) unit to change color schemes when Night Color kicks in (to get dark mode).

This is basically a workaround until maybe someday Plasma gets proper support.

The color schemes are named in plasma-color-scheme-on-night-color.sh line 16 and 18. For a hack it's not worth makig it configurable.

Write the systemd config file in ~/.config/systemd/user/plasma-color-scheme-on-night-color.service and the script in ~/opt/scripts/plasma-color-scheme-on-night-color.sh.

Once the files are written, use systemctl --user enable --now plasma-color-scheme-on-night-color to turn on.

[Unit]
Description=Set Plasma Color Scheme Based on Night Color
Requires=plasma-kded.service
PartOf=graphical-session.target
[Service]
Type=simple
ExecStart=%h/opt/scripts/plasma-color-scheme-on-night-color.sh
[Install]
WantedBy=plasma-kded.service
#!/usr/bin/env bash
# Color temperature limit; above this is day, and below is night.
TEMP_LIMIT=5000
# Get whether Night Color is active
get_running() {
local temp=$(dbus-send --session --dest=org.kde.NightColor --print-reply \
/ColorCorrect \
org.freedesktop.DBus.Properties.Get \
string:org.kde.kwin.ColorCorrect string:targetTemperature \
| awk 'END { print $NF }')
[ $temp -lt $TEMP_LIMIT ]
}
# Set the color scheme based on if Night Color is active
set_color_scheme() {
local targetScheme=BreezeClassic
if get_running; then
targetScheme=BreezeDark
fi
local currentScheme=$(kreadconfig5 --file kdeglobals --group General --key ColorScheme)
if [[ $targetScheme != $currentScheme ]]; then
plasma-apply-colorscheme $targetScheme
fi
}
set_color_scheme
dbus-monitor --session --profile \
"type='signal',sender='org.kde.NightColor',path='/ColorCorrect',interface='org.freedesktop.DBus.Properties',member='PropertiesChanged'" \
| while read -r line; do
set_color_scheme
done
@aureus02
Copy link

aureus02 commented Nov 20, 2024

It fails when enabling the service:

Failed to start plasma-color-scheme-on-night-color.service: Unit plasma-kded.service not found.

I do have Plasma 6. My guess is plasma-kded.service is for plasma 5? Any suggestions on this?
Trying to find some info about this service but no luck with it. I should ask in matrix chat for that, maybe?

@mook
Copy link
Author

mook commented Nov 23, 2024

Yeah, this was for Plasma 5. I'm afraid I have no idea what Plasma 6 has (since I don't use it yet), maybe plasma-plasmashell.service or plasma-kwin_wayland.service (resp. _x11)? I'm sure kreadconfig5 would need to change too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment