Created
April 6, 2025 17:45
Poor man's rainmeter - mond
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
num_colors=5 # Analyze the 5 most dominant colors | |
# Function to check if a string is a 6-digit hexadecimal code | |
function is_valid_hexadecimal { | |
local hex="$1" | |
[[ "$hex" =~ ^#[0-9a-fA-F]{6}$ ]] && return 0 || return 1 | |
} | |
# Function to convert a 6-digit hexadecimal color code to RGB | |
function hex_to_rgb { | |
local hex="$1" | |
if is_valid_hexadecimal "$hex"; then | |
local r g b | |
r=$(printf '%d' "0x${hex:1:2}") | |
g=$(printf '%d' "0x${hex:3:2}") | |
b=$(printf '%d' "0x${hex:5:2}") | |
echo "$r $g $b" | |
else | |
echo "0 0 0" # Return black if the format is invalid | |
fi | |
} | |
# Function to convert RGB to hexadecimal | |
function rgb_to_hex { | |
local r="$1" | |
local g="$2" | |
local b="$3" | |
printf "#%02x%02x%02x" "$r" "$g" "$b" | |
} | |
# Function to calculate the complementary color from RGB | |
function calculate_complementary_rgb { | |
local r="$1" | |
local g="$2" | |
local b="$3" | |
local comp_r=$((255 - r)) | |
local comp_g=$((255 - g)) | |
local comp_b=$((255 - b)) | |
echo "$comp_r $comp_g $comp_b" | |
} | |
function calculate_luminosity { | |
local rgb_str="$1" | |
local r=$(echo "$rgb_str" | awk '{print $1}') | |
local g=$(echo "$rgb_str" | awk '{print $2}') | |
local b=$(echo "$rgb_str" | awk '{print $3}') | |
local lum=$(( (299 * r + 587 * g + 114 * b) / 1000 )) | |
echo "$lum" | |
} | |
resolution=$(xrandr --current | grep '*' | awk '{print $1}') | |
if [[ -n "$resolution" ]]; then | |
WIDTH=$(echo "$resolution" | cut -d 'x' -f 1) | |
HEIGHT=$(echo "$resolution" | cut -d 'x' -f 2) | |
echo "Screen width (X): $WIDTH" | |
echo "Screen height (Y): $HEIGHT" | |
fi | |
OBJPATH="$HOME/.wallpapers" | |
image="$OBJPATH/$(date +"%d-%m-%y").jpg" | |
wget -O "$image" "https://picsum.photos/$WIDTH/$HEIGHT/?random" | |
# Get the dominant colors and filter only valid hexadecimal codes | |
dominant_colors_hex=$(convert "$image" -colors "$num_colors" -unique-colors txt: | grep -o '#[0-9a-fA-F]*' | grep -E '^#[0-9a-fA-F]{6}$') | |
text_color="white" # Default text color | |
darkest_dominant_color_hex="" | |
darkest_dominant_rgb="0 0 0" # Initialize with black | |
darkest_dominant_luminosity=256 # Initialize with a high value | |
# Iterate over the dominant colors and find the darkest one (lowest luminosity) | |
while IFS= read -r color_hex; do | |
rgb=$(hex_to_rgb "$color_hex") | |
r=$(echo "$rgb" | awk '{print $1}') | |
g=$(echo "$rgb" | awk '{print $2}') | |
b=$(echo "$rgb" | awk '{print $3}') | |
# Ensure that the RGB conversion was successful | |
if [[ "$r" -ne 0 || "$g" -ne 0 || "$b" -ne 0 ]]; then | |
luminosity=$(calculate_luminosity "$rgb") | |
if [[ "$luminosity" -lt "$darkest_dominant_luminosity" ]]; then | |
darkest_dominant_luminosity="$luminosity" | |
darkest_dominant_color_hex="$color_hex" | |
darkest_dominant_rgb="$r $g $b" | |
fi | |
fi | |
done <<< "$dominant_colors_hex" | |
echo "Darkest dominant color (hex): $darkest_dominant_color_hex (RGB: $darkest_dominant_rgb)" | |
# Calculate the complementary color of the darkest dominant color | |
r_dom=$(echo "$darkest_dominant_rgb" | awk '{print $1}') | |
g_dom=$(echo "$darkest_dominant_rgb" | awk '{print $2}') | |
b_dom=$(echo "$darkest_dominant_rgb" | awk '{print $3}') | |
complementary_rgb=$(calculate_complementary_rgb "$r_dom" "$g_dom" "$b_dom") | |
comp_r=$(echo "$complementary_rgb" | awk '{print $1}') | |
comp_g=$(echo "$complementary_rgb" | awk '{print $2}') | |
comp_b=$(echo "$complementary_rgb" | awk '{print $3}') | |
text_color_hex=$(rgb_to_hex "$comp_r" "$comp_g" "$comp_b") | |
echo "Complementary text color (hex): $text_color_hex" | |
height=$(identify -format "%h" "$image") | |
margin=$((height / 6)) | |
margin2=$((2 * margin)) | |
line2=$(date +'%d %^B %Y') | |
convert "$image" -font Anurati-Regular -gravity North \ | |
-pointsize 100 -fill "$text_color_hex" \ | |
-annotate +0+"$margin" "$(date +'%^A')" \ | |
-font DejaVu-Sans -pointsize 50 -fill $text_color_hex -annotate +0+"$margin2" "$line2" \ | |
"$OBJPATH/wallpaper.jpg" | |
feh --bg-fill "$OBJPATH/wallpaper.jpg" | |
echo "Image with text generated in wallpaper.jpg with text color: $text_color_hex" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here you can find my version of the rainmeter - mond , really love that, but it does not work with my setup, where is a simple script that you can launch at system start. Hope you find it useful