Raster bars: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
(→‎{{header|Perl 6}}: Rearrange, add a few more options, at this point I'm just fiddling with it.)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(16 intermediate revisions by 10 users not shown)
Line 1:
{{draft task}}
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.
 
;Task:
=={{header|Perl 6}}==
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 acceptable.
<br><br>
 
=={{header|Ada}}==
{{libheader|SDLAda}}
<syntaxhighlight 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;</syntaxhighlight>
 
=={{header|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:
<pre>
$ convert raster_bars.gif -coalesce raster_bars2.gif
$ eog raster_bars2.gif
</pre>
 
<syntaxhighlight 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)
}
}</syntaxhighlight>
 
 
=={{header|Julia}}==
<syntaxhighlight 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
</syntaxhighlight>
 
=={{header|Nim}}==
{{trans|Julia}}
{{libheader|gintro}}
<syntaxhighlight lang="nim">import gintro/[gobject, glib, gdk, gtk, gio, cairo]
 
 
const Palette = [[166.0, 124.0, 0.0],
[191.0, 155.0, 48.0],
[255.0, 191.0, 0.0],
[255.0, 207.0, 64.0],
[255.0, 220.0, 115.0]]
 
type
 
# Description of the simulation.
Simulation = ref object
area: DrawingArea
horizontal: bool
paletteIndex: Natural
count: int
 
Color = array[3, float]
 
 
#---------------------------------------------------------------------------------------------------
 
proc newSimulation(area: DrawingArea): Simulation {.noInit.} =
## Allocate and initialize the simulation object.
Simulation(area: area, horizontal: true, paletteIndex: 0, count: 0)
 
#---------------------------------------------------------------------------------------------------
 
proc bar(ctx: cairo.Context; x1, y1, x2, y2: int; c: Color) =
## Draw a bar.
ctx.setSource(c)
ctx.rectangle(x1.toFloat, y1.toFloat, (x2 - x1 + 1).toFloat, (y2 - y1 + 1).toFloat)
ctx.fill()
 
#---------------------------------------------------------------------------------------------------
 
proc draw(sim: Simulation; ctx: cairo.Context) =
## Draw the bars.
 
let width = sim.area.window.width()
let height = sim.area.window.height()
 
if sim.horizontal:
for i in countup(0, height - 4, 3):
ctx.bar(0, i, width - 1, i + 3, Palette[sim.paletteIndex])
sim.paletteIndex = (sim.paletteIndex + 1) mod 5
 
else:
for i in countup(0, width - 4, 3):
ctx.bar(i, 0, i + 3, height - 1, Palette[sim.paletteIndex])
sim.paletteIndex = (sim.paletteIndex + 1) mod 5
 
#---------------------------------------------------------------------------------------------------
 
proc update(sim: Simulation): gboolean =
## Update the simulation state.
 
sim.draw(sim.area.window.cairoCreate())
sim.area.showAll()
sim.paletteIndex = (sim.paletteIndex + 2) mod 5
inc sim.count
if sim.count mod 30 == 29:
sim.horizontal = not sim.horizontal
result = gboolean(1)
 
#---------------------------------------------------------------------------------------------------
 
proc activate(app: Application) =
## Activate the application.
 
let window = app.newApplicationWindow()
window.setSizeRequest(500, 500)
window.setTitle("Raster Bar Demo")
 
let area = newDrawingArea()
window.add(area)
 
let sim = newSimulation(area)
timeoutAdd(50, update, sim)
 
window.showAll()
 
#———————————————————————————————————————————————————————————————————————————————————————————————————
 
let app = newApplication(Application, "Rosetta.rasterbars")
discard app.connect("activate", activate)
discard app.run()</syntaxhighlight>
 
=={{header|Phix}}==
Some grey bars that vaguely shimmer as they march up the screen... (did not turn out quite as well as hoped)
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">-- demo\rosetta\Raster_bars.exw</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">title</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"Raster bars"</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">N</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">10</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">nColours</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">255</span><span style="color: #0000FF;">/</span><span style="color: #000000;">N</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">offset</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">clast</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">nColours</span>
<span style="color: #008080;">include</span> <span style="color: #000000;">pGUI</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
<span style="color: #004080;">Ihandle</span> <span style="color: #000000;">dlg</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">canvas</span>
<span style="color: #004080;">cdCanvas</span> <span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">cdcanvas</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">colour</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">g</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">*</span><span style="color: #000000;">N</span>
<span style="color: #008080;">return</span> <span style="color: #7060A8;">cdEncodeColorAlpha</span><span style="color: #0000FF;">(</span><span style="color: #000000;">g</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">g</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">g</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">255</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">redraw_cb</span><span style="color: #0000FF;">(</span><span style="color: #004080;">Ihandle</span> <span style="color: #000080;font-style:italic;">/*ih*/</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000080;font-style:italic;">/*posx*/</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">/*posy*/</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">cdCanvasActivate</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">cdCanvasSetBackground</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">colour</span><span style="color: #0000FF;">(</span><span style="color: #000000;">clast</span><span style="color: #0000FF;">))</span>
<span style="color: #7060A8;">cdCanvasClear</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">width</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">height</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupGetIntInt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">canvas</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"DRAWSIZE"</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">h</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">height</span><span style="color: #0000FF;">/(</span><span style="color: #000000;">nColours</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)),</span>
<span style="color: #000000;">y</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">height</span><span style="color: #0000FF;">+</span><span style="color: #000000;">offset</span>
<span style="color: #7060A8;">cdCanvasSetInteriorStyle</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">,</span> <span style="color: #004600;">CD_SOLID</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">2</span> <span style="color: #008080;">to</span> <span style="color: #000000;">nColours</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">c</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">clast</span><span style="color: #0000FF;">+</span><span style="color: #000000;">i</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">></span><span style="color: #000000;">nColours</span> <span style="color: #008080;">then</span> <span style="color: #000000;">c</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">nColours</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #7060A8;">cdCanvasSetForeground</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">colour</span><span style="color: #0000FF;">(</span><span style="color: #000000;">c</span><span style="color: #0000FF;">))</span>
<span style="color: #7060A8;">cdCanvasBox</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">width</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">y</span><span style="color: #0000FF;">-</span><span style="color: #000000;">h</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">y</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">y</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">h</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">cdCanvasFlush</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">offset</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">offset</span><span style="color: #0000FF;">>=</span><span style="color: #000000;">h</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">offset</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
<span style="color: #000000;">clast</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">clast</span><span style="color: #0000FF;">></span><span style="color: #000000;">nColours</span> <span style="color: #008080;">then</span> <span style="color: #000000;">clast</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">return</span> <span style="color: #004600;">IUP_DEFAULT</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">map_cb</span><span style="color: #0000FF;">(</span><span style="color: #004080;">Ihandle</span> <span style="color: #000000;">ih</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">cdcanvas</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">cdCreateCanvas</span><span style="color: #0000FF;">(</span><span style="color: #004600;">CD_IUP</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">ih</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">cddbuffer</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">cdCreateCanvas</span><span style="color: #0000FF;">(</span><span style="color: #004600;">CD_DBUFFER</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">cdcanvas</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #004600;">IUP_DEFAULT</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">unmap_cb</span><span style="color: #0000FF;">(</span><span style="color: #004080;">Ihandle</span> <span style="color: #000080;font-style:italic;">/*ih*/</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">cdKillCanvas</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">cdKillCanvas</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cdcanvas</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #004600;">IUP_DEFAULT</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">timer_cb</span><span style="color: #0000FF;">(</span><span style="color: #004080;">Ihandle</span> <span style="color: #000080;font-style:italic;">/*ih*/</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">IupRedraw</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #004600;">IUP_DEFAULT</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">IupOpen</span><span style="color: #0000FF;">()</span>
<span style="color: #000000;">canvas</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupCanvas</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"RASTERSIZE=600x400"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">IupSetCallbacks</span><span style="color: #0000FF;">(</span><span style="color: #000000;">canvas</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"MAP_CB"</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">Icallback</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"map_cb"</span><span style="color: #0000FF;">),</span>
<span style="color: #008000;">"UNMAP_CB"</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">Icallback</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"unmap_cb"</span><span style="color: #0000FF;">),</span>
<span style="color: #008000;">"ACTION"</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">Icallback</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"redraw_cb"</span><span style="color: #0000FF;">)})</span>
<span style="color: #000000;">dlg</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupDialog</span><span style="color: #0000FF;">(</span><span style="color: #000000;">canvas</span><span style="color: #0000FF;">,</span><span style="color: #008000;">`TITLE="%s"`</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">title</span><span style="color: #0000FF;">})</span>
<span style="color: #004080;">Ihandle</span> <span style="color: #000000;">hTimer</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupTimer</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">Icallback</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"timer_cb"</span><span style="color: #0000FF;">),</span> <span style="color: #000000;">10</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">IupShow</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">IupSetAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">canvas</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"RASTERSIZE"</span><span style="color: #0000FF;">,</span> <span style="color: #004600;">NULL</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()!=</span><span style="color: #004600;">JS</span> <span style="color: #008080;">then</span>
<span style="color: #7060A8;">IupMainLoop</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">IupClose</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</syntaxhighlight>-->
 
=={{header|Raku}}==
(formerly Perl 6)
{{works with|Rakudo|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 Left / Right to adjust the gap between the raster bars.
* Use Space bar to pause / resume scrolling.
* Use Left Ctrl to reverse scroll direction.
* 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.
 
<syntaxhighlight lang="raku" line>unit sub MAIN (
<lang perl6>use SDL2::Raw;
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;
 
Line 27 ⟶ 492:
 
my $window = SDL_CreateWindow(
'Raster Bars - Perl 6Raku',
SDL_WINDOWPOS_CENTERED_MASK,
SDL_WINDOWPOS_CENTERED_MASK,
Line 35 ⟶ 500:
my $render = SDL_CreateRenderer($window, -1, ACCELERATED +| PRESENTVSYNC);
 
my @bars = (^256128).map: { gen-bar( rand xx 3 ) };
 
my $event = SDL_Event.new;
Line 51 ⟶ 516:
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 $dir = -1;
my $step = 5;
my $incr = 1;
my $gap = 100;
my $bh = 64;
$height += 32;
my $port = +@bars * $gap;
my $y = $dir > 0 ?? $height - $port !! $height ;
my $anglenow = 0now;
my $period = 2;
 
main: loop {
handle-event($event) while SDL_PollEvent($event);
 
$yrandomize =if $steprnd and now *- $dirnow +> $yrnd;
 
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( -($gapwidth*4), $y + $offset * $_, $width * 210, $bhbar-height),
$angle.Num, SDL_Point.new(:x(0(4.5*$width).Int),:y(0$y + $offset * $_)), 0
 
)
}
Line 85 ⟶ 556:
 
SDL_RenderClear($render);
 
print fps;
}
 
put '';
 
SDL_Quit();
 
sub gen-bar (@color) {
my $bar = Cairo::Image.create( Cairo::FORMAT_ARGB32, 1, 32128 );
given Cairo::Context.new($bar) {
my Cairo::Pattern::Gradient::Linear $lpat .= create(0.0, 0.0, 0.0, 32128.0);
$lpat.add_color_stop_rgba( 1, |(@color »*» .3), 1);
$lpat.add_color_stop_rgba( .2, |(@color), 1);
$lpat.add_color_stop_rgba( .875, |(@color), 1);
$lpat.add_color_stop_rgba( 0, |(@color »+» .85), 1);
.rectangle(0, 0, 1, 32128);
.pattern($lpat);
.fill;
Line 105 ⟶ 580:
my $bar_texture = SDL_CreateTexture(
$render, %PIXELFORMAT<ARGB8888>,
STATIC, 1, 32128
);
 
SDL_UpdateTexture(
$bar_texture,
SDL_Rect.new(:x(0), :y(0), :w(1), :h(32128)),
$bar.data, $bar.stride // 1
);
Line 124 ⟶ 599:
if KEY_CODES(.scancode) -> $comm {
given $comm {
when 'K_UP' { $step += $incr1 }
when 'K_DOWN' { $step -= $incr1 if $step > $incr1 }
when 'K_LEFT' { $gap = $gap < 32 ?? $gap !! $gap - 1; $port = +@bars * $gap; }
when 'K_RIGHT' { $gap++; $port += +@bars; }
when 'K_PGUP' { $bhbar-height += 2 }
when 'K_PGDN' { $bhbar-height = $bhbar-height >= 34 ?? $bhbar-height - 2 !! $bhbar-height }
when 'K_SPACE' { $step = $step ?? 0 !! $incr1 }
when 'K_LCTRL' { $dir *= -1 }
when 'K_Z' { $angle = $angle > -45 ?? $angle - 15 !! $angle }
when 'K_X' { $angle = $angle < 45 ?? $angle + 15 !! $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 == 5RESIZED {
$width = .data1;
$height = .data2 + 32$bar-height;
}
}
}
}
}</lang>
 
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
}</syntaxhighlight>
 
Screenshot still of typical run: [https://github.com/thundergnat/rc/blob/master/img/Raster-bars-Perl6.png (offsite .png image)]
 
(Small) Moving gif of swaying bars: [https://github.com/thundergnat/rc/blob/master/img/Raster-bars-swaying-Raku.gif (offsite .gif image)]
 
=={{header|Rust}}==
<syntaxhighlight lang="rust">
extern crate minifb;
 
use std::iter::repeat;
 
use minifb::{Key, Window, WindowOptions};
 
const WIDTH: usize = 640;
const HEIGHT: usize = 360;
 
struct Bar {
color: [f32; 3],
height: u32,
position: f32,
speed: f32,
}
 
impl Bar {
 
fn draw_column(&self, column_buffer: &mut [u32]) {
let start_row = (self.position.round() as i32).clamp(0, HEIGHT as i32) as usize;
let end_row = (start_row + self.height as usize).clamp(0, HEIGHT);
 
column_buffer[start_row..end_row].iter_mut().enumerate()
.for_each(|(row_index, color)| {
*color = self.row_color(row_index as i32);
});
}
 
fn row_color(&self, y: i32) -> u32 {
let yy = y as f32 / (self.height - 1) as f32;
let yy = 1.0 - 2.0 * (yy - 0.5).abs();
 
let r = (25.0 + yy.powf((2.0 - 2.0 * self.color[0]).exp()) * 230.0).round() as u8;
let g = (25.0 + yy.powf((2.0 - 2.0 * self.color[1]).exp()) * 230.0).round() as u8;
let b = (25.0 + yy.powf((2.0 - 2.0 * self.color[2]).exp()) * 230.0).round() as u8;
 
u32::from_be_bytes([255, r, g, b])
}
 
fn update_position(&mut self) {
self.position += self.speed;
 
if let Some(position) = self.bounce_position() {
self.position = position;
self.speed = -self.speed;
}
}
 
fn bounce_position(&self) -> Option<f32> {
if self.speed > 0.0 {
let limit = (HEIGHT as i32 - self.height as i32) as f32;
(self.position >= limit).then(|| limit - (self.position - limit))
} else {
(self.position <= 0.0).then(|| -self.position)
}
}
 
}
 
fn main() {
 
let mut window = Window::new(
"Raster Bars",
WIDTH,
HEIGHT,
WindowOptions::default(),
)
.unwrap_or_else(|e| {
panic!("{}", e);
});
 
// Limit to max ~60 fps update rate
window.limit_update_rate(Some(std::time::Duration::from_micros(16600)));
 
let mut bars = Vec::new();
 
bars.push(Bar { color: [1.0, 1.0, 0.0], height: 20, position: 0.0, speed: 3.1 });
bars.push(Bar { color: [1.0, 0.0, 0.0], height: 25, position: 0.0, speed: 0.2 });
bars.push(Bar { color: [0.0, 1.0, 0.0], height: 7, position: 0.0, speed: 0.4 });
bars.push(Bar { color: [0.0, 0.0, 1.0], height: 30, position: 0.0, speed: 1.0 });
 
while window.is_open() && !window.is_key_down(Key::Escape) {
 
let mut column_buffer = vec![0x_00_00_00_u32; HEIGHT];
for bar in &mut bars {
bar.draw_column(&mut column_buffer);
bar.update_position();
}
 
let buffer: Vec<u32> = column_buffer.iter()
.flat_map(|&row_color| repeat(row_color).take(WIDTH))
.collect();
 
window
.update_with_buffer(&buffer, WIDTH, HEIGHT)
.unwrap();
}
}
</syntaxhighlight>
 
 
=={{header|Wren}}==
{{trans|Go}}
{{libheader|DOME}}
Unlike the Go example, repeats indefinitely until the window is closed.
 
As DOME has no facility to create an animated .gif file, the moving image is displayed but not saved.
<syntaxhighlight lang="wren">import "dome" for Window
import "graphics" for Canvas, Color
 
var c0 = Color.rgb(166, 124, 0, 255)
var c1 = Color.rgb(191, 155, 48, 255)
var c2 = Color.rgb(255, 191, 0, 255)
var c3 = Color.rgb(255, 207, 64, 255)
var c4 = Color.rgb(255, 220, 115, 255)
 
var Palette = [c0, c1, c2, c3, c4]
 
class RasterBars {
construct new(width, height) {
Window.resize(width, height)
Canvas.resize(width, height)
Window.title = "Raster bars"
_width = width
_height = height
}
 
init() {
_f = 0
_nframes = 320
_draw = false
_c = false
}
 
hline(x1, y, x2, ci) {
while (x1 <= x2) {
Canvas.pset(x1, y, Palette[ci])
x1 = x1 + 1
}
}
 
vline(x, y1, y2, ci) {
while (y1 <= y2) {
Canvas.pset(x, y1, Palette[ci])
y1 = y1 + 1
}
}
 
drawHBar(x1, y1, x2, y2, ci) {
while (y1 <= y2) {
hline(x1, y1, x2, ci)
y1 = y1 + 1
}
}
 
drawVBar(x1, y1, x2, y2, ci) {
while (x1 <= x2) {
vline(x1, y1, y2, ci)
x1 = x1 + 1
}
}
 
update() {
_f = _f + 1
if (_f % 8 == 0) {
_draw = true
_c = !_c
} else {
_draw = false
}
if (_f == _nframes) _f = 0
}
 
draw(alpha) {
if (!_draw) return
var c = _c ? 0 : 1
if (_f < _nframes / 2) {
var y = 0
while (y < _height) {
drawHBar(0, y, _width, y+19, c)
c = (c < 4) ? c + 1 : 0
y = y + 20
}
} else {
var x = 0
while (x < _width) {
drawVBar(x, 0, x+19, _height, c)
c = (c < 4) ? c + 1 : 0
x = x + 20
}
}
}
}
 
var Game = RasterBars.new(500, 500)</syntaxhighlight>
 
=={{header|X86 Assembly}}==
256 byte executable. Translation of XPL0. Output is the same.
<syntaxhighlight lang="asm">
page 240, 132 ;minimize annoying page headers
;Assemble with:
; tasm
; tlink /t
.model tiny
.code
.486
org 100h
 
;MS-DOS loads .com files with registers set this way:
; ax=0, bx=0, cx=00FFh, dx=cs, si=0100h, di=-2, bp=09xx, sp=-2, es=ds=cs=ss
;The direction flag (d) is clear (incrementing).
start:
if 0 ;if debug then must initialize registers
mov cx, 00FFh
mov dx, cs
mov si, 0100h
mov di, -2
mov bp, 0955h
endif
mov al,13h ;call BIOS to set graphic mode 13h
int 10h ; 320x200 with 256 colors (ah = 0)
 
;Set up color registers with sequential shades of red and green (gives yellow,
; gold, copper, brown, etc.). Since the hardware only allows 64 levels of
; intensity, the shades are not continuous. Code from SHR's YEW2 demo program.
yew10: mov al,cl ;cx is assumed set to 00ffh by loader
mov dx,03c8h ;select color register ffh down thru 01h
out dx,al
 
inc dx ;point to corresponding RGB registers
out dx,al ;set red intensity
 
aam ;set green intensity to 7/8 of red value
org $-1 ;ah <- al/8; al <- rem
db 8 ;(values other than 0a work just fine)
mov al,cl
sub al,ah
out dx,al
 
xor ax,ax ;set blue intensity to 0
out dx,al
loop yew10 ;loop for 255 color registers (ff-01)
 
fninit ;initialize math coprocessor (FPU)
fldz ;push (load) 0.0 onto FPU stack
fstp angle ; Angle:= 0.;
 
; - - - - - - - - - - - - - - - Main Loop - - - - - - - - - - - - - - - - - - -
ras60:
;WaitForVSync
mov dx, 03dah
wait10: in al, dx ;wait for vertical retrace to go away
and al, 08h
jne short wait10
wait20: in al, dx ;wait for vertical retrace
and al, 08h
je short wait20
 
;Erase buffer
mov di, offset buffer
mov cx, 200*320/2
xor ax, ax
rep stosw ;es:[di++]:= ax; cx--
 
; for N:= 2 to 9 do
mov dx, 2 ;dx = N
mov N, dx
ras70:
;Phase:= float(N)*0.4;
fild N ;can't use dx here; must be memory loc
fld n4
fmul
fstp phase
 
;Y:= fix(80.*Sin(Angle+Phase));
fld n80
fld angle
fld phase
fadd
fsin ;return the sine in one instruction!
fmul
push ax
mov bp, sp
fistp word ptr [bp]
pop ax
 
;ShowBar(Y+100, 2*N, 64*((3&N)+1)-1);
add ax, 100
mov sbY0, ax
imul ax, dx, 2
mov sbR, ax
mov ax, dx
and ax, 3
inc ax
shl ax, 6
dec ax
mov sbC0, ax
call ShowBar
 
inc N
mov dx, N
cmp dx, 9
jle short ras70
 
;Copy buffer to video RAM
mov cx, 320*200/2
mov si, offset buffer
xor di, di
push 0A000h
pop es
rep movsw ;es:[di++] <- ds:[si++]; cx--
push cs
pop es
 
; Angle:= Angle + 0.04;
fld angle
fld n04
fadd
fstp angle
 
mov ah, 1 ;a keystroke terminates the program
int 16h
je ras60 ;loop back if no keystroke
 
mov ah, 0 ;eat the keystroke
int 16h ; so it isn't echoed on the display
 
mov ax, 0003h ;restore the normal text display mode
int 10h
ret
 
;-------------------------------------------------------------------------------
ShowBar:
; proc ShowBar(Y0, R, C0); \Display cylindrical rod with its center
; int Y0, R, C0; \ at Y0, radius R, and center color C0
 
; for Y:= -R to +R do
mov ax, sbR
mov bp, ax ;bp = 'for' loop limit
neg ax
mov si, ax ;si = 'for' loop control variable
sb40:
;C:= C0 - 31*abs(Y)/R; \(looks better than a circular surface)
mov ax, si
sb45: neg ax
jl short sb45
imul ax, 31
cwd
idiv sbR
neg ax
add ax, sbC0
 
;Line(319, Y+Y0, C); \draw horizontal line from left to right
mov cx, 320
mov di, si
add di, sbY0
imul di, 320
add di, offset buffer
rep stosb ;es:[di++]:= al; cx--
 
inc si
cmp si, bp
jle short sb40
ret
 
n4 dd 0.4 ;single precision 'float' constants
n04 dd 0.04
n80 dd 80.0
N dw ?
angle dd ?
phase dd ?
sbY0 dw ?
sbR dw ?
sbC0 dw ?
buffer db 320*200 dup(?) ;stack starts above this at top of 64K
end start
</syntaxhighlight>
 
=={{header|XPL0}}==
[[File:RasterXPL0.gif|200px|thumb|right]]
<syntaxhighlight lang "XPL0">proc ShowBar(Y0, R, C0); \Display a cylindrical rod with its center
int Y0, R, C0; \ at Y0, radius R, and center color C0
int Y, C;
for Y:= -R to +R do
[C:= C0 - 31*abs(Y)/R; \(looks better than circular surface)
Move(0, Y+Y0); \start line at left side of screen
Line(319, Y+Y0, C); \draw horizontal line to right side
];
 
int N, Y;
Screenshot still of typical run: [https://github.com/thundergnat/rc/blob/master/img/Raster-bars-Perl6.png (offsite .png image)]
real Angle, Phase;
[SetVid($13); \set 320x200x8 graphics
for N:= 0 to 255 do \set palette colors to
SetPalette(N, N<<2, (7*N/8)<<2, 0); \ copper, brass, and gold
Angle:= 0.;
repeat WaitForVSync; \regulate speed
Clear; \erase screen
for N:= 2 to 9 do
[Phase:= float(N)*0.4;
Y:= fix(80.*Sin(Angle+Phase));
ShowBar(Y+100, 2*N, 64*((3&N)+1)-1);
];
Angle:= Angle + 0.04;
until KeyHit;
]</syntaxhighlight>
9,476

edits