patched rainbow text
This commit is contained in:
parent
ff9e852444
commit
de96d1f38e
@ -11,31 +11,38 @@ NC='\033[0m'
|
||||
|
||||
render_gradient_string() {
|
||||
local input="$1"
|
||||
local regex='#([0-9a-fA-F]{6})(.)'
|
||||
while [[ "$input" =~ $regex ]]; do
|
||||
color="${BASH_REMATCH[1]}"
|
||||
char="${BASH_REMATCH[2]}"
|
||||
r=$((16#${color:0:2}))
|
||||
g=$((16#${color:2:2}))
|
||||
b=$((16#${color:4:2}))
|
||||
printf "\e[38;2;%d;%d;%dm%s" "$r" "$g" "$b" "$char"
|
||||
input="${input#"#${color}${char}"}"
|
||||
local regex='^#([0-9a-fA-F]{6})(.)'
|
||||
local rest
|
||||
|
||||
while [[ -n "$input" ]]; do
|
||||
if [[ "$input" =~ $regex ]]; then
|
||||
color="${BASH_REMATCH[1]}"
|
||||
char="${BASH_REMATCH[2]}"
|
||||
r=$((16#${color:0:2}))
|
||||
g=$((16#${color:2:2}))
|
||||
b=$((16#${color:4:2}))
|
||||
printf "\e[38;2;%d;%d;%dm%s" "$r" "$g" "$b" "$char"
|
||||
input="${input:9}" # strip exactly 7 (color) + 1 (char) = 8 chars, plus 1 for #
|
||||
else
|
||||
printf "%s" "${input:0:1}"
|
||||
input="${input:1}"
|
||||
fi
|
||||
done
|
||||
|
||||
echo -e "\e[0m"
|
||||
}
|
||||
|
||||
|
||||
|
||||
rainbowize_text() {
|
||||
local text="$1"
|
||||
local freq=0.15
|
||||
local i=0
|
||||
local output=""
|
||||
local pi=3.14159265
|
||||
local output=""
|
||||
|
||||
clamp() {
|
||||
local val="$1"
|
||||
local min="$2"
|
||||
local max="$3"
|
||||
local val="$1" min="$2" max="$3"
|
||||
if (( val < min )); then echo "$min"
|
||||
elif (( val > max )); then echo "$max"
|
||||
else echo "$val"
|
||||
@ -45,6 +52,7 @@ rainbowize_text() {
|
||||
while IFS= read -r line || [ -n "$line" ]; do
|
||||
for (( j=0; j<${#line}; j++ )); do
|
||||
char="${line:$j:1}"
|
||||
|
||||
r=$(awk -v i="$i" -v f="$freq" 'BEGIN { printf("%d", 127 * (sin(f*i + 0) + 1)) }')
|
||||
g=$(awk -v i="$i" -v f="$freq" 'BEGIN { printf("%d", 127 * (sin(f*i + 2*3.1415/3) + 1)) }')
|
||||
b=$(awk -v i="$i" -v f="$freq" 'BEGIN { printf("%d", 127 * (sin(f*i + 4*3.1415/3) + 1)) }')
|
||||
@ -53,14 +61,10 @@ rainbowize_text() {
|
||||
g=$(clamp "$g" 50 230)
|
||||
b=$(clamp "$b" 50 230)
|
||||
|
||||
rh=$(printf "%02x" "$r")
|
||||
gh=$(printf "%02x" "$g")
|
||||
bh=$(printf "%02x" "$b")
|
||||
|
||||
output+="#${rh}${gh}${bh}${char}"
|
||||
output+="#$(printf '%02x%02x%02x' "$r" "$g" "$b")$char"
|
||||
((i++))
|
||||
done
|
||||
output+=$'\n' # Important!
|
||||
output+=$'\n'
|
||||
done <<< "$text"
|
||||
|
||||
echo -ne "$output"
|
||||
@ -69,6 +73,7 @@ rainbowize_text() {
|
||||
|
||||
|
||||
|
||||
|
||||
# rainbowize_text() {
|
||||
# local text="$1"
|
||||
# local freq=0.15
|
||||
|
Loading…
x
Reference in New Issue
Block a user