Added color conversion scripts

This commit is contained in:
z3rOR0ne 2022-12-22 20:32:47 -08:00
parent 286395a230
commit eaef5a8fc3
3 changed files with 140 additions and 0 deletions

53
scripts/hex2hsl.sh Executable file
View file

@ -0,0 +1,53 @@
#!/usr/bin/env bash
# Prompt user for hex value
echo -n "Enter hex color code: "
read hex
# Convert hex to RGB
r=$((16#${hex:1:2}))
g=$((16#${hex:3:2}))
b=$((16#${hex:5:2}))
# Convert RGB to HSL
r=$((r/255))
g=$((g/255))
b=$((b/255))
max=${r}
min=${r}
if [[ ${g} -gt ${max} ]]; then max=${g}; fi
if [[ ${b} -gt ${max} ]]; then max=${b}; fi
if [[ ${g} -lt ${min} ]]; then min=${g}; fi
if [[ ${b} -lt ${min} ]]; then min=${b}; fi
l=$(((max+min)/2))
if [[ ${max} == ${min} ]]; then
h=0
s=0
else
d=$((max-min))
if [[ $(awk "BEGIN {print (${l} > 0.5)}") -eq 1 ]]; then
s=$((d/(2-max-min)))
else
s=$((d/(max+min)))
fi
case ${max} in
${r}) h=$(((g-b)/d))
if [[ ${h} -lt 0 ]]; then h=$((h+6)); fi
;;
${g}) h=$((2+(b-r)/d))
;;
${b}) h=$((4+(r-g)/d))
;;
esac
h=$((h*60))
fi
# Assign alpha value (default is 1.0)
a=1.0
# Output HSLA values
echo ${h} ${s} ${l} ${a}

42
scripts/hsl2hex.sh Executable file
View file

@ -0,0 +1,42 @@
#!/usr/bin/env bash
set -e -o pipefail
# Prompt user for HSLA values
echo -n "Enter HSLA values: "
read h s l a
# Convert HSL to RGB
if [[ ${s} -eq 0 ]]; then
r=$((l*255))
g=$((l*255))
b=$((l*255))
else
if [[ $(echo "${l} < 0.5" | bc -l) -eq 1 ]]; then q=$((l*(1+s))); else q=$((l+s-(l*s))); fi
p=$((2*l-q))
r=$(printf "%.0f" $(echo "(${p}+(${q}-${p})*${hue_to_rgb(p,q,$(((h+120)%360)))})" | bc -l))
g=$(printf "%.0f" $(echo "(${p}+(${q}-${p})*${hue_to_rgb(p,q,${h})})" | bc -l))
b=$(printf "%.0f" $(echo "(${p}+(${q}-${p})*${hue_to_rgb(p,q,$(((h-120+360)%360)))})" | bc -l))
fi
# Convert RGB to hex
r=$((r/16))
g=$((g/16))
b=$((b/16))
hex="#$(printf "%x" ${r})$(printf "%x" ${g})$(printf "%x" ${b})"
# Output hex color code
echo ${hex}
# Helper function to convert hue to RGB
hue_to_rgb() {
p=${1}
q=${2}
t=${3}
if [[ $(echo "${t} < 0" | bc -l) -eq 1 ]]; then t=$((t+360)); fi
if [[ $(echo "${t} > 360" | bc -l) -eq 1 ]]; then t=$((t-360)); fi
if [[ $(echo "${t} < 60" | bc -l) -eq 1 ]]; then return $(echo "(${p}+(${q}-${p})*${t}/60)" | bc -l); fi
if [[ $(echo "${t} < 180" | bc -l) -eq 1 ]]; then return $(echo "${q}" | bc -l); fi
if [[ $(echo "${t} < 240" | bc -l) -eq 1 ]]; then return $(echo "(${p}+(${q}-${p})*(240-${t})/60)" | bc -l); fi
return $(echo "${p}" | bc -l)
}

45
scripts/rgb2hsl.sh Executable file
View file

@ -0,0 +1,45 @@
#!/usr/bin/env bash
# Prompt user for RGB or RGBA values
echo -n "Enter RGB or RGBA values: "
read r g b a
# Convert RGB to HSL
r=$((r/255))
g=$((g/255))
b=$((b/255))
max=${r}
min=${r}
if [[ ${g} -gt ${max} ]]; then max=${g}; fi
if [[ ${b} -gt ${max} ]]; then max=${b}; fi
if [[ ${g} -lt ${min} ]]; then min=${g}; fi
if [[ ${b} -lt ${min} ]]; then min=${b}; fi
l=$(((max+min)/2))
if [[ ${max} == ${min} ]]; then
h=0
s=0
else
d=$((max-min))
if [[ $(echo "${l} > 0.5" | bc -l) -eq 1 ]]; then
s=$((d/(2-max-min)))
else
s=$((d/(max+min)))
fi
case ${max} in
${r}) h=$(((g-b)/d))
if [[ ${h} -lt 0 ]]; then h=$((h+6)); fi
;;
${g}) h=$((2+(b-r)/d))
;;
${b}) h=$((4+(r-g)/d))
;;
esac
h=$((h*60))
fi
# Output HSLA values
echo ${h} ${s} ${l} ${a}