Greyscale bars/Display: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(57 intermediate revisions by 19 users not shown)
Line 7:
 
Halfway down the display, we start with black, and produce 32 bars, ending in white, and for the last quarter, we start with white and step through 62 shades of grey, before finally arriving at black in the bottom right hand corner, producing a total of 64 bars for the bottom quarter.
 
=={{header|ActionScript}}==
<langsyntaxhighlight ActionScriptlang="actionscript">package
{
import flash.display.Sprite;
Line 33 ⟶ 34:
}
}
}</langsyntaxhighlight>
 
=={{header|Ada}}==
{{libheader|GTK}}
{{libheader|GtkAda}}
<langsyntaxhighlight Adalang="ada">with Gtk.Window; use Gtk.Window;
with Gtk.Enums;
with Gtk.Handlers;
Line 123 ⟶ 124:
Gtk.Main.Main;
end Greyscale;
</syntaxhighlight>
</lang>
=={{header|ANSI Standard BASIC}}==
 
=={{transheader|BBCAmazing BASICHopper}}==
{{trans|AWK}}
Version: hopper-FLOW!
<syntaxhighlight lang="amazing hopper">
#include <flow.h>
#include <flow-term.h>
 
#define SPACE(_T_,_N_) REPLICATE( " ", {_T_}DIV-INTO(_N_) )
<lang ANSI Standard BASIC>100 SET WINDOW 0,1279,0,1023
 
110 REM (0,0) is the bottom left of the display
DEF-MAIN(argv,argc)
120 SET AREA COLOR 1 ! Select color one for drawing
CLR-SCR
130 FOR row=1 TO 4
GOSUB( Print Grey Scale )
140 LET n=IP(2^(row+2))
END
150 LET w=IP(1280/n)
 
160 LET py=IP(256*(4-row))
RUTINES
170 FOR b=0 TO n-1
 
180 LET g=b/(n-1)
DEF-FUN( Print Grey Scale )
190 IF n=16 OR n=64 THEN LET g=1-g
SET( nrcolors, 8 )
200 SET COLOR MIX(1) g,g,g ! Reprogram color 1 to the gray we want
SET( direction, 1 )
210 PLOT AREA: w*b,py; w*b+w,py; w*b+w,py+256; w*b,py+256
MSET( quarter, color )
220 NEXT b
LOCATE( 0, 0 )
230 NEXT row
FOR( LT?( quarter, 4 ), ++quarter )
240 END</lang>
SET( height, 0 )
FOR( LT?( height, 5 ), ++height )
SET( width, 0 )
FOR( LT?( width, nrcolors ), ++width )
LET( color := CEIL( MUL( width, DIV( 255, SUB(nrcolors,1) ) ) ) )
WHEN( NOT( MOD( direction, 2 ) ) ){
LET( color := SUB( 255, color ) )
}
PRN( COLOR-RGBB( color, color, color) SPACE( 128, nrcolors ) )
NEXT
PRNL("\OFF")
NEXT
nrcolors*=2
++direction
NEXT
RET
</syntaxhighlight>
 
=={{header|AutoHotkey}}==
Requires the GDI+ Standard Library by tic: http://www.autohotkey.com/forum/viewtopic.php?t=32238
<langsyntaxhighlight AHKlang="ahk">h := A_ScreenHeight
w := A_ScreenWidth
pToken := gdip_Startup()
Line 201 ⟶ 223:
QColor(r, g, b){
return 0xFF000000 | (r << 16) | (g << 8) | (b)
}</langsyntaxhighlight>
 
=={{header|BASIC256}}==
=={{header|AWK}}==
<lang>
<syntaxhighlight lang="awk">
BEGIN {
nrcolors = 8
direction = 1
 
for (quarter=0; quarter<4; quarter++) {
for (height=0; height<5; height++) {
for (width=0; width<nrcolors; width++) {
# gradient goes white-to-black or black-to-white depending on direction
if (direction % 2)
color = width * (255 / (nrcolors-1))
else
color = 255 - width * (255 / (nrcolors-1))
 
# print (ANSI) RGB greysacle color and amount of spaces
printf("\033[48;2;%d;%d;%dm%*s", color, color, color, 64 / nrcolors, " ")
}
# reset color and print newline
printf("\033[0m\n")
}
 
# 8, 16, 32, 64 colors and alternating direction of gradient
nrcolors *= 2
direction++
}
}
</syntaxhighlight>
 
=={{header|BASIC}}==
==={{header|ANSI BASIC}}===
{{trans|BBC BASIC}}
{{works with|Decimal BASIC}}
<syntaxhighlight lang="basic">100 SET WINDOW 0,1279,0,1023
110 REM (0,0) is the bottom left of the display
120 SET AREA COLOR 1 ! Select color one for drawing
130 FOR row=1 TO 4
140 LET n=IP(2^(row+2))
150 LET w=IP(1280/n)
160 LET py=IP(256*(4-row))
170 FOR b=0 TO n-1
180 LET g=b/(n-1)
190 IF n=16 OR n=64 THEN LET g=1-g
200 SET COLOR MIX(1) g,g,g ! Reprogram color 1 to the gray we want
210 PLOT AREA: w*b,py; w*b+w,py; w*b+w,py+256; w*b,py+256
220 NEXT b
230 NEXT row
240 END</syntaxhighlight>
 
==={{header|BASIC256}}===
<syntaxhighlight lang="text">
h=ceil(graphheight/4)
for row=1 to 4
Line 214 ⟶ 286:
next n
next row
</syntaxhighlight>
</lang>
 
 
==={{header|BBC BASIC}}===
<langsyntaxhighlight lang="bbcbasic">MODE 8:REM 640 x 512 pixel display mode: BBC BASIC gives 2 graphics points per pixel
REM (0,0) is the bottom left of the display
GCOL 1 :REM Select colour one for drawing
Line 231 ⟶ 302:
RECTANGLE FILL w%*b%,py%,w%,256
NEXT b%
NEXT row%</langsyntaxhighlight>
 
=={{header|C}}==
{{libheader|GTK}}
<langsyntaxhighlight lang="c">#include <gtk/gtk.h>
/* do some greyscale plotting */
void gsplot (cairo_t *cr,int x,int y,double s) {
Line 269 ⟶ 340:
gtk_main ();
return 0;
} </langsyntaxhighlight>
 
=={{header|C sharp|C#}}==
{{libheader|System.Windows.Forms}}
<langsyntaxhighlight lang="csharp">using System;
using System.Drawing;
using System.Windows.Forms;
Line 305 ⟶ 376:
return colorBars;
}
}</langsyntaxhighlight>
 
=={{header|C++}}==
Line 312 ⟶ 383:
file greytones.h
</PRE>
<langsyntaxhighlight lang="cpp">#ifndef MYWIDGET_H
#define MYWIDGET_H
#include <QWidget>
Line 325 ⟶ 396:
void paintEvent( QPaintEvent * ) ;
} ;
#endif</langsyntaxhighlight>
<pre>file greytones.cpp</pre>
<langsyntaxhighlight lang="cpp">#include <QtGui>
#include "greytones.h"
 
Line 360 ⟶ 431:
run++ ;
}
}</langsyntaxhighlight>
<PRE>
file main.cpp
</PRE>
<langsyntaxhighlight lang="cpp">#include <QApplication>
#include "greytones.h"
 
Line 373 ⟶ 444:
window.show( ) ;
return app.exec( ) ;
}</langsyntaxhighlight>
 
=={{Headerheader|Component Pascal}}==
{{works with|Black Box Component Builder}}
 
[[File:GreyScaleCp.png|thumb|right]]
 
<langsyntaxhighlight lang="oberon2">
MODULE RosettaGreys;
IMPORT Views, Ports, Properties, Controllers, StdLog;
Line 457 ⟶ 528:
 
"RosettaGreys.Deposit; StdCmds.Open"
</syntaxhighlight>
</lang>
=={{header|Easyprog.online}}==
 
=={{header|Delphi}}==
<lang easyprog.online>n = 8
{{works with|Delphi|6.0}}
for row range 4
{{libheader|Windows,Types,StdCtrls,ExtCtrls,SysUtils,Graphics}}
sz# = 100 / n
Uses Delphi graphic objects to create a subroutine which draws rows of bars according to specificification for number of bars and rows, the row number and the direction of the color graident.
for i range n
 
c# = i / (n - 1)
<syntaxhighlight lang="Delphi">
if row mod 2 = 1
procedure DrawBar(Image: TImage; Bars,Rows,Row: integer; WhiteToBlack: boolean);
c# = 1 - c#
{Draw horizontal bar according the following parameters:}
.
{Bars = number of color bars to fit into the horizontal space}
color_red c#
{Rows = number of rows to fit into the vertical space}
color_green c#
{Row = the row to place the current bar - numbered 0..n, top to bottom}
color_blue c#
{WhiteToBlack - if true, bars in the row go from white to back}
move sz# * i row * 25
var X: integer;
rect sz# + 1 25
var Color: integer;
.
var ColorStep: double;
n = n * 2
var BarHeight: integer;
.</lang>
var R,R2: TRect;
begin
{Calculate bar dimensions}
BarHeight:=Image.Height div Rows;
R:=Rect(0,0,(Image.Width div Bars)+1, BarHeight);
OffsetRect(R,0,BarHeight * Row);
R2:=R;
{Calculate color parameters}
ColorStep:=255/(Bars-1);
if WhiteToBlack then
begin
Color:=255;
ColorStep:=-ColorStep
end
else Color:=0;
{Draw bars}
for X:=1 to Bars do
begin
{Set color}
Image.Canvas.Brush.Color:=RGB(Color,Color,Color);
Image.Canvas.Pen.Color:=RGB(Color,Color,Color);
{Draw rectangular bar}
Image.Canvas.Rectangle(R2);
{Move rectangle and calculate color}
OffsetRect(R2,R.Right,0);
Color:=Round(X * ColorStep);
end;
end;
 
 
procedure ShowGrayBars(Image: TImage);
{Draw four bar, with alternating color scheme}
begin
DrawBar(Image,8,4,0,False);
DrawBar(Image,16,4,1,True);
DrawBar(Image,32,4,2,False);
DrawBar(Image,64,4,3,True);
end;
</syntaxhighlight>
{{out}}
[[File:DelphGrayBars.png|thumb|none]]
<pre>
</pre>
 
=={{header|EasyLang}}==
 
[https://easylang.dev/show/#cod=PY5BDoMgEEX3nOIvK00RMKbdeBqKCYkyDZqa9PSdMSAr5v3PGzImvNRMBYUOvlvshEEB2H48OmvRI8ssndQaGQ84oXwCw8St2wm7StN8Gld6w4uo4tp3XA0VmeahhcrAcbiilb5RPqJ5wXPkN6LU8GPNSwy75HcWXnBbYvzAGutVs2foCV4Z9Qc= Run it]
 
<syntaxhighlight lang="text">
n = 8
for row = 0 to 3
sz = 100 / n
for i = 0 to n - 1
c = i / (n - 1)
if row mod 2 = 1
c = 1 - c
.
color3 c c c
move sz * i 75 - row * 25
rect sz + 1 25
sleep 0.02
.
n *= 2
.
</syntaxhighlight>
 
=={{header|Eiffel}}==
 
[https://github.com/ljr1981/rosettacode_answers/blob/main/testing/rc_greyscale_bars/rc_greyscale_bars_test_set.e Full Example Code]
 
[https://github.com/ljr1981/rosettacode_answers/blob/main/testing/rc_greyscale_bars/eifgreyscale.png PNG Output]
 
<syntaxhighlight lang="eiffel">
feature -- Test routines
 
rc_greyscale_bars_test
-- Greyscale bars/Display
note
testing:
"execution/isolated",
"execution/serial"
local
y: INTEGER
do
y := 0
paint_row (black, y, r1_div_count, r1_width)
 
y := y + row_height
paint_row (white, y, r2_div_count, r2_width)
 
y := y + row_height
paint_row (black, y, r3_div_count, r3_width)
 
y := y + row_height
paint_row (white, y, r4_div_count, r4_width)
 
pic.save_to_named_file (create {EV_PNG_FORMAT}, ".\testing\rc_greyscale_bars\eifgreyscale.png")
end
 
feature {NONE} -- Test Support
 
paint_row (a_init_color: REAL; a_y, r_div, r_width: INTEGER)
-- `paint_row' with rectangles from `black' to `white' or reverse.
require
valid_color: a_init_color = white or else a_init_color = black
valid_y: (<<0,row_height * 1, row_height * 2, row_height * 3>>).has (a_y)
valid_div: (<<r1_div_count, r2_div_count, r3_div_count, r4_div_count>>).has (r_div)
valid_width: (<<r1_width, r2_width, r3_width, r4_width>>).has (r_width)
local
color: REAL
x, dir: INTEGER
do
color := a_init_color
if color = white then dir := down else dir := up end
⟳ i:1 |..| r_div ¦
pic.set_foreground_color (create {EV_COLOR}.make_with_rgb (color, color, color))
pic.fill_rectangle (x, a_y, r_width, row_height)
color := color + ((1/r_div).truncated_to_real * dir)
x := x + r_width
end
 
feature -- Constants
 
pic: EV_PIXMAP
once
create Result.make_with_size (width, height)
end
 
width: INTEGER = 1024
height: INTEGER = 768
 
row_height: INTEGER once Result := (height / 4).truncated_to_integer end
 
r1_width: INTEGER = 128; r1_div_count: INTEGER = 8 --| width of each rectangle; number of rectangles on this row
r2_width: INTEGER = 64; r2_div_count: INTEGER = 16
r3_width: INTEGER = 32; r3_div_count: INTEGER = 32
r4_width: INTEGER = 16; r4_div_count: INTEGER = 64
 
black: REAL = 0.0
white: REAL = 1.0
 
down: INTEGER = -1 --| From `white' to `black' or ...
up: INTEGER = 1 --| From `black' to `white'
</syntaxhighlight>
 
=={{header|Euler Math Toolbox}}==
<syntaxhighlight lang="euler math toolbox">
<lang Euler Math Toolbox>
>function grayscale(y1,y2,n,direction=1) ...
$ loop 0 to n-1;
Line 499 ⟶ 714:
$endfunction
>grayscales:
</syntaxhighlight>
</lang>
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">' version 01-09-2017
' compile with: fbc -s console
' or compile with: fbc -s gui
Line 535 ⟶ 751:
 
Sleep
End</langsyntaxhighlight>
 
=={{header|Frink}}==
<langsyntaxhighlight lang="frink">
fakewidth =!= dummy
 
g = new graphics
g.antialiased[false]
drawBars[g, 0, 1, 0, 1/4, 8]
drawBars[g, 1, 0, 1/4, 1/2, 16]
Line 559 ⟶ 776:
}
}
</syntaxhighlight>
</lang>
 
=={{header|FutureBasic}}==
[[File:GrayscalesR.png|thumb|right]]
<syntaxhighlight lang="futurebasic">
_window = 1
 
void local fn BuildGrayBarWindow
NSInteger i
float gray
CGRect r = fn CGRectMake( 0, 0, 640, 400 )
window _window, @"Gray Scale Window", r
WindowSetBackgroundColor( _window, fn ColorBlack )
gray = 0.0
r = fn CGrectMake( 0, 300, 80, 100 )
for i = 1 to 8
textfield i, YES,,r,_window
TextFieldSetEditable( i, NO )
TextFieldSetBackgroundColor( i, fn ColorWithRGB( gray, gray, gray, 1.0 ) )
r = fn CGRectOffset( r, 80, 0 )
gray += 0.142857142857143 // 1/7
next
gray = 1.0
r = fn CGrectMake( 0, 200, 40, 100 )
for i = 11 to 26
textfield i, YES,,r,_window
TextFieldSetEditable( i, NO )
TextFieldSetBackgroundColor( i, fn ColorWithRGB( gray, gray, gray, 1.0 ) )
r = fn CGRectOffset( r, 40, 0 )
gray -= 0.066666666666667 // 1/5
next
gray = 0.0
r = fn CGrectMake( 0, 100, 20, 100 )
for i = 31 to 62
textfield i, YES,,r,_window
TextFieldSetEditable( i, NO )
TextFieldSetBackgroundColor( i, fn ColorWithRGB( gray, gray, gray, 1.0 ) )
r = fn CGRectOffset( r, 20, 0 )
gray += 0.032258064516129 // 1/31
next
gray = 1.0
r = fn CGrectMake( 0, 0, 10, 100 )
for i = 101 to 164
textfield i, YES,,r,_window
TextFieldSetEditable( i, NO )
TextFieldSetBackgroundColor( i, fn ColorWithRGB( gray, gray, gray, 1.0 ) )
r = fn CGRectOffset( r, 10, 0 )
gray -= 0.015873015873016 // 1/63
next
end fn
 
fn BuildGrayBarWindow
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
[See accompanying screenshot.]
</pre>
And here's another clever and shorter solution:
<syntaxhighlight lang="futurebasic">void local fn BuildWindow
CGRect r = {0,0,640,400}
long i, j, bars = 8
CGFloat gray, delta, y = 0.0, w = 80
window 1, @"Grayscale Bars", r
pen -1
for j = 1 to 4
delta = 1.0/(bars-1)
if ( j mod 2 ) then gray = 0.0 else gray = 1.0 : delta = -delta
r = fn CGRectMake( 0, y, w, 100 )
for i = 1 to bars
rect fill r, fn ColorWithWhite( gray, 1.0 )
r.origin.x += w
gray += delta
next
bars = bars << 1
y += 100 : w = w/2
next
end fn
 
fn BuildWindow
 
HandleEvents</syntaxhighlight>
 
=={{header|Gambas}}==
<langsyntaxhighlight lang="gambas">Public Sub Form_Open()
Dim iRow, iCol, iClr As Integer 'For Row, Column and Colour
Dim iInc As Integer = 4 'To calculate RGB colour
Line 590 ⟶ 896:
Next
 
End</langsyntaxhighlight>
 
'''[http://www.cogier.com/gambas/GreyScale.png Click here for image of the output]'''
Line 597 ⟶ 903:
{{libheader|Go Graphics}}
{{trans|Java}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 636 ⟶ 942:
greyBars(dc)
dc.SavePNG("greybars.png")
}</langsyntaxhighlight>
 
{{out}}
Line 646 ⟶ 952:
This program uses an inlined XPM file which is scaled to fill an entire GTK fullscreen window
 
<langsyntaxhighlight Haskelllang="haskell">import Graphics.UI.Gtk
import Graphics.UI.Gtk.Gdk.GC
import Control.Monad.Trans (liftIO)
Line 703 ⟶ 1,009:
"AADDFFHHJJLLNNPPRRTTVVXXZZbbddffhhjjllnnpprrttvvxxzz11336688..**",
"*+.9876543210zyxwvutsrqponmlkjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCA"
]</langsyntaxhighlight>
 
=={{header|Icon}} and {{header|Unicon}}==
Line 710 ⟶ 1,016:
 
[[File:Greyscale_unicon.png|thumb|right]]
<langsyntaxhighlight Iconlang="icon">link graphics,printf,numbers
procedure main()
Line 740 ⟶ 1,046:
put(TC.bands,band(height)) # bottom sentinal
return TC
end</langsyntaxhighlight>
 
{{libheader|Icon Programming Library}}
Line 749 ⟶ 1,055:
=={{header|J}}==
'''Solution:'''
<langsyntaxhighlight lang="j"> load 'viewmat'
NB. size=. 2{.".wd'qm' NB. J6
NB. size=. getscreenwh_jgtk_ '' NB. J7
size=. 2 3{_".wd'qscreen' NB. J9
rows=. (2^3+i.4),._1^i.4
bars=. ((64%{.)#[:(<:@|%~i.)*/)"1 rows
togreyscale=. (256#. [:<.255 255 255&*)"0
'rgb' viewmat (4<.@%~{:size)# (64<.@%~{.size)#"1 togreyscale bars</lang>
wd 'pmove 0 _30 ',":size [ wd 'psel ',1{::,hforms_jviewmat_'' NB. J9</syntaxhighlight>
 
Note that thishardware solutionchanges isover notthe postedyears directlyhave toleft thetheir screenmark buton tointerface a viewmat windowprotocols, which mayshows up here notas belanguage centeredchanges.
 
=={{header|Java}}==
using basically the same code as in the C++ example
<langsyntaxhighlight Javalang="java">import javax.swing.* ;
import java.awt.* ;
 
Line 807 ⟶ 1,115:
Greybars gb = new Greybars( ) ;
}
}</langsyntaxhighlight>
 
=={{header|JavaScript}}==
Live Demo: http://jsfiddle.net/gcN9g/embedded/result/
<langsyntaxhighlight JavaScriptlang="javascript"><html><body>
<script type="text/javascript">
var width = 640; var height = 400;
Line 840 ⟶ 1,148:
</script>
</body></html>
</syntaxhighlight>
</lang>
 
 
=={{header|Julia}}==
<langsyntaxhighlight Julialang="julia">using Gtk, Cairo, ColorTypes
 
function generategrays(n, screenwidth)
Line 898 ⟶ 1,205:
endit(w) = notify(cond)
signal_connect(endit, win, :destroy)
wait(cond)</langsyntaxhighlight>
 
=={{header|Kotlin}}==
{{trans|Java}}
<langsyntaxhighlight lang="scala">// version 1.1
 
import java.awt.Color
Line 952 ⟶ 1,259:
fun main(args: Array<String>) {
GreyBars()
}</langsyntaxhighlight>
 
=={{header|Liberty BASIC}}==
Black boxes were added around each color for ease of counting the boxes.
<syntaxhighlight lang="lb">
<lang lb>
nomainwin
 
Line 989 ⟶ 1,296:
end
 
</syntaxhighlight>
</lang>
Resulting [http://www.diga.me.uk/greyscale.gif GreyScale image] without the outlines.
 
=={{header|Lua}}==
{{libheader|nw}}
{{libheader|cairo}}
<syntaxhighlight lang="lua">local nw = require("nw")
local app = nw:app()
local cw, ch = 320, 240
local win = app:window(cw, ch, "Grayscale Bars", false)
function win:repaint()
local cr = win:bitmap():cairo()
local ystride = ch/4
for y = 0, 3 do
local i, n = 1, 2^(y+3)
local xstride = cw/n
for x = 0, n-1 do
cr:rectangle(x*xstride, y*ystride, xstride, ystride)
local gray = x / (n-1)
if y%2>0 then gray=1-gray end
cr:rgb(gray, gray, gray)
cr:fill()
end
end
end
win:show()
app:run()</syntaxhighlight>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
 
<langsyntaxhighlight lang="mathematica">CreateDocument[ Graphics[ Flatten@Table[
{ If[EvenQ[#3], GrayLevel[ 1. - j/#1 ], GrayLevel[ j/#1 ]],
Rectangle[{j #2, 7*#3}, {#2 (j + 1), (#3 + 1) 7}]}, {j, 0, #1}] & @@@
{{7, 8, 3}, {15, 4, 2}, {31, 2, 1}, {63, 1, 0} }
,ImageSize -> Full], WindowFrame -> "Frameless", WindowSize -> Full]</langsyntaxhighlight>
[[File:greyscales.jpg|thumb|right]]
 
Line 1,004 ⟶ 1,336:
 
3ds max provides customizable maps like gradient to the user, but you can also write it:
<syntaxhighlight lang="maxscript">
<lang MAXScript>
fn drawBarRow _bmp _row _width _number _inverse=
(
Line 1,074 ⟶ 1,406:
b = bitmap_verticalBars()
display b
</syntaxhighlight>
</lang>
 
=={{header|Nim}}==
{{libheader|gintro}}
<syntaxhighlight lang="nim">import gintro/[glib, gobject, gtk, gio, cairo]
 
const
Width = 640
Height = 480
 
#---------------------------------------------------------------------------------------------------
 
proc draw(area: DrawingArea; context: Context) =
## Draw the greyscale bars.
 
const
Black = 0.0
White = 1.0
 
var y = 0.0
var nrect = 8
let rectHeight = Height / 4
 
# Draw quarters.
for quarter in 0..3:
let rectWidth = Width / nrect
var x = 0.0
var (grey, incr) = if (quarter and 1) == 0: (Black, 1 / nrect) else: (White, -1 / nrect)
 
# Draw rectangles.
for _ in 1..nrect:
context.rectangle(x, y, rectWidth, rectHeight)
context.setSource([grey, grey, grey])
context.fill()
x += rectWidth
grey += incr
 
y += rectHeight
nrect *= 2
 
#---------------------------------------------------------------------------------------------------
 
proc onDraw(area: DrawingArea; context: Context; data: pointer): bool =
## Callback to draw/redraw the drawing area contents.
 
area.draw(context)
result = true
 
#---------------------------------------------------------------------------------------------------
 
proc activate(app: Application) =
## Activate the application.
 
let window = app.newApplicationWindow()
window.setSizeRequest(Width, Height)
window.setTitle("Greyscale bars")
 
# Create the drawing area.
let area = newDrawingArea()
window.add(area)
 
# Connect the "draw" event to the callback to draw the spiral.
discard area.connect("draw", ondraw, pointer(nil))
 
window.showAll()
 
#———————————————————————————————————————————————————————————————————————————————————————————————————
 
let app = newApplication(Application, "Rosetta.GreyscaleBars")
discard app.connect("activate", activate)
discard app.run()</syntaxhighlight>
 
=={{header|OCaml}}==
 
<langsyntaxhighlight lang="ocaml">open Graphics
 
let round x = truncate (floor (x +. 0.5))
Line 1,099 ⟶ 1,501:
done
) bars;
ignore(read_key())</langsyntaxhighlight>
 
Run with:
Line 1,105 ⟶ 1,507:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">sub partition {
my($all, $div) = @_;
my @marks = 0;
Line 1,141 ⟶ 1,543:
bars($h2,$w,4,1),
bars($h3,$w,5,0),
bars($h4,$w,6,1);</langsyntaxhighlight>
[https://github.com/SqrtNegInf/Rosettacode-Perl5-Smoke/blob/master/ref/Greyscale-bars-perl5.png See Greyscale-bars-perl5] (offsite image)
 
=={{header|Perl 6Phix}}==
Resizeable. Use of nx avoids rounding/misalignment errors
<lang perl6>my ($width,$height) = 1280,768;
{{libheader|Phix/pGUI}}
{{libheader|Phix/online}}
You can run this online [http://phix.x10.mx/p2js/Greyscale_bars.htm here].
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\Greyscale_bars.exw
-- ===============================
--</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</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>
my $PGM = open "Greyscale-bars-perl6.pgm", :w orelse die "Can't create Greyscale-bars-perl6.pgm: $_";
<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: #004080;">integer</span> <span style="color: #000000;">quit</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
$PGM.print: qq:to/EOH/;
<span style="color: #004080;">bool</span> <span style="color: #000000;">refire</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">false</span>
P2
# Greyscale-bars-perl6.pgm
$width $height
65535
EOH
<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>
my ($h1,$h2,$h3,$h4) = divvy($height,4);
<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: #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: #004080;">integer</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;">4</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">height</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">row</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">4</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">x</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">p2</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">row</span><span style="color: #0000FF;">+</span><span style="color: #000000;">2</span><span style="color: #0000FF;">),</span> <span style="color: #000000;">c</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;">p2</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">))</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">to</span> <span style="color: #000000;">p2</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">integer</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: #000000;">n</span><span style="color: #0000FF;">*</span><span style="color: #000000;">#010101</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">and_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">row</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #000000;">colour</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">xor_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">colour</span><span style="color: #0000FF;">,</span><span style="color: #000000;">#FFFFFF</span><span style="color: #0000FF;">)</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: #004080;">integer</span> <span style="color: #000000;">nx</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">ceil</span><span style="color: #0000FF;">(</span><span style="color: #000000;">width</span><span style="color: #0000FF;">*(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)/</span><span style="color: #000000;">p2</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;">x</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">nx</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">4</span><span style="color: #0000FF;">-</span><span style="color: #000000;">row</span><span style="color: #0000FF;">)*</span><span style="color: #000000;">h</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">height</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">x</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">nx</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #000000;">height</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">4</span><span style="color: #0000FF;">-</span><span style="color: #000000;">row</span><span style="color: #0000FF;">)*</span><span style="color: #000000;">h</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</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: #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>
my @nums = ((0/7,1/7...7/7) X* 65535)».floor;
<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>
my $line = ~(@nums Zxx divvy($width,8));
<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>
$PGM.say: $line for ^$h1;
<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>
@nums = ((15/15,14/15...0/15) X* 65535)».floor;
<span style="color: #7060A8;">cdKillCanvas</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">)</span>
$line = ~(@nums Zxx divvy($width,16));
<span style="color: #7060A8;">cdKillCanvas</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cdcanvas</span><span style="color: #0000FF;">)</span>
$PGM.say: $line for ^$h2;
<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>
@nums = ((0/31,1/31...31/31) X* 65535)».floor;
<span style="color: #7060A8;">IupOpen</span><span style="color: #0000FF;">()</span>
$line = ~(@nums Zxx divvy($width,32));
<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>
$PGM.say: $line for ^$h3;
<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="Greyscale bars"`</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>
@nums = ((63/63,62/63...0/63) X* 65535)».floor;
<!--</syntaxhighlight>-->
$line = ~(@nums Zxx divvy($width,64));
$PGM.say: $line for ^$h4;
$PGM.close;
sub divvy($all, $div) {
my @marks = ((1/$div,2/$div ... 1) X* $all)».round;
@marks Z- 0,|@marks;
}</lang>
[https://github.com/SqrtNegInf/Rosettacode-Perl6-Smoke/blob/master/ref/Greyscale-bars-perl6.png See Greyscale-bars-perl6] (offsite image)
 
=={{header|Phix}}==
Resizeable. Use of nx avoids rounding/misalignment errors
{{libheader|pGUI}}
<lang Phix>--
-- demo\rosetta\Greyscale_bars.exw
--
include pGUI.e
 
Ihandle dlg, canvas
cdCanvas cddbuffer, cdcanvas
 
function redraw_cb(Ihandle /*ih*/, integer /*posx*/, integer /*posy*/)
cdCanvasActivate(cddbuffer)
integer {width, height} = IupGetIntInt(canvas, "DRAWSIZE")
integer h = ceil(height/4)
for row=1 to 4 do
integer x = 0, p2 = power(2,row+2), c = floor(255/(p2-1))
for n=0 to p2-1 do
integer colour = c*n*#010101
if and_bits(row,1)=0 then colour = xor_bits(colour,#FFFFFF) end if
cdCanvasSetForeground(cddbuffer, colour)
integer nx = ceil(width*(n+1)/p2)
cdCanvasBox(cddbuffer, x, nx, height-h, height)
x = nx
end for
height -= h
end for
cdCanvasFlush(cddbuffer)
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 esc_close(Ihandle /*ih*/, atom c)
if c=K_ESC then return IUP_CLOSE end if
return IUP_CONTINUE
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", "Greyscale bars")
IupSetCallback(canvas, "ACTION", Icallback("redraw_cb"))
IupSetCallback(dlg, "K_ANY", Icallback("esc_close"))
 
IupMap(dlg)
IupSetAttribute(canvas, "RASTERSIZE", NULL)
 
IupShowXY(dlg,IUP_CENTER,IUP_CENTER)
 
IupMainLoop()
 
IupClose()
end procedure
 
main()</lang>
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(let Pgm # Create PGM of 384 x 288 pixels
(make
(for N 4
Line 1,272 ⟶ 1,632:
(prinl (length (car Pgm)) " " (length Pgm))
(prinl 255)
(for Y Pgm (apply wr Y)) ) )</langsyntaxhighlight>
 
=={{header|Plain English}}==
<syntaxhighlight lang="plainenglish">To run:
Start up.
Clear the screen.
Imagine a box with the screen's left and the screen's top and the screen's right and the screen's bottom divided by 4.
Make a gradient with the box and 8 and "left-to-right".
Draw the gradient.
Draw the next gradient given the gradient.
Draw the next gradient given the gradient.
Draw the next gradient given the gradient.
Refresh the screen.
Wait for the escape key.
Shut down.
 
A gradient is a record with
A box,
A partitions number,
And a direction string. \"left-to-right" or "right-to-left"
 
To make a gradient with a box and a number and a string:
Put the box into the gradient's box.
Put the number into the gradient's partitions.
Put the string into the gradient's direction.
 
To draw a gradient:
Put the white color into a color.
Put 1000 [the maximum lightness] divided by the gradient's partitions into an amount.
If the gradient's direction is "left-to-right", put the black color into the color.
Put the gradient's box into a box.
Put the gradient's box's width divided by the gradient's partitions into a width number.
Put the width plus the box's left into the box's right.
Loop.
If a counter is past the gradient's partitions, exit.
Draw and fill the box with the color.
Move the box right the width.
If the gradient's direction is "left-to-right", lighten the color by the amount; repeat.
Darken the color by the amount.
Repeat.
 
To draw the next gradient given a gradient:
Move the gradient's box down the gradient's box's height.
Double the gradient's partitions.
If the gradient's direction is "left-to-right", set a flag.
If the flag is set, put "right-to-left" into the gradient's direction.
If the flag is not set, put "left-to-right" into the gradient's direction.
Draw the gradient.</syntaxhighlight>
 
=={{header|Processing}}==
<syntaxhighlight lang="java">
//Aamrun, 3rd July 2022
 
void drawPanel(int startColour,int endColour,int bars,int startY){
int rectWidth = width / bars,rectHeight = height / 4, startX = 0,increment;
increment = (endColour - startColour)/(bars-1);
for(int i = 0;i < bars;i++){
fill(startColour + i*increment);
rect(startX + i*rectWidth,startY,rectWidth,rectHeight);
}
}
 
void setup(){
size(1280,960);
drawPanel(0,255,8,0);
drawPanel(255,0,16,height/4);
drawPanel(0,255,32,height/2);
drawPanel(255,0,64,3*height/4);
}
</syntaxhighlight>
 
=={{header|PureBasic}}==
<syntaxhighlight lang="purebasic">EnableExplicit
<lang PureBasic>If Not InitKeyboard(): End: EndIf ;can't init keyboard
If Not InitSprite(): End: EndIf ;can't init sprite/screen library
If Not ExamineDesktops(): End: EndIf ;can't retrieve information about desktop
 
Macro Check(Function)
Define height.f, width.f, depth
If Not Function : End : EndIf
height.f = DesktopHeight(0)
EndMacro
width.f = DesktopWidth(0)
depth = DesktopDepth(0)
 
Check(InitKeyboard()) ; Cannot initialize keyboard
If OpenScreen(width, height, depth, "Press ENTER to exit")
Check(InitSprite()) ; Cannot initialize sprite/screen library
Define vsCount, v, h, columns, columnWidth, endColor, shade
Check(ExamineDesktops()) ; Cannot retrieve informations about desktops
StartDrawing(ScreenOutput())
vsCount = 4
For v = 0 To 3
columns = (v + 1) * 8
columnWidth = Round(width / columns, #PB_Round_Up)
endColor = $FFFFFF * (v % 2) ;alternate between black and white for first and last bar
Box(0, (height * v) / vsCount, columnWidth, height / vsCount, endColor)
 
Define.i iHeight, iWidth, iDepth
For h = 1 To columns - 2
iHeight = DesktopHeight(0)
If v % 2 = 0
iWidth = DesktopWidth(0)
shade = 256 / columns * (h + 1)
iDepth = DesktopDepth(0)
Else
 
shade = 256 / columns * (columns - (h + 1))
If OpenScreen(iWidth, iHeight, iDepth, "Press ENTER to exit")
EndIf
Define.i bMode.b, iLines, fLine.f, iRow, iSpans, fSpan.f,
Box((width * h) / columns, (height * v) / vsCount, columnWidth, height / vsCount, RGB(shade, shade, shade))
fColor.f, iTop, iWide, iHigh, iCol, iShade
If StartDrawing(ScreenOutput())
 
bMode = #True ; Pow = #True; Add = #False
iLines = 4 ; Number of Lines
 
If iLines < 1 : iLines = 1 : EndIf ; Pow/Add-Min
If bMode
If iLines > 6 : iLines = 6 : EndIf ; Pow-Max
Else
If iLines > 32 : iLines = 32 : EndIf ; Add-Max
EndIf
fLine = iHeight / iLines
iLines - 1
 
For iRow = 0 To iLines
If bMode
iSpans = Pow(2, iRow + 3) - 1 ; Pow: 8, 16, 32, 64, 128, 256
Else
iSpans = (iRow + 1) * 8 - 1 ; Add: 8, 16, 24, 32, 40, 48, ...
EndIf
fSpan = iWidth / (iSpans + 1)
fColor = 255 / iSpans
iTop = Round(iRow * fLine, #PB_Round_Up)
iWide = Round(fSpan, #PB_Round_Up)
iHigh = Round(fLine, #PB_Round_Up)
For iCol = 0 To iSpans
iShade = Round(fColor * iCol, #PB_Round_Nearest)
If iRow % 2 <> 0 : iShade = 255 - iShade : EndIf ; Alternation
Box(Round(iCol * fSpan, #PB_Round_Up), iTop, iWide, iHigh,
RGB(iShade, iShade, iShade))
Next
Next
 
Box((width * (columns - 1)) / columns, (height * v) / vsCount, columnWidth, height / vsCount, $FFFFFF - endColor)
Next StopDrawing()
StopDrawing FlipBuffers()
FlipBuffers()
 
Repeat
Delay(1030)
ExamineKeyboard()
Until KeyboardPushed(#PB_Key_Escape) Or KeyboardPushed(#PB_Key_Return)
KeyboardPushed(#PB_Key_Return)
EndIf
CloseScreen()
EndIf</lang>
End</syntaxhighlight>
Press Enter or Escape to exit the programs's display.
 
=={{header|Python}}==
{{libheader|livewires}}
<langsyntaxhighlight Pythonlang="python">#!/usr/bin/env python
#four gray scaled stripes 8:16:32:64 in Python 2.7.1
 
Line 1,343 ⟶ 1,799:
while keys_pressed() != [' ']: # press spacebar to close window
pass
</syntaxhighlight>
</lang>
 
=={{header|Quackery}}==
 
<syntaxhighlight lang="Quackery"> [ $ "turtleduck.qky" loadfile ] now!
 
[ 1280 1 ] is width ( --> s )
[ 720 1 ] is height ( --> s )
 
[ dip [ 1 - dup ]
255 * swap /
1 swap
3 of dup colour fill
[ 2 times
[ width 2over v/ walk
-1 4 turn
height 4 1 v/ walk
-1 4 turn ]
2drop ] ] is bar ( n n --> )
 
[ 1 over times
[ over i bar
width 2over v/ fly ]
2drop ] is line ( n --> )
 
[ turtle
10 frames
width 2 1 v/ fly
1 4 turn
height 2 1 v/ fly
1 2 turn
height fly
-1 4 turn
' [ 8 16 32 64 ]
2 times
[ behead line
1 4 turn
height -2 1 v/ fly
1 4 turn
behead line
1 2 turn ]
drop
1 frames ] is greyscale-bars ( --> )
 
greyscale-bars</syntaxhighlight>
 
{{out}}
[[File:Quackery greyscale.png|center|thumb]]
 
=={{header|R}}==
Create a 4x64 matrix representing the described pattern, set margins to 0 so the image will fill the display, and plot the matrix in grayscale using the "image" function:
[[File:GrayscalesR.png|thumb|right]]
<syntaxhighlight lang="r">
<lang R>
mat <- matrix(c(rep(1:8, each = 8) / 8,
rep(16:1, each = 4) / 16,
Line 1,356 ⟶ 1,859:
par(mar = rep(0, 4))
image(t(mat[4:1, ]), col = gray(1:64/64), axes = FALSE)
</syntaxhighlight>
</lang>
 
Or, this can be generalized with the function below, which produces the pattern for an arbitrary number of rows (though rows become visibly indistinguishable after about row 5):
[[File:GrayscalesR-6.png|thumb|right]]
<syntaxhighlight lang="r">
<lang R>
grayscalesImage <- function(nrow = 4) {
X <- matrix(NA, nrow = nrow, ncol = 2^(nrow + 2))
Line 1,372 ⟶ 1,875:
## Example ##
grayscalesImage(6) # produces image shown in screenshot to the right
</syntaxhighlight>
</lang>
 
=={{header|Racket}}==
Line 1,380 ⟶ 1,883:
[[File:Grayscale-pict.png|thumb|right]]
 
<langsyntaxhighlight lang="racket">
#lang racket/gui
(require slideshow/pict)
Line 1,401 ⟶ 1,904:
(vc-append (grays 1/8 'right) (grays 1/16 'left)
(grays 1/32 'right) (grays 1/64 'left))
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" line>my ($width,$height) = 1280,768;
my $PGM = open "Greyscale-bars-perl6.pgm", :w orelse die "Can't create Greyscale-bars-perl6.pgm: $_";
$PGM.print: qq:to/EOH/;
P2
# Greyscale-bars-perl6.pgm
$width $height
65535
EOH
my ($h1,$h2,$h3,$h4) = divvy($height,4);
my @nums = ((0/7,1/7...7/7) X* 65535)».floor;
my $line = ~(@nums Zxx divvy($width,8));
$PGM.say: $line for ^$h1;
@nums = ((15/15,14/15...0/15) X* 65535)».floor;
$line = ~(@nums Zxx divvy($width,16));
$PGM.say: $line for ^$h2;
@nums = ((0/31,1/31...31/31) X* 65535)».floor;
$line = ~(@nums Zxx divvy($width,32));
$PGM.say: $line for ^$h3;
@nums = ((63/63,62/63...0/63) X* 65535)».floor;
$line = ~(@nums Zxx divvy($width,64));
$PGM.say: $line for ^$h4;
$PGM.close;
sub divvy($all, $div) {
my @marks = ((1/$div,2/$div ... 1) X* $all)».round;
@marks Z- 0,|@marks;
}</syntaxhighlight>
[https://github.com/SqrtNegInf/Rosettacode-Perl6-Smoke/blob/master/ref/Greyscale-bars-perl6.png See Greyscale-bars-perl6] (offsite image)
 
=={{header|RapidQ}}==
<syntaxhighlight lang="vb">
<lang vb>
Declare Sub PaintCanvas
 
Line 1,433 ⟶ 1,975:
 
Form.showmodal
</syntaxhighlight>
</lang>
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : Greyscale bars/Display
 
Line 1,495 ⟶ 2,037:
}
label1 { setpicture(p1) show() }
</syntaxhighlight>
</lang>
Output:
https://www.dropbox.com/s/01iywg04iwubf55/GreyscaleBars.jpg?dl=0
 
=={{header|Run BASIC}}==
<langsyntaxhighlight Runbasiclang="runbasic">for i = 1 to 4
incr = int(256 / (i * 8))
c = 256
Line 1,511 ⟶ 2,053:
next i
html "</table>"
end</langsyntaxhighlight>
<pre>Run in a browser</pre>
 
=={{header|Scala}}==
<langsyntaxhighlight lang="scala">import scala.swing._
 
class GreyscaleBars extends Component {
Line 1,533 ⟶ 2,075:
}
}
}</langsyntaxhighlight>
Open window:
[[File:greyscalebars_scala.png|thumb|right]]
<langsyntaxhighlight lang="scala">new MainFrame(){
title="Greyscale bars"
visible=true
preferredSize=new Dimension(640, 320)
contents=new GreyscaleBars()
}</langsyntaxhighlight>
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "draw.s7i";
include "keybd.s7i";
Line 1,574 ⟶ 2,116:
end for;
ignore(getc(KEYBOARD));
end func;</langsyntaxhighlight>
 
=={{header|Tcl}}==
{{libheader|Tk}}
<langsyntaxhighlight lang="tcl">package require Tcl 8.5
package require Tk 8.5
Line 1,598 ⟶ 2,140:
}
incr y $dy
}</langsyntaxhighlight>
 
=={{header|Wren}}==
{{libheader|DOME}}
<syntaxhighlight lang="wren">import "graphics" for Canvas, Color
import "dome" for Window
import "math" for Math
 
class GreyBars {
construct new(width, height) {
Window.title = "Grey bars example"
Window.resize(width, height)
Canvas.resize(width, height)
_w = width
_h = height
}
 
init() {
drawBars()
}
 
drawBars() {
var run = 0
var colorComp = 0 // component of the color
var columnCount = 8
while (columnCount < 128) {
var colorGap = 255 / (columnCount - 1) // by this gap we change the background color
var columnWidth = (_w / columnCount).floor
var columnHeight = (_h / 4).floor
if (run % 2 == 0) { // switches color directions with each iteration of while loop
colorComp = 0
} else {
colorComp = 255
colorGap = -colorGap
}
var ystart = columnHeight * run
var xstart = 0
for (i in 0...columnCount) {
var iColor = Math.round(colorComp)
var nextColor = Color.rgb(iColor, iColor, iColor)
Canvas.rectfill(xstart, ystart, xstart + columnWidth, ystart + columnHeight, nextColor)
xstart = xstart + columnWidth
colorComp = colorComp + colorGap
}
run = run + 1
columnCount = columnCount * 2
}
}
 
update() {}
 
draw(alpha) {}
}
 
var Game = GreyBars.new(640, 320)</syntaxhighlight>
 
=={{header|XPL0}}==
Floating point is used to get the full range of black to white.
 
<langsyntaxhighlight XPL0lang="xpl0">include c:\cxpl\codes; \intrinsic 'code' declarations
int Q, N, W, B, C, Y;
[SetVid($112); \640x480x24 graphics
Line 1,618 ⟶ 2,214:
Q:= ChIn(1); \wait for keystroke
SetVid(3); \restore normal text mode
]</langsyntaxhighlight>
 
=={{header|Yabasic}}==
<langsyntaxhighlight Yabasiclang="yabasic">open window 1024, 600
w = peek("winwidth")
h = peek("winheight")
Line 1,643 ⟶ 2,239:
pause .1
next n
next row</langsyntaxhighlight>
 
=={{header|zkl}}==
Line 1,649 ⟶ 2,245:
{{trans|XPL0}}
Uses the PPM class from http://rosettacode.org/wiki/Bitmap/Bresenham%27s_line_algorithm#zkl
<langsyntaxhighlight lang="zkl">img:=PPM(640,480);
foreach q in ([0..3]){ //quarter of screen
n:=(8).shiftLeft(q); //number of bars
Line 1,661 ⟶ 2,257:
}
}
img.write(File("foo.ppm","wb"));</langsyntaxhighlight>
{{out}}
Same as the R image (but smaller):
Line 1,671 ⟶ 2,267:
and we have a set of 8 bars:
 
<langsyntaxhighlight lang="zxbasic">10 REM wind the colour down or use a black and white television to see greyscale bars
20 REM The ZX Spectrum display is 32 columns wide, so we have 8 columns of 4 spaces
25 BORDER 0: CLS
Line 1,679 ⟶ 2,275:
60 NEXT c
70 REM at this point the cursor has wrapped, so we don't need a newline
80 NEXT r</langsyntaxhighlight>
 
{{omit from|GUISS}}
9,485

edits