mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2024-11-25 14:11:03 +01:00
Optimized keyboard color logic
This commit is contained in:
parent
746ba54c60
commit
bc0e5f33b8
@ -1,7 +1,9 @@
|
||||
#!/bin/bash
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
# Check if the vendor and product ID is provided
|
||||
if [ -z "$1" ]; then
|
||||
if [ -z "${1-}" ]; then
|
||||
echo "Error: Vendor and product ID is missing."
|
||||
echo "Usage: /opt/keyboard_color.sh <vendor_and_product_id>"
|
||||
exit 1
|
||||
@ -11,48 +13,79 @@ fi
|
||||
vendor_and_product_id=$1
|
||||
|
||||
# Define the color transition parameters
|
||||
fade_duration=1800 # 30 minutes in seconds
|
||||
transition_start="06:00"
|
||||
transition_end="06:30"
|
||||
red_start="18:00"
|
||||
red_end="06:00"
|
||||
red_color="ff0000"
|
||||
noon_color="ffffff"
|
||||
twilight_color="ff00a8"
|
||||
sunset_color="ff0000"
|
||||
dawn_color="ff0000"
|
||||
sunrise_color="00e4ff"
|
||||
|
||||
# Function to calculate the color based on the transition ratio
|
||||
calculate_color() {
|
||||
local color_start=$1
|
||||
local color_end=$2
|
||||
local transition_ratio=$3
|
||||
|
||||
local start_value=$((16#${color_start}))
|
||||
local end_value=$((16#${color_end}))
|
||||
|
||||
local current_value=$(awk "BEGIN { value = ${start_value} + (${end_value} - ${start_value}) * ${transition_ratio}; printf(\"%.0f\", value) }")
|
||||
|
||||
printf "%06x" "${current_value}"
|
||||
}
|
||||
|
||||
# Get the current time in HH:MM format
|
||||
current_time=$(date +%H:%M)
|
||||
|
||||
# Check if it's within the red period
|
||||
if [[ "$current_time" > "$red_start" || "$current_time" < "$red_end" ]]; then
|
||||
# Set the color to red directly
|
||||
sudo msi-perkeyrgb --model GS65 -s "$red_color" --id "$vendor_and_product_id"
|
||||
# Calculate the transition ratio based on the current time
|
||||
if [[ "$current_time" > "21:00" || "$current_time" < "06:00" ]]; then
|
||||
# Transition from sunset to dawn (21:00 to 06:00)
|
||||
color_start="ff0000"
|
||||
color_end="ff0000"
|
||||
color_start_time="21:00"
|
||||
color_end_time="00:00"
|
||||
elif [[ "$current_time" > "06:00" && "$current_time" < "09:00" ]]; then
|
||||
# Transition from dawn to sunrise (06:00 to 09:00)
|
||||
color_start="ff0000"
|
||||
color_end="00e4ff"
|
||||
color_start_time="06:00"
|
||||
color_end_time="09:00"
|
||||
elif [[ "$current_time" > "09:00" && "$current_time" < "12:00" ]]; then
|
||||
# Transition from sunrise to noon (09:00 to 12:00)
|
||||
color_start="00e4ff"
|
||||
color_end="ffffff"
|
||||
color_start_time="09:00"
|
||||
color_end_time="12:00"
|
||||
elif [[ "$current_time" > "12:00" && "$current_time" < "18:00" ]]; then
|
||||
# Transition from noon to twilight (12:00 to 18:00)
|
||||
color_start="ffffff"
|
||||
color_end="ff00a8"
|
||||
color_start_time="12:00"
|
||||
color_end_time="18:00"
|
||||
else
|
||||
# Check if it's within the fading period
|
||||
if [[ "$current_time" > "$transition_start" && "$current_time" < "$transition_end" ]]; then
|
||||
# Calculate the transition ratio within the fading period
|
||||
start_seconds=$(date -d "$transition_start" +%s)
|
||||
end_seconds=$(date -d "$transition_end" +%s)
|
||||
current_seconds=$(date -d "$current_time" +%s)
|
||||
transition_ratio=$(awk "BEGIN { ratio = ($current_seconds - $start_seconds) / ($end_seconds - $start_seconds); print ratio }")
|
||||
|
||||
# Calculate the current color based on the transition ratio
|
||||
r=$(awk "BEGIN { value = 255 - 255 * $transition_ratio; printf(\"%.0f\", value) }")
|
||||
g=0
|
||||
b=0
|
||||
else
|
||||
# Calculate the transition ratio based on the time of day
|
||||
start_seconds=$(date -d "$transition_end" +%s)
|
||||
end_seconds=$(date -d "$red_start" +%s)
|
||||
current_seconds=$(date -d "$current_time" +%s)
|
||||
transition_ratio=$(awk "BEGIN { ratio = ($current_seconds - $start_seconds) / ($end_seconds - $start_seconds); print ratio }")
|
||||
|
||||
# Calculate the current color based on the transition ratio
|
||||
r=$(awk "BEGIN { value = 255 * $transition_ratio; printf(\"%.0f\", value) }")
|
||||
g=$(awk "BEGIN { value = 0 + (255 - 0) * $transition_ratio; printf(\"%.0f\", value) }")
|
||||
b=$(awk "BEGIN { value = 0 + (255 - 0) * $transition_ratio; printf(\"%.0f\", value) }")
|
||||
fi
|
||||
# Convert the RGB values to hexadecimal format
|
||||
current_color=$(printf '%02x%02x%02x' $r $g $b)
|
||||
|
||||
# Set the color using msi-perkeyrgb
|
||||
msi-perkeyrgb --model GS65 -s "$current_color" --id "$vendor_and_product_id"
|
||||
# Transition from twilight to sunset (18:00 to 21:00)
|
||||
color_start="ff00a8"
|
||||
color_end="ff0000"
|
||||
color_start_time="18:00"
|
||||
color_end_time="21:00"
|
||||
fi
|
||||
|
||||
# Get the current date in YYYY-MM-DD format
|
||||
current_date=$(date +%Y-%m-%d)
|
||||
|
||||
# Calculate the start and end timestamps based on the current date and time
|
||||
start_timestamp=$(date -d "${current_date} ${color_start_time}" +%s)
|
||||
end_timestamp=$(date -d "${current_date} ${color_end_time}" +%s)
|
||||
|
||||
# Get the current timestamp
|
||||
current_timestamp=$(date +%s)
|
||||
|
||||
# Calculate the transition ratio
|
||||
transition_ratio=$(awk "BEGIN { ratio = ($current_timestamp - $start_timestamp) / ($end_timestamp - $start_timestamp); print ratio }")
|
||||
|
||||
# Calculate the current color based on the transition ratio
|
||||
current_color=$(calculate_color "$color_start" "$color_end" "$transition_ratio")
|
||||
|
||||
echo "changing keyboard color to #$current_color."
|
||||
|
||||
# Set the color using msi-perkeyrgb
|
||||
msi-perkeyrgb --model GS65 -s "$current_color" --id "$vendor_and_product_id"
|
Loading…
Reference in New Issue
Block a user