Ubuntu 自動調整螢幕色溫

色溫

長時間使用電腦螢幕,降低藍光保護眼睛。

我長久以來的習慣都是把電腦螢幕調整成偏暖的色調,色溫的值是數字越大表是越冷、偏藍;數字越小表示越暖、偏紅。


(圖片來源:https://zh.wikipedia.org/zh-tw/色溫)

常見色溫數值:

  • 2800 K:白熾燈
  • 3000 K:鹵素燈及黃光日光燈
  • 4100 K:月光、淺黃光日光燈
  • 5000 K:日光
  • 6500 K:最常見的白光日光燈色溫

光色的應用:

  • 暖色光:3300K 以下,給人溫暖、想睡的感受,適合睡前使用。
  • 中性色光:3300K ~ 5300K,給人舒適的感受,適合辧公室使用。
  • 冷色光:5300K 以上,接近自然光,給人精力集中的感受,適合辧公室使用。

摘自維基百科

因為工作是寫程式,必須整天盯著螢幕,如何減緩眼睛的不適就很重要。我個人的感覺是,螢幕太亮、冷色調 (偏藍) 的情況下,看久了眼睛就會痠痛,因此後來只要不是在處理照片、圖片等需要正確顏色的情況,就會把螢幕調成暖色,白天大約是 4200K ~ 5000K 左右,晚上就會降到 2800K ~ 3400K,越晚越暖。

在 Mac 上都是安裝 f.lux 來自動化,白天的預設值更動為 4800K 左右,可以發現螢幕的藍色會微微的減少。

現在主要使用 Ubuntu 22.04,但是 f.lux 只支援 X11,而 Redshift 最新版只到 2018 年,也只有支援 X11,對於 Ubuntu Wayland 視窗系統完全無法使用。沒辧法之下只能手動開啟「夜光模式」,可是非常麻煩,第一,我不知道當下調整的色溫是多少;第二,白天及晚上想要調整不一樣的色溫,每天手動調整實在有夠麻煩。

後來找到可以調整色溫的命令列指令,這樣就能完全透過指令來調整,只要寫成腳本加上排程就能做到自動調整色溫,以下就是整個實作過程。

night-light-auto.sh 完整程式碼:

#!/bin/bash

# 如果沒有啟用「夜光模式」,就不要設定色溫
is_enabled=$(gsettings get org.gnome.settings-daemon.plugins.color night-light-enabled)
if [ "$is_enabled" = "false" ]; then
    exit
fi

# 設定環境變數,在 Crontab 中才會正常執行
PID=$(pgrep -o gnome-shell)
export DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$PID/environ|cut -d= -f2-)

# 取得當前小時
current_hour=$(date +%H)
# 取得目前的色溫,回傳結果「uint32 4200」
current_temperature=$(gsettings get org.gnome.settings-daemon.plugins.color night-light-temperature)
# 去掉「uint32 」
current_temperature=${current_temperature//uint32 /}

# 在 00:00 到 05:00 之間
if [ "$current_hour" -ge 0 ] && [ "$current_hour" -lt 5 ]; then
    if [ "$current_temperature" -ne 2600 ]; then
        gsettings set org.gnome.settings-daemon.plugins.color night-light-temperature 2600
        notify-send -i face-cool -t 1000 "調整色溫" "2600K"
    fi

# 在 05:00 到 06:00 之間
elif [ "$current_hour" -ge 5 ] && [ "$current_hour" -lt 6 ]; then
    if [ "$current_temperature" -ne 3400 ]; then
        gsettings set org.gnome.settings-daemon.plugins.color night-light-temperature 3400
        notify-send -i face-cool -t 1000 "調整色溫" "3400K"
    fi

# 在 06:00 到 16:00 之間
elif [ "$current_hour" -ge 6 ] && [ "$current_hour" -lt 16 ]; then
    if [ "$current_temperature" -ne 4200 ]; then
        gsettings set org.gnome.settings-daemon.plugins.color night-light-temperature 4200
        notify-send -i face-cool -t 1000 "調整色溫" "4200K"
    fi

# 在 16:00 到 19:00 之間
elif [ "$current_hour" -ge 16 ] && [ "$current_hour" -lt 19 ]; then
    if [ "$current_temperature" -ne 3800 ]; then
        gsettings set org.gnome.settings-daemon.plugins.color night-light-temperature 3800
        notify-send -i face-cool -t 1000 "調整色溫" "3800K"
    fi

# 在 19:00 到 22:00 之間
elif [ "$current_hour" -ge 19 ] && [ "$current_hour" -lt 22 ]; then
    if [ "$current_temperature" -ne 3400 ]; then
        gsettings set org.gnome.settings-daemon.plugins.color night-light-temperature 3400
        notify-send -i face-cool -t 1000 "調整色溫" "3400K"
    fi

# 在 22:00 到 24:00 之間
elif [ "$current_hour" -ge 22 ]; then
    if [ "$current_temperature" -ne 2600 ]; then
        gsettings set org.gnome.settings-daemon.plugins.color night-light-temperature 2600
        notify-send -i face-cool -t 1000 "調整色溫" "2600K"
    fi
fi

# 註:notify-send 的 icon(-i 選項) 可在 /usr/share/icons/Adwaita/scalable/emotes 找到可用的圖示

記得把 night-light-auto.sh 設定為可執行

chmod +x night-light-auto.sh

另外,必須手動在「設定」中啟用「夜光模式」這個腳本才會執行,這是因為在修圖的時候不想調整色溫,會手動關閉夜光模式。

接著設定排程:

crontab -e
# 加入以下排程

# 每15分鐘執行
0,15,30,45 * * * * /home/tony/dev/shell/night-light-auto.sh
# 開機後 15 秒才執行
@reboot sleep 15; /home/tony/dev/shell/night-light-auto.sh
本文網址:https://blog.tonycube.com/2025/01/ubuntu-night-light-color-temperature.html
Tony Blog 撰寫,請勿全文複製,轉載時請註明出處及連結,謝謝 😀

我要留言

留言小提醒:
1.回覆時間通常在晚上,如果太忙可能要等幾天。
2.請先瀏覽一下其他人的留言,也許有人問過同樣的問題。
3.程式碼請先將它編碼後再貼上。(線上編碼:http://bit.ly/1DL6yog)
4.文字請加上標點符號及斷行,難以閱讀者恕難回覆。
5.感謝您的留言,您的問題也可能幫助到其他有相同問題的人。