本文目录导读:

我来帮你编写定时调整色温的脚本,这里提供几种常见场景的实现方案:
Linux/Windows 通用脚本(Python)
#!/usr/bin/env python3
"""
定时调整色温脚本
支持 Linux (redshift) 和 Windows (Windows夜灯)
"""
import time
import datetime
import subprocess
import platform
import json
import os
class ColorTemperatureScheduler:
def __init__(self):
self.system = platform.system()
self.config_file = os.path.expanduser("~/.color_temp_config.json")
self.load_config()
def load_config(self):
"""加载配置文件"""
default_config = {
"schedule": [
{
"start": "06:00",
"end": "18:00",
"temperature": 6500 # 白天色温
},
{
"start": "18:00",
"end": "22:00",
"temperature": 4500 # 傍晚色温
},
{
"start": "22:00",
"end": "06:00",
"temperature": 3500 # 夜间色温
}
],
"enabled": True
}
if os.path.exists(self.config_file):
with open(self.config_file, 'r') as f:
self.config = json.load(f)
else:
self.config = default_config
self.save_config()
def save_config(self):
"""保存配置"""
with open(self.config_file, 'w') as f:
json.dump(self.config, f, indent=2)
def get_current_temperature(self):
"""根据当前时间获取色温值"""
now = datetime.datetime.now()
current_time = now.strftime("%H:%M")
for schedule in self.config["schedule"]:
start = schedule["start"]
end = schedule["end"]
if start <= end:
# 同一天内的时段
if start <= current_time <= end:
return schedule["temperature"]
else:
# 跨越午夜的时段
if current_time >= start or current_time <= end:
return schedule["temperature"]
# 默认返回最高色温
return 6500
def apply_temperature(self, temperature):
"""应用色温设置"""
if self.system == "Linux":
self.apply_linux(temperature)
elif self.system == "Windows":
self.apply_windows(temperature)
elif self.system == "Darwin": # macOS
self.apply_mac(temperature)
def apply_linux(self, temperature):
"""Linux 使用 Redshift"""
try:
# 先清除现有的 redshift 进程
subprocess.run(["pkill", "-f", "redshift"], capture_output=True)
# 启动新的 redshift
subprocess.Popen([
"redshift",
"-O", str(temperature),
"-b", "0.8:0.7" # 亮度和伽马
], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
print(f"[{datetime.datetime.now()}] 色温已调整为: {temperature}K")
except FileNotFoundError:
print("请先安装 redshift: sudo apt install redshift")
def apply_windows(self, temperature):
"""Windows 使用注册表夜灯设置"""
try:
# 计算亮度值 (6500K = 最高色温)
# Windows 夜灯范围 1200-6500K
if temperature < 1200:
temperature = 1200
elif temperature > 6500:
temperature = 6500
# 使用 PowerShell 设置夜灯
settings = f"""
New-ItemProperty -Path "HKCU:\\Software\\Microsoft\\Windows\\CurrentVersion\\CloudStore\\Store\\Cache\\DefaultAccount"
-Name "NightLight" -Value "{temperature}" -PropertyType String -Force
"""
subprocess.run(["powershell", "-Command", settings], capture_output=True)
print(f"[{datetime.datetime.now()}] 已设置 Windows 夜灯色温: {temperature}K")
except Exception as e:
print(f"设置 Windows 夜灯失败: {e}")
def apply_mac(self, temperature):
"""macOS 使用夜览设置"""
try:
# macOS 夜览设置需要 GUI 工具,这里使用 AppleScript
script = f'''
tell application "System Events"
tell process "System Settings"
set frontmost to true
click menu item "Displays" of menu 1 of menu bar 1
delay 1
click radio button "Night Shift" of tab group 1 of window 1
delay 0.5
set value of slider "Color Temperature" to {temperature}
end tell
end tell
'''
subprocess.run(["osascript", "-e", script], capture_output=True)
print(f"[{datetime.datetime.now()}] 已设置 macOS 夜览色温: {temperature}K")
except Exception as e:
print(f"设置 macOS 夜览失败: {e}")
def run_scheduler(self):
"""运行调度器"""
print("色温调度器已启动...")
last_temperature = None
while True:
if self.config["enabled"]:
current_temperature = self.get_current_temperature()
if current_temperature != last_temperature:
self.apply_temperature(current_temperature)
last_temperature = current_temperature
# 每 5 分钟检查一次
time.sleep(300)
def main():
scheduler = ColorTemperatureScheduler()
scheduler.run_scheduler()
if __name__ == "__main__":
main()
简单 Shell 脚本(Linux)
#!/bin/bash
# 简单的色温调度脚本
# 配置色温
DAY_TEMP=6500
NIGHT_TEMP=3500
TRANSITION_START=18 # 18:00 开始过渡
TRANSITION_END=22 # 22:00 完全变暗
while true; do
# 获取当前小时
HOUR=$(date +%H | sed 's/^0*//')
# 根据时间设置色温
if [ $HOUR -ge 6 ] && [ $HOUR -lt 18 ]; then
# 白天
TEMP=$DAY_TEMP
elif [ $HOUR -ge 18 ] && [ $HOUR -lt 22 ]; then
# 过渡期:线性降低色温
PROGRESS=$(( ($HOUR - 18) * 100 / 4 ))
TEMP=$(( $DAY_TEMP - ($DAY_TEMP - $NIGHT_TEMP) * $PROGRESS / 100 ))
else
# 夜间
TEMP=$NIGHT_TEMP
fi
# 应用色温
redshift -O $TEMP 2>/dev/null
echo "$(date): 设置色温为 $TEMP"
# 每 10 分钟检查一次
sleep 600
done
macOS 专用脚本
#!/bin/bash
# macOS 夜览定时脚本
# 语法: osa_scale 开始时间 结束时间 最终亮度
osascript -e '
on run argv
set startHour to item 1 of argv as integer
set endHour to item 2 of argv as integer
set finalBrightness to item 3 of argv as real
set currentHour to ((current date) as hours) mod 24
if currentHour >= startHour and currentHour < endHour then
-- 白天使用正常亮度
set brightnessValue to 1.0
else
-- 夜晚降低亮度
set brightnessValue to finalBrightness
end if
tell application "System Events"
tell dock preferences
set properties to {autohide:false}
end tell
end tell
-- 这里可以设置夜览色温
do shell script "echo 调整色温"
end run
'
# 主循环
while true; do
HOUR=$(date +%H)
if [ $HOUR -ge 6 ] && [ $HOUR -lt 18 ]; then
# 白天 - 正常色温 6500K
osascript -e 'set appearance to light'
elif [ $HOUR -ge 18 ] && [ $HOUR -lt 22 ]; then
# 傍晚 - 中间色温 4500K
osascript -e 'set appearance to dark'
else
# 夜间 - 最低色温 3500K
osascript -e 'set appearance to dark'
fi
sleep 300
done
配置文件示例
{
"schedule": [
{
"start": "06:00",
"end": "09:00",
"temperature": 5500,
"name": "早间"
},
{
"start": "09:00",
"end": "14:00",
"temperature": 6500,
"name": "正午"
},
{
"start": "14:00",
"end": "18:00",
"temperature": 5800,
"name": "午后"
},
{
"start": "18:00",
"end": "22:00",
"temperature": 4500,
"name": "晚间"
},
{
"start": "22:00",
"end": "06:00",
"temperature": 3500,
"name": "夜间"
}
]
}
使用 systemd 服务(Linux)
# /etc/systemd/system/color-temperature.service [Unit] Description=Color Temperature Scheduler After=network.target [Service] Type=simple ExecStart=/usr/bin/python3 /home/user/color_temp_scheduler.py Restart=on-failure User=1000 # 改为你的 UID [Install] WantedBy=multi-user.target
使用方法
-
安装依赖:
- Linux:
sudo apt install redshift - Windows: 已自带夜灯功能
- Linux:
-
运行脚本:
python3 color_temp_scheduler.py
-
设置开机自启(Linux):
sudo systemctl enable color-temperature sudo systemctl start color-temperature
-
自定义时段:修改
config_file中的 JSON 配置
注意事项
- Linux 需要安装 redshift
- Windows 夜灯功能需要 Windows 10/11
- macOS 需要允许辅助功能权限
- 可以根据需要调整色温值和时段
这个脚本会根据一天中的不同时段自动调整屏幕色温,帮助保护眼睛并改善睡眠质量,需要调整具体时段或色温值时,只需修改配置文件即可。
版权声明:NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播