Created
May 18, 2022 05:49
-
-
Save ashleysommer/9550cba6989c96076897234a14367f9e to your computer and use it in GitHub Desktop.
Lauch alacritty with a random hue shift on foreground and background colors
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 | |
DEFAULT_BACKGROUND='#012b36' | |
DEFAULT_FOREGROUND='#839496' | |
HEX_RGB_RET=(0 0 0) | |
RGB_HEX_IN=("0" "0" "0") | |
RGB_HEX_RET="" | |
function _hex_to_rgb { | |
first=${1: 0:1} | |
if [[ "$first" = "#" ]]; then | |
HEX_RGB_RET[0]=$((16#${1: 1:2})) | |
HEX_RGB_RET[1]=$((16#${1: 3:2})) | |
HEX_RGB_RET[2]=$((16#${1: 5:2})) | |
else | |
echo "Bad HEX code, should be '#aabbcc'" | |
fi | |
} | |
function _rgb_to_hex { | |
printf -v RGB_HEX_RET "#%02x%02x%02x" ${RGB_HEX_IN[0]} ${RGB_HEX_IN[1]} ${RGB_HEX_IN[2]} | |
} | |
function _rgb_to_hsl { | |
echo "$1,$2,$3" | awk -F "[,]+" '\ | |
# Returns minimum number | |
function abs(num) { | |
if (num < 0.0) | |
return num*-1.0 | |
return num | |
} | |
function find_min(num1, num2, num3){ | |
if (num1 < num2) | |
a=num1 | |
else | |
a=num2 | |
if (a < num3) | |
return a | |
return num3 | |
} | |
# Returns maximum number | |
function find_max(num1, num2, num3){ | |
if (num1 > num2) | |
a=num1 | |
else | |
a=num2 | |
if (a > num3) | |
return a | |
return num3 | |
} | |
function to_hsl(Rd, Gd, Bd){ | |
Rf=Rd/255.0 | |
Gf=Gd/255.0 | |
Bf=Bd/255.0 | |
# Find minimum number | |
m=find_min(Rf, Gf, Bf) | |
# Find maximum number | |
M=find_max(Rf, Gf, Bf) | |
chroma=M-m | |
L=(M+m)/2.0 | |
if (chroma == 0.0) { | |
H=0.0 | |
S=0.0 | |
} else { | |
if (M == Bf) | |
Hprime=((Rf-Gf)/chroma)+4.0 | |
else if (M == Gf) | |
Hprime=((Bf-Rf)/chroma)+2.0 | |
else | |
Hprime=((Gf-Bf)/chroma)%6.0 | |
H=Hprime*60.0 | |
S=chroma/(1.0-abs((2.0*L)-1.0)) | |
} | |
print H "," S "," L | |
} | |
# Script execution starts here | |
{ | |
to_hsl($1, $2, $3) | |
}' | |
} | |
function _hsl_to_rgb { | |
echo "$1,$2,$3" | awk -F "[,]+" '\ | |
function abs(num) { | |
if (num < 0.0) | |
return num*-1.0 | |
return num | |
} | |
function to_rgb(H, S, L){ | |
H=H%360.0 | |
S=abs(S) | |
if (S < 0.0) S=0.0 | |
if (S > 1.0) S=1.0 | |
L=abs(L) | |
if (L < 0.0) L=0.0 | |
if (L > 1.0) L=1.0 | |
C=(1.0-abs((2.0*L)-1.0))*S | |
X=C*(1.0-abs(((H/60.0)%2.0)-1.0)) | |
m=L-(C/2.0) | |
if (H >= 300.0) { | |
Rp=C; Gp=0.0; Bp=X | |
} else if (H >= 240.0) { | |
Rp=X; Gp=0.0; Bp=C | |
} else if (H >= 180.0) { | |
Rp=0.0; Gp=X; Bp=C | |
} else if (H >= 120.0) { | |
Rp=0.0; Gp=C; Bp=X | |
} else if (H >= 60.0) { | |
Rp=X; Gp=C; Bp=0.0 | |
} else { | |
Rp=C; Gp=X; Bp=0.0 | |
} | |
R=(Rp+m)*255.0 | |
G=(Gp+m)*255.0 | |
B=(Bp+m)*255.0 | |
print int(R+0.5) "," int(G+0.5) "," int(B+0.5) | |
} | |
{ | |
to_rgb($1*1.0, $2*1.0, $3*1.0) | |
}' | |
} | |
function _rotate_angle { | |
echo "$1,$2" | awk -F "[,]+" '\ | |
function rotate_angle(A, R){ | |
R=R%32768.0 | |
amount=1.0+((R/32767.0) * 358.0) | |
print (A+amount)%360.0 | |
} | |
{rotate_angle($1*1.0, $2*1.0)}' | |
} | |
function _add_offset { | |
echo "$1,$2,$3" | awk -F "[,]+" '\ | |
function add_offset(S,D,R){ | |
R=R%32768.0 | |
amount=((R/32767.0)*D*2.0)-D | |
S=S+amount | |
if (S < 0.0) S=0.0 | |
if (S > 1.0) S=1.0 | |
print S | |
} | |
{add_offset($1*1.0, $2*1.0, $3*1.0)}' | |
} | |
_hex_to_rgb "$DEFAULT_FOREGROUND" | |
HSLstring="$(_rgb_to_hsl ${HEX_RGB_RET[0]} ${HEX_RGB_RET[1]} ${HEX_RGB_RET[2]})" | |
readarray -td, FOREGROUND_HSL <<<"$HSLstring,"; | |
_hex_to_rgb "$DEFAULT_BACKGROUND" | |
HSLstring="$(_rgb_to_hsl ${HEX_RGB_RET[0]} ${HEX_RGB_RET[1]} ${HEX_RGB_RET[2]})" | |
readarray -td, BACKGROUND_HSL <<<"$HSLstring,"; | |
RAND1=$RANDOM | |
RAND2=$RANDOM | |
FOREGROUND_HSL[0]=$(_rotate_angle ${FOREGROUND_HSL[0]} $RAND1) | |
BACKGROUND_HSL[0]=$(_rotate_angle ${BACKGROUND_HSL[0]} $RAND1) | |
FOREGROUND_HSL[1]=$(_add_offset ${FOREGROUND_HSL[1]} "0.4" $RAND2) | |
BACKGROUND_HSL[1]=$(_add_offset ${BACKGROUND_HSL[1]} "0.4" $RAND2) | |
RGBstring="$(_hsl_to_rgb ${FOREGROUND_HSL[0]} ${FOREGROUND_HSL[1]} ${FOREGROUND_HSL[2]})" | |
readarray -td, RGB_HEX_IN <<<"$RGBstring,"; | |
_rgb_to_hex | |
NEW_FOREGROUND="-o colors.primary.foreground='$RGB_HEX_RET'" | |
RGBstring="$(_hsl_to_rgb ${BACKGROUND_HSL[0]} ${BACKGROUND_HSL[1]} ${BACKGROUND_HSL[2]})" | |
readarray -td, RGB_HEX_IN <<<"$RGBstring,"; | |
_rgb_to_hex | |
NEW_BACKGROUND="-o colors.primary.background='$RGB_HEX_RET'" | |
exec alacritty $NEW_FOREGROUND $NEW_BACKGROUND "$@" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment