zebulon_fr

Fill console with 24-bit RGB colored blocks/short free demo.

Apr 10th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.48 KB | None | 0 0
  1. #!/bin/bash
  2. # Fill console with 24-bit RGB colored blocks -- short free demo
  3. # Copyright (c)2018/04/10, zebulon_fr
  4. # License: Public Domain ;D
  5. # ---------------------------------------------------------------
  6. shopt -s checkwinsize
  7. export COLUMNS
  8. export LINES
  9. XMAX=$[COLUMNS-1]
  10. YMAX=$[LINES-2]
  11.  
  12. # Random bi-colored block
  13. Random() {
  14.   local Rf Gf Bf Rb Gb Bb
  15.     # Random foreground RGB color
  16.     Rf=$[RANDOM%256]
  17.     Gf=$[RANDOM%256]
  18.     Bf=$[RANDOM%256]
  19.     # Random background RGB color
  20.     Rb=$[RANDOM%256]
  21.     Gb=$[RANDOM%256]
  22.     Bb=$[RANDOM%256]
  23.     # Bi-colored block: top=foreground color, bottom=background color
  24.     echo "\e[48;2;$Rb;$Gb;${Bb}m\e[38;2;$Rf;$Gf;${Bf}mâ–€"
  25. }
  26.  
  27. # Computed color from (X,Y)
  28. Color1() { # X Y
  29.   local Lines LastPixel LastColor X Y Yt Yb Pt Pb It Ib RGBt RGBb Rf Gf Bf Rb Gb Bb
  30.   Lines=$[(LINES-1)*2] # with have 2 pixels in a block
  31.   LastPixel=$[(COLUMNS*Lines)-1]
  32.   LastColor=$[(256*256*256)-1] # 24-bit RGB colors -> 16777216
  33.   X=$1
  34.   Y=$2
  35.   # We compute two lines at once -- 't' stands for Top, 'b' stands for Bottom
  36.   Yt=$[Y*2]
  37.   Yb=$[Yt+1]
  38.   # Compute pixel indexes: 0 to LastPixel
  39.   Pt=$[Yt*COLUMNS+X]
  40.   Pb=$[Yb*COLUMNS+X]
  41.   # Convert to color indexes: 0 to LastColor (16777215)
  42.   It=$[(LastColor*Pt)/LastPixel]
  43.   Ib=$[(LastColor*Pb)/LastPixel]
  44.   # Convert indexes as hex values, which contains RGB we want
  45.   RGBt=$(printf "%06x" $It)
  46.   RGBb=$(printf "%06x" $Ib)
  47.   # Extract R/G/B from hex -- foreground color
  48.   Rf=$(printf %u '0x'${RGBt:0:2})
  49.   Gf=$(printf %u '0x'${RGBt:2:2})
  50.   Bf=$(printf %u '0x'${RGBt:4:2})
  51.   # Extract R/G/B from hex -- background color
  52.   Rb=$(printf %u '0x'${RGBb:0:2})
  53.   Gb=$(printf %u '0x'${RGBb:2:2})
  54.   Bb=$(printf %u '0x'${RGBb:4:2})
  55.     # Bi-colored block: top=foreground color, bottom=background color
  56.     echo "\e[48;2;$Rb;$Gb;${Bb}m\e[38;2;$Rf;$Gf;${Bf}mâ–€"
  57. }
  58.  
  59. clear
  60. for Y in $(seq 0 $YMAX); do
  61.     # A line is made of two horizontal lines at once, of half top blocks 'â–€' :
  62.     # - visible top block as foreground color
  63.     # - invisible bottom block as background color
  64.     S=''
  65.     for X in $(seq 0 $XMAX); do
  66.         # Add a bi-colored block to the line.
  67.         # Swap the commented/uncommented 2 lines below, to get something really different ;)
  68.         S+="$(Random)"
  69.         #S+="$(Color1 $X $Y)"
  70.     done
  71.     echo -e "$S"
  72. done
  73. # restore default foreground/background colors
  74. echo -en '\e[49;39m'
  75. # Snapshot of Random() colored blocks, here: https://zupimages.net/up/18/15/u9af.png
  76. # Snapshot of Color1() colored blocks, here: https://zupimages.net/up/18/15/svv4.png
Add Comment
Please, Sign In to add comment