mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2024-11-13 00:11:05 +01:00
Optimized keyboard color logic
This commit is contained in:
parent
746ba54c60
commit
bc0e5f33b8
@ -1,7 +1,9 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
# Check if the vendor and product ID is provided
|
# 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 "Error: Vendor and product ID is missing."
|
||||||
echo "Usage: /opt/keyboard_color.sh <vendor_and_product_id>"
|
echo "Usage: /opt/keyboard_color.sh <vendor_and_product_id>"
|
||||||
exit 1
|
exit 1
|
||||||
@ -11,48 +13,79 @@ fi
|
|||||||
vendor_and_product_id=$1
|
vendor_and_product_id=$1
|
||||||
|
|
||||||
# Define the color transition parameters
|
# Define the color transition parameters
|
||||||
fade_duration=1800 # 30 minutes in seconds
|
noon_color="ffffff"
|
||||||
transition_start="06:00"
|
twilight_color="ff00a8"
|
||||||
transition_end="06:30"
|
sunset_color="ff0000"
|
||||||
red_start="18:00"
|
dawn_color="ff0000"
|
||||||
red_end="06:00"
|
sunrise_color="00e4ff"
|
||||||
red_color="ff0000"
|
|
||||||
|
# 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
|
# Get the current time in HH:MM format
|
||||||
current_time=$(date +%H:%M)
|
current_time=$(date +%H:%M)
|
||||||
|
|
||||||
# Check if it's within the red period
|
# Calculate the transition ratio based on the current time
|
||||||
if [[ "$current_time" > "$red_start" || "$current_time" < "$red_end" ]]; then
|
if [[ "$current_time" > "21:00" || "$current_time" < "06:00" ]]; then
|
||||||
# Set the color to red directly
|
# Transition from sunset to dawn (21:00 to 06:00)
|
||||||
sudo msi-perkeyrgb --model GS65 -s "$red_color" --id "$vendor_and_product_id"
|
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
|
else
|
||||||
# Check if it's within the fading period
|
# Transition from twilight to sunset (18:00 to 21:00)
|
||||||
if [[ "$current_time" > "$transition_start" && "$current_time" < "$transition_end" ]]; then
|
color_start="ff00a8"
|
||||||
# Calculate the transition ratio within the fading period
|
color_end="ff0000"
|
||||||
start_seconds=$(date -d "$transition_start" +%s)
|
color_start_time="18:00"
|
||||||
end_seconds=$(date -d "$transition_end" +%s)
|
color_end_time="21:00"
|
||||||
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"
|
|
||||||
fi
|
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