Colour bars/Display: Difference between revisions

From Rosetta Code
Content added Content deleted
(upgraded to task)
({{header|UNIX Shell}})
Line 16: Line 16:
set colors [list {*}[lrange $colors 1 end] [lindex $colors 0]]
set colors [list {*}[lrange $colors 1 end] [lindex $colors 0]]
}</lang>
}</lang>

== {{header|UNIX Shell}} ==
<lang sh>clear
WIDTH=`tput cols`
HEIGHT=`tput lines`
NUMBARS=8
BARWIDTH=`expr $WIDTH / $NUMBARS`

l="1" # Set the line counter to 1
while [ "$l" -lt $HEIGHT ]; do
b="0" # Bar counter
while [ "$b" -lt $NUMBARS ]; do
tput setab $b
s="0"
while [ "$s" -lt $BARWIDTH ]; do
echo -n " "
s=`expr $s + 1`
done
b=`expr $b + 1`
done
echo # newline
l=`expr $l + 1`
done

tput sgr0 # reset</lang>


== {{header|ZX Spectrum Basic}} ==
== {{header|ZX Spectrum Basic}} ==

Revision as of 23:11, 29 May 2011

Task
Colour bars/Display
You are encouraged to solve this task according to the task description, using any language you may know.

The task is to display a series of vertical color bars across the width of the display. The color bars should either use the system palette, or the sequence of colors: Black, Red, Green, Blue, Magenta, Cyan, Yellow, White.

Tcl

Library: Tk

<lang tcl>package require Tcl 8.5 package require Tk 8.5

wm attributes . -fullscreen 1 pack [canvas .c] -fill both -expand 1 set colors {black red green blue magenta cyan yellow white}

for {set x 0} {$x < [winfo screenwidth c]} {incr x 8} {

   .c create rectangle $x 0 [expr {$x+7}] [winfo screenheight c] \
           -fill [lindex $colors 0] -outline {}
   set colors [list {*}[lrange $colors 1 end] [lindex $colors 0]]

}</lang>

UNIX Shell

<lang sh>clear WIDTH=`tput cols` HEIGHT=`tput lines` NUMBARS=8 BARWIDTH=`expr $WIDTH / $NUMBARS`

l="1" # Set the line counter to 1 while [ "$l" -lt $HEIGHT ]; do

 b="0"    # Bar counter
 while [ "$b" -lt $NUMBARS ]; do
   tput setab $b
   s="0"
   while [ "$s" -lt $BARWIDTH ]; do
     echo -n " "
     s=`expr $s + 1`
   done
   b=`expr $b + 1`
 done
 echo    # newline
 l=`expr $l + 1`

done

tput sgr0 # reset</lang>

ZX Spectrum Basic

<lang basic>10 REM The ZX Spectrum display is 32 columns wide, so we have 8 columns of 4 spaces 20 FOR r=0 TO 20: REM There are 21 rows 30 FOR c=0 TO 7: REM We use the native colour sequence here 40 PAPER c: REM set the background colour for the spaces to be printed 50 PRINT " ";: REM four spaces, the semicolon prevents newline 60 NEXT c 70 REM at this point the cursor has wrapped, so we don't need a newline 80 NEXT r</lang>