Raster bars

From Rosetta Code
Revision as of 00:04, 12 July 2020 by Thundergnat (talk | contribs) (→‎{{header|Raku}}: Now with more swaying)
Raster bars is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

Display bars of color, moving up and down across the screen for horizontal raster bars, or left and right across the screen for vertical raster bars, making the raster bars move as stated by swaying side-to-side the respective way if possible. Use multiple colors in succession and carefully make a gradient between the colors if possible to achieve an effect of metallic-looking horizontal bars. Horizontal and/or vertical raster bars are accepted.

Ada

Library: SDLAda

<lang Ada>with Ada.Numerics.Elementary_Functions;

with SDL.Video.Windows.Makers; with SDL.Video.Renderers.Makers; with SDL.Video.Palettes; with SDL.Events.Events;

procedure Raster_Bars is

  Width   : constant := 1200;
  Height  : constant := 600;
  Window   : SDL.Video.Windows.Window;
  Renderer : SDL.Video.Renderers.Renderer;
  function Check_Quit return Boolean is
     use type SDL.Events.Event_Types;
     Event : SDL.Events.Events.Events;
  begin
     while SDL.Events.Events.Poll (Event) loop
        if Event.Common.Event_Type = SDL.Events.Quit then
           return True;
        end if;
     end loop;
     return False;
  end Check_Quit;
  procedure Draw_Raster_Bar (Y : Integer) is
     use SDL.Video.Palettes;
     use SDL.C;
     Cc : Colour_Component;
  begin
     for I in 0 .. 10 loop
        Cc := Colour_Component (50 + 20 * I);
        Renderer.Set_Draw_Colour ((Cc, Cc, Cc, 255));
        Renderer.Draw (Line => ((0,     C.int (Y + I)),
                                (Width, C.int (Y + I))));
     end loop;
  end Draw_Raster_Bar;
  procedure Draw_Raster_Bar_V (X : Integer) is
     use SDL.Video.Palettes;
     use SDL.C;
     Cc : Colour_Component;
  begin
     for I in 0 .. 10 loop
        Cc := Colour_Component (250 - 20 * I);
        Renderer.Set_Draw_Colour ((Cc, Cc, Cc, 255));
        Renderer.Draw (Line => ((C.int (X + I), 0),
                                (C.int (X + I), Height)));
     end loop;
  end Draw_Raster_Bar_V;
  procedure Raster_Bars is
     use Ada.Numerics.Elementary_Functions;
     Xc  : constant Integer := 800;
     Yc  : constant Integer := 200;
     XA  : constant Float   := 40.0;
     YA  : constant Float   := 30.0;
     N : Float := 0.0;
  begin
     loop
        Renderer.Set_Draw_Colour ((0, 0, 0, 255));
        Renderer.Fill (Rectangle => (0, 0, Width, Height));
        Draw_Raster_Bar (Yc + Integer (YA * Sin (N + 00.0, 360.0)));
        Draw_Raster_Bar (Yc + Integer (YA * Sin (N + 25.0, 360.0)));
        Draw_Raster_Bar (Yc + Integer (YA * Sin (N + 50.0, 360.0)));
        Draw_Raster_Bar (Yc + Integer (YA * Sin (N + 75.0, 360.0)));
        Draw_Raster_Bar_V (Xc + Integer (XA * Sin (0.6 * N + 00.0, 360.0)));
        Draw_Raster_Bar_V (Xc + Integer (XA * Sin (0.6 * N + 90.0, 360.0)));
        Draw_Raster_Bar_V (Xc + Integer (XA * Sin (0.6 * N + 180.0, 360.0)));
        Window.Update_Surface;
        if Check_Quit then
           exit;
        end if;
        delay 0.010;
        N := N + 4.0;
     end loop;
  end Raster_Bars;

begin

  if not SDL.Initialise (Flags => SDL.Enable_Screen) then
     return;
  end if;
  SDL.Video.Windows.Makers.Create (Win      => Window,
                                   Title    => "Raster bar",
                                   Position => SDL.Natural_Coordinates'(X => 10, Y => 10),
                                   Size     => SDL.Positive_Sizes'(Width, Height),
                                   Flags    => 0);
  SDL.Video.Renderers.Makers.Create (Renderer, Window.Get_Surface);
  Raster_Bars;
  Window.Finalize;
  SDL.Finalise;

end Raster_Bars;</lang>

Go

This uses Go's 'image' packages in its standard library to create an animated GIF.

After each 20 frames, it switches between horizontal and vertical bars and repeats the whole process 10 times.

WARNING: If you have problems with flashing images, either switch to a longer delay than 150ms between 'sways' or don't run it at all.

Although the .gif works fine in Firefox it might not do so in EOG due to optimizations made during its creation. If so, then the following ImageMagick command should fix it:

  $ convert raster_bars.gif -coalesce raster_bars2.gif
  $ eog raster_bars2.gif

<lang go>package main

import (

   "image"
   "image/color"
   "image/gif"
   "log"
   "os"

)

var (

   c0 = color.RGBA{166, 124, 0, 255}
   c1 = color.RGBA{191, 155, 48, 255}
   c2 = color.RGBA{255, 191, 0, 255}
   c3 = color.RGBA{255, 207, 64, 255}
   c4 = color.RGBA{255, 220, 115, 255}

)

var palette = []color.Color{c0, c1, c2, c3, c4}

func hline(img *image.Paletted, x1, y, x2 int, ci uint8) {

   for ; x1 <= x2; x1++ {
       img.SetColorIndex(x1, y, ci)
   }

}

func vline(img *image.Paletted, x, y1, y2 int, ci uint8) {

   for ; y1 <= y2; y1++ {
       img.SetColorIndex(x, y1, ci)
   }

}

func drawHbar(img *image.Paletted, x1, y1, x2, y2 int, ci uint8) {

   for ; y1 <= y2; y1++ {
       hline(img, x1, y1, x2, ci)
   }

}

func drawVbar(img *image.Paletted, x1, y1, x2, y2 int, ci uint8) {

   for ; x1 <= x2; x1++ {
       vline(img, x1, y1, y2, ci)
   }

}

func main() {

   const nframes = 40
   const delay = 15 // 150ms
   width, height := 500, 500
   anim := gif.GIF{LoopCount: 9} // repeats 9 + 1 times
   rect := image.Rect(0, 0, width, height)
   for f := 0; f < nframes; f++ {
       img := image.NewPaletted(rect, palette)
       c := uint8(f % 2)
       if f < nframes/2 {
           for y := 0; y < height; y += 20 {
               drawHbar(img, 0, y, width, y+19, c)
               c++
               if c == 5 {
                   c = 0
               }
           }
       } else {
           for x := 0; x < width; x += 20 {
               drawVbar(img, x, 0, x+19, height, c)
               c++
               if c == 5 {
                   c = 0
               }
           }
       }
       anim.Delay = append(anim.Delay, delay)
       anim.Image = append(anim.Image, img)
   }
   file, err := os.Create("raster_bars.gif")
   if err != nil {
       log.Fatal(err)
   }
   defer file.Close()
   if err2 := gif.EncodeAll(file, &anim); err != nil {
       log.Fatal(err2)
   }

}</lang>


Julia

<lang julia>using Colors, Cairo, Gtk

const can = GtkCanvas() const win = GtkWindow(can, "Raster Bar Demo", 500, 500) const pallete_index = [1] const horizontal = [true] const palette = [ # colors from the Go example

   colorant"rgba(166, 124, 0, 1.0)",
   colorant"rgba(191, 155, 48, 1.0)",
   colorant"rgba(255, 191, 0, 1.0)",
   colorant"rgba(255, 207, 64, 1.0)",
   colorant"rgba(255, 220, 115, 1.0)",

]

function line(ctx, x1, y1, x2, y2, colr, width=1)

   set_source(ctx, colr)
   set_line_width(ctx, width)
   move_to(ctx, x1, y1)
   line_to(ctx, x2, y2)
   stroke(ctx)

end hline(ctx, x1, y, x2, c) = line(ctx, x1, y, x2, y, c) hbar(ctx, x1, y1, x2, y2, c) = foreach(y -> hline(ctx, x1, y, x2, c), y1:y2) vline(ctx, x, y1, y2, c) = line(ctx, x, y1, x, y2, c) vbar(ctx, x1, y1, x2, y2, c) = foreach(x -> vline(ctx, x, y1, y2, c), x1:x2)

draw(can) do widget

   ctx = Gtk.getgc(can)
   height, width = Gtk.height(can), Gtk.width(can)
   if horizontal[1]
       for i in 1:3:height
           hbar(ctx, 0, i, width, i + 3, palette[pallete_index[1]])
           pallete_index[1] = mod1(pallete_index[1] + 1, 5)
       end
   else
       for i in 1:3:width
           vbar(ctx, i, 0, i + 3, height, palette[pallete_index[1]])
           pallete_index[1] = mod1(pallete_index[1] + 1, 5)
       end
   end

end

set_gtk_property!(can, :expand, true) for i in 1:typemax(Int)

   sleep(0.1)
   draw(can)
   showall(win)
   pallete_index[1] = mod1(pallete_index[1] + 2, 5)
   if i % 30 == 29
       horizontal[1] = !horizontal[1]
   end

end </lang>


Phix

Some grey bars that vaguely shimmer as they march up the screen... (did not turn out quite as well as hoped) <lang Phix>-- demo\rosetta\Raster_bars.exw constant N = 10,

        nColours = floor(255/N)

integer offset = 0, clast = nColours

include pGUI.e

Ihandle dlg, canvas cdCanvas cddbuffer, cdcanvas

function colour(integer i)

   integer g = i*N
   return  cdEncodeColorAlpha(g, g, g, 255)

end function

function redraw_cb(Ihandle /*ih*/, integer /*posx*/, integer /*posy*/)

   cdCanvasActivate(cddbuffer)
   cdCanvasSetBackground(cddbuffer, colour(clast))
   cdCanvasClear(cddbuffer)
   integer {width, height} = IupGetIntInt(canvas, "DRAWSIZE"),
           h = floor(height/(nColours-1)),
           y = height+offset
   cdCanvasSetInteriorStyle(cddbuffer, CD_SOLID)
   for i=2 to nColours do
       integer c = clast+i-1
       if c>nColours then c -= nColours end if
       cdCanvasSetForeground(cddbuffer, colour(c))
       cdCanvasBox(cddbuffer, 0, width, y-h, y)
       y -= h
   end for
   cdCanvasFlush(cddbuffer)
   offset += 1
   if offset>=h then
       offset = 0
       clast += 1
       if clast>nColours then clast = 1 end if
   end if
   return IUP_DEFAULT

end function

function map_cb(Ihandle ih)

   cdcanvas = cdCreateCanvas(CD_IUP, ih)
   cddbuffer = cdCreateCanvas(CD_DBUFFER, cdcanvas)
   return IUP_DEFAULT

end function

function unmap_cb(Ihandle /*ih*/)

   cdKillCanvas(cddbuffer)
   cdKillCanvas(cdcanvas)
   return IUP_DEFAULT

end function

function timer_cb(Ihandle /*ih*/)

   IupRedraw(dlg) 
   return IUP_DEFAULT

end function

procedure main()

   IupOpen()

   canvas = IupCanvas(NULL)
   IupSetAttribute(canvas, "RASTERSIZE", "600x400")

   IupSetCallback(canvas, "MAP_CB", Icallback("map_cb"))
   IupSetCallback(canvas, "UNMAP_CB", Icallback("unmap_cb"))

   dlg = IupDialog(canvas)
   IupSetAttribute(dlg, "TITLE", "Raster bars")
   IupSetCallback(canvas, "ACTION", Icallback("redraw_cb"))
   IupCloseOnEscape(dlg)
   Ihandle hTimer = IupTimer(Icallback("timer_cb"), 10)

   IupMap(dlg)
   IupSetAttribute(canvas, "RASTERSIZE", NULL)

   IupShowXY(dlg,IUP_CENTER,IUP_CENTER)

   IupMainLoop()

   IupClose()

end procedure

main()</lang>

Raku

(formerly Perl 6)

Works with: Rakudo version 2019.03

As there is no reference implementation, and rather sketchy task instructions, this may or may not fulfill the original task authors intent.

Not really sure what is meant by "swaying", perhaps this will suffice.

Generate random colored bars and display them. (They ended up looking more plastic than metallic; ah well.)

  • Use Up / Down arrows to change the scroll speed.
  • Use Left / Right arrows to adjust the gap between the raster bars.
  • Use Pg Up / Pg Dn to adjust raster bar height.
  • Use Z / X to change the angle of the raster bars.
  • Use Space bar to pause / resume scrolling.
  • Use Left Ctrl to toggle the scroll direction.
  • Press R to toggle Randomize on / off.
  • If Randomize is active, adjust the randomize delay with < / >
  • Press S to toggle Swaying on / off.
  • If Swaying is active, adjust the period with D / F
  • Press Q to exit.

<lang perl6>unit sub MAIN (

   Int :b(:$bar-height) is copy = 60; #= Height of the individual "Raster bars", minimum 32 (pixels)
   Int :d(:$dir) is copy        = -1; #= Scroll direction: -1 is "up" 1 is "down"
   Int :s(:$step) is copy       = 4;  #= Scroll speed (pixels per step
   Int :g(:$gap) is copy        = $bar-height + 50; #= Gap between bars (pixels)
   Int :a(:$angle) is copy      = 0; #= Angle to orient bars off horizontal (-60 to 60 degrees)
   Int :sw(:$sway) is copy      = 0; #= Swaying on / off
   Real :r(:$rnd) is copy       = 0; #= Delay between randomize events

);

say q:to/END/;

   Use Up / Down arrows to change the scroll speed.
   Use Left / Right arrows to adjust the gap between the raster bars.
   Use Pg Up / Pg Dn to adjust raster bar height.
   Use Z / X to change the angle of the raster bars.
   Use Space bar to pause / resume scrolling.
   Use Left Ctrl to toggle the scroll direction.
   Press R to toggle Randomize on / off.
   If Randomize is active, adjust the randomize delay with < / >
   Press S to toggle Swaying on / off.
   If Swaying is active, adjust the period with D / F
   Press Q to exit.
   END

use SDL2::Raw; use Cairo;

my $width = 800; my $height = 800;

SDL_Init(VIDEO);

my $window = SDL_CreateWindow(

   'Raster Bars - Raku',
   SDL_WINDOWPOS_CENTERED_MASK,
   SDL_WINDOWPOS_CENTERED_MASK,
   $width, $height, RESIZABLE

);

my $render = SDL_CreateRenderer($window, -1, ACCELERATED +| PRESENTVSYNC);

my @bars = (^128).map: { gen-bar( rand xx 3 ) };

my $event = SDL_Event.new;

enum KEY_CODES (

   K_UP     => 82,
   K_DOWN   => 81,
   K_LEFT   => 80,
   K_RIGHT  => 79,
   K_SPACE  => 44,
   K_PGUP   => 75,
   K_PGDN   => 78,
   K_LCTRL  => 224,
   K_Z      => 29,
   K_X      => 27,
   K_Q      => 20,
   K_R      => 21,
   K_S      => 22,
   K_D      => 7,
   K_F      => 9,
   K_LT     => 54,
   K_GT     => 55,

);

my $port = +@bars * $gap; my $y = $dir > 0 ?? $height - $port !! $height ; my $now = now; my $period = 2;

main: loop {

   handle-event($event) while SDL_PollEvent($event);
   randomize if $rnd and now - $now > $rnd;
   if $dir > 0 {
       $y = $height - $port if $y > 0 - ceiling $height / cos(π * $angle / 180).abs
   } else {
       $y = 0 - ceiling $height / cos(π * $angle / 180).abs if $y < $height - $port
   }
   $y = $step * $dir + $y;
   $angle = (((now * $period) % τ).sin * 35).Int if $sway;
   for ^@bars {
       my $offset = $sway ?? $gap !! ceiling $gap / cos(π * $angle / 180).abs;
       SDL_RenderCopyEx( $render, @bars[$_], Nil,
         SDL_Rect.new( -($width*4), $y + $offset * $_, $width * 10, $bar-height),
         $angle.Num, SDL_Point.new(:x((4.5*$width).Int),:y($y + $offset * $_)), 0
       )
   }
   SDL_RenderPresent($render);
   SDL_RenderClear($render);
   print fps;

}

put ;

SDL_Quit();

sub gen-bar (@color) {

   my $bar = Cairo::Image.create( Cairo::FORMAT_ARGB32, 1, 128 );
   given Cairo::Context.new($bar) {
       my Cairo::Pattern::Gradient::Linear $lpat .= create(0.0, 0.0,  0.0, 128.0);
       $lpat.add_color_stop_rgba(  1, |(@color »*» .3), 1);
       $lpat.add_color_stop_rgba( .2, |(@color),        1);
       $lpat.add_color_stop_rgba(.75, |(@color),        1);
       $lpat.add_color_stop_rgba(  0, |(@color »+» .5), 1);
       .rectangle(0, 0, 1, 128);
       .pattern($lpat);
       .fill;
       $lpat.destroy;
   }
   my $bar_texture = SDL_CreateTexture(
       $render, %PIXELFORMAT<ARGB8888>,
       STATIC, 1, 128
   );
   SDL_UpdateTexture(
       $bar_texture,
       SDL_Rect.new(:x(0), :y(0), :w(1), :h(128)),
       $bar.data, $bar.stride // 1
   );
   $bar_texture

}

sub handle-event ($event) {

   my $casted_event = SDL_CastEvent($event);
   given $casted_event {
       when *.type == QUIT    { last main }
       when *.type == KEYDOWN {
           if KEY_CODES(.scancode) -> $comm {
               given $comm {
                   when 'K_UP'     { $step += 1 }
                   when 'K_DOWN'   { $step -= 1 if $step > 1 }
                   when 'K_LEFT'   { $gap = $gap < 32 ?? $gap !! $gap - 1; $port = +@bars * $gap; }
                   when 'K_RIGHT'  { $gap++; $port += +@bars; }
                   when 'K_PGUP'   { $bar-height += 2 }
                   when 'K_PGDN'   { $bar-height = $bar-height >= 34 ?? $bar-height - 2 !! $bar-height }
                   when 'K_SPACE'  { $step = $step ?? 0 !! 1 }
                   when 'K_LCTRL'  { $dir  *= -1 }
                   when 'K_Z'      { $angle = $angle > -45 ?? $angle - 5 !! $angle }
                   when 'K_X'      { $angle = $angle <  45 ?? $angle + 5 !! $angle }
                   when 'K_R'      { $rnd = $rnd ?? 0 !! 1 }
                   when 'K_S'      { $sway xor= 1 }
                   when 'K_F'      { $period += .1 }
                   when 'K_D'      { $period = $period - .1 max .1;  }
                   when 'K_GT'     { $rnd += .2 }
                   when 'K_LT'     { $rnd = $rnd > .2 ?? $rnd -.2 !! .2 }
                   when 'K_Q'      { last main }
               }
           } #else { say .scancode, "\n" }
       }
       when *.type == WINDOWEVENT {
           if .event == RESIZED {
               $width  = .data1;
               $height = .data2 + $bar-height;
           }
       }
   }

}

sub randomize {

   $dir   = (-1,1).pick;
   $step  = (4..8).pick;
   $bar-height   = (32..200).pick;
   $gap   = $bar-height + (1..100).pick;
   $angle = (-45, *+5 ... 45).pick;
   $port = +@bars * $gap;
   if $dir > 0 {
       $y = $height - $port;
   } else {
       $y = 0 - ceiling ($height max $width) / cos(π * $angle / 180).abs;
   }
   $now = now;

}

sub fps {

   state $fps-frames = 0;
   state $fps-now    = now;
   state $fps        = ;
   $fps-frames++;
   if now - $fps-now >= 1 {
       $fps = [~] "\b" x 40, ' ' x 20, "\b" x 20 ,
           sprintf "FPS: %5.2f  ", ($fps-frames / (now - $fps-now)).round(.01);
       $fps-frames = 0;
       $fps-now = now;
   }
   $fps

}</lang>

Screenshot still of typical run: (offsite .png image)

(Small) Moving gif of swaying bars: (offsite .gif image)