Last active
May 3, 2026 18:50
-
-
Save kmrk/4ab7ddfffafb1d3c901240e4893a1019 to your computer and use it in GitHub Desktop.
24 bit color test with qsort animation
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 | |
| COLS=${1:-16} | |
| ROWS=${2:-15} | |
| DELAY=${3:-0.005} | |
| TOTAL=$((COLS * ROWS)) | |
| declare -a R G B ORD | |
| init() { | |
| for ((i=0; i<TOTAL; i++)); do | |
| hue=$((i * 360 / TOTAL)) | |
| sec=$((hue / 60)) | |
| f=$((hue % 60)) | |
| q=$((255 * (60 - f) / 60)) | |
| t=$((255 * f / 60)) | |
| case $sec in | |
| 0) R[i]=255; G[i]=$t; B[i]=0 ;; | |
| 1) R[i]=$q; G[i]=255; B[i]=0 ;; | |
| 2) R[i]=0; G[i]=255; B[i]=$t ;; | |
| 3) R[i]=0; G[i]=$q; B[i]=255 ;; | |
| 4) R[i]=$t; G[i]=0; B[i]=255 ;; | |
| *) R[i]=255; G[i]=0; B[i]=$q ;; | |
| esac | |
| ORD[i]=$i | |
| done | |
| } | |
| swap() { | |
| local a=$1 b=$2 tmp | |
| tmp=${R[a]}; R[a]=${R[b]}; R[b]=$tmp | |
| tmp=${G[a]}; G[a]=${G[b]}; G[b]=$tmp | |
| tmp=${B[a]}; B[a]=${B[b]}; B[b]=$tmp | |
| tmp=${ORD[a]}; ORD[a]=${ORD[b]}; ORD[b]=$tmp | |
| } | |
| shuffle() { | |
| for ((i=TOTAL-1; i>0; i--)); do | |
| swap "$i" "$((RANDOM % (i + 1)))" | |
| done | |
| } | |
| draw_all() { | |
| local n=0 | |
| for ((row=0; row<ROWS; row++)); do | |
| for ((col=0; col<COLS; col++)); do | |
| printf "\e[48;2;%d;%d;%dm " "${R[n]}" "${G[n]}" "${B[n]}" | |
| ((n++)) | |
| done | |
| printf "\e[0m\n" | |
| done | |
| printf "\e[0mENTER=sorted | Ctrl+C=exit\n" | |
| } | |
| draw_cell() { | |
| local idx=$1 r g b | |
| local row_up=$((ROWS + 1 - idx / COLS)) | |
| local col=$((idx % COLS * 2 + 1)) | |
| if (($# == 4)); then | |
| r=$2; g=$3; b=$4 | |
| else | |
| r=${R[idx]}; g=${G[idx]}; b=${B[idx]} | |
| fi | |
| printf "\e[%dA\e[%dG\e[48;2;%d;%d;%dm \e[0m\e[%dB\e[1G" "$row_up" "$col" "$r" "$g" "$b" "$row_up" | |
| } | |
| restore_original() { | |
| for ((i=0; i<TOTAL; i++)); do | |
| for ((j=i; j<TOTAL; j++)); do | |
| if ((ORD[j] == i)); then | |
| swap "$i" "$j" | |
| break | |
| fi | |
| done | |
| done | |
| } | |
| sort_animate() { | |
| for ((i=0; i<TOTAL-1; i++)); do | |
| min=$i | |
| for ((j=i+1; j<TOTAL; j++)); do | |
| ((ORD[j] < ORD[min])) && min=$j | |
| done | |
| if ((min != i)); then | |
| draw_cell "$i" 255 255 0 | |
| draw_cell "$min" 255 255 0 | |
| swap "$i" "$min" | |
| IFS= read -t "$DELAY" -r key 2>/dev/null && { | |
| restore_original | |
| printf "\e[%dA" "$((ROWS + 1))" | |
| draw_all | |
| return 1 | |
| } | |
| draw_cell "$i" | |
| draw_cell "$min" | |
| fi | |
| done | |
| } | |
| main() { | |
| printf "\e[?25l" | |
| init | |
| shuffle | |
| draw_all | |
| sleep 0.3 | |
| stty -echo -icanon time 0 min 0 2>/dev/null | |
| sort_animate | |
| stty sane 2>/dev/null | |
| printf "\e[?25h\e[0m\n" | |
| } | |
| main |
kmrk
commented
May 3, 2026
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment